Is there a way to perform a countifs-like function in Beastmode?
Basically, I have a dataset that is appended with new data regularly. I am trying to see how many accounts are associated with date a that are no longer showing up in the most recent (date b) appended data. I can write a meastmode variable that can show what lines qualify as date data a and date data b, and I can figure out the nominal difference, but that difference is a function of two variables: new accounts and closed accounts). I need to separate out new from closed. A countif function would allow me to search a field, using criteria from other fields. Anything like that? Good workarounds?
Comments
-
You can do a case statement inside a Count() function and that should be equivalent to a countif()
Count(Case when `Field1` = 'text' and `DateField` = '2016-04-01' then `Value` else 0 end)
Domo Arigato!
**Say 'Thanks' by clicking the thumbs up in the post that helped you.
**Please mark the post that solves your problem as 'Accepted Solution'1 -
Thank you for your prompt response! Unfortunately, this doesn't seem to be a dynamic solution, meaning it will only work with the one name that I hard code into the formula. I need it to work independently for each name.
As an example, I uploaded a small dataset that has two fields: Month and Name, each with 22 lines. 11 of those Month lines say January and 11 say February. There are 11 unique names in the first 11 lines (January). The names in the next 11 lines (February) are copied from the first 11 lines, and then one name is changed. This simulates an event in which we have 11 accounts in January and 11 accounts in February, but we lost an account and gained an account between these two snapshots.
Ideally, I would love to create a Beast mode field in which it looks at each name and associated month, figures out if it is month n or month n+1, and then looks to see if the name is present in both months. If the name is present in only month n it might say “Account Closed”. If the name is present in only month n+1 it might say “New Account”. If the name is present in both months, it might just be blank.
Am I missing something in your previous comment?
0 -
Was anyone able to find a solution to this problem? Running into the same thing right now.
0 -
Hello,
Sadly there is not a way to accomplish this through a beastmode. Beastmode runs data on a row by row comparison. This means that we cannot compare data from one row to another row.
To accomplish this we will need to use a MySQL dataflow. The query is slightly complex.
Lets say we start with a table such as:month month_number name Jan 1 Albus Dumbledore Jan 1 Harry Potter Jan 1 Hermione Granger Jan 1 Ronald Weasley Jan 1 Rubeus Hagrid Jan 1 Severus Snape Feb 2 Gandalf Feb 2 Hermione Granger Feb 2 Ronald Weasley Feb 2 Rubeus Hagrid Feb 2 Samwise Gamgee Feb 2 Severus Snape Mar 3 Gandalf Mar 3 Hermione Granger Mar 3 Ronald Weasley Mar 3 Rubeus Hagrid Mar 3 Samwise Gamgee Mar 3 Severus Snape
We have several names with people who are 'New Accounts', 'Continuing Accounts' and 'Closed Accounts'
The first step in this is to create the base of our table.SELECT
a.`month`
,a.`month_number`
,a.`name`
,@prev:= `name`
FROM input_dataset a
,(SELECT @prev:='') b
order by `name`, `month_number`Here we create a variable '@prev'. This will be used later to determine if our value from the last row is the same for this row.
We also are ordering first by the name, then the date. (For simplicity I made a month name and month number column)
Now that we have the base of our query we can create our statement to calculate what we need.
Lets break the statement we will need into pieces to get a better understanding of what it does.If(@prev=a.`name` ,'','New Account')
This will look at the previous name in our table and determine if it is new or not new.
We now know when an account/name is new or not new.
Next we will need to determine if ' ' results to a closed account or a continuing account. We will need to add a comparison:a.`month_number` = (SELECT max(`month_number`) month from input_dataset where a.`name` = `name` )
This is going to compare the current month, with the last month created for that account.
WIth these two statements we can make our final statement:IF(If(@prev=a.`name` ,'','New Account') = '' AND a.`month_number` = (SELECT max(`month_number`) month from input_dataset where a.`name` = `name` ),'Account Closed',If(@prev=a.`name` ,'','New Account')) as 'Example'
Our transform/query will look similar to:SELECT
a.`month`
,a.`month_number`
,a.`name`
,IF(If(@prev=a.`name` ,'','New Account') = '' AND a.`month_number` = (SELECT max(`month_number`) month from input_dataset where a.`name` = `name` ),'Account Closed',If(@prev=a.`name` ,'','New Account')) as 'Example'
,@prev:= `name`
FROM input_dataset a
,(SELECT @prev:='') b
order by `name`, `month_number`
After running your dataflow with this query you will create a table that will look like:month month_number name Example Jan 1 Albus Dumbledore New Account Feb 2 Gandalf New Account Mar 3 Gandalf Account Closed Jan 1 Harry Potter New Account Jan 1 Hermione Granger New Account Feb 2 Hermione Granger Mar 3 Hermione Granger Account Closed Jan 1 Ronald Weasley New Account Feb 2 Ronald Weasley Mar 3 Ronald Weasley Account Closed Jan 1 Rubeus Hagrid New Account Feb 2 Rubeus Hagrid Mar 3 Rubeus Hagrid Account Closed Feb 2 Samwise Gamgee New Account Mar 3 Samwise Gamgee Account Closed Jan 1 Severus Snape New Account Feb 2 Severus Snape Mar 3 Severus Snape Account Closed
Please let me know if you would like me to go into further detail on any of the steps this query is doing.**Say “Thanks" by clicking the thumbs up in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"1 -
@shaanarora @nlclayville, tagging you to check out ilikenno's reply.
0 -
Thanks for the update @ilikenno and @kshah008. The issue I'm seeing here is that the current or most recent month will always say account closed. I forgot to update earlier but I used the LAG function in Redshift to accomplish what I needed. It may not be applicable in this situation but might be worth checking out to @nlclayville
0 -
@shaanarora,
Glad you were able to use the LAG function.
Just for additional reference. As mentioned the most recent will always return "Account Closed". We can use another formula to avoid this.
In the original "Example" query we will replace 'Account Closed' with another formula.IF(If(@prev=a.`name` ,'','New Account') = '' AND a.`month_number` = (SELECT max(`month_number`) month from input_dataset where a.`name` = `name` ),'Account Closed',If(@prev=a.`name` ,'','New Account')) as 'Example'
Replace with:
CASE
WHEN MONTH(`Date`) = (SELECT MONTH(MAX(`date`)) from input_dataset)
AND YEAR(`Date`) = (SELECT YEAR(MAX(`date`)) from input_dataset)
THEN 'Account Closed'
ELSE 'Current Account'
END
@nlclayville,
If you have any additional questions please let me know.**Say “Thanks" by clicking the thumbs up in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"1 -
worked, thanks!
0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 618 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 741 Beast Mode
- 58 App Studio
- 40 Variables
- 685 Automate
- 176 Apps
- 452 APIs & Domo Developer
- 47 Workflows
- 10 DomoAI
- 36 Predict
- 15 Jupyter Workspaces
- 21 R & Python Tiles
- 395 Distribute
- 113 Domo Everywhere
- 276 Scheduled Reports
- 6 Software Integrations
- 124 Manage
- 121 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 108 Community Announcements
- 4.8K Archive