aggregate function with case when condition in calculated field
Hi, have a question for how aggregate function with case when condition in calculated field.
- domo dataset follow attached schema/data,
- then create a Single Value card from this dataset,
- then I created a filter Team=team1
I want to archive:
have a calculated field , use total of col Old_Col before 2023-04-01( should be 2) , or use col New_Col after 2023-04-01( should be 2) , get the sum of this, to divide Count(Distinct(`ID`)) ( should be 7) , to get a rate, as 4/7 =0.57 .
Here is how I write the calculated field:
Name: Failed_Rate
Calculation: (CASE WHEN ReleasedDate
>= '2023-04-01T00:00:00' THEN SUM(New_Col
)
WHEN ReleasedDate
< '2023-04-01T00:00:00' THEN SUM(Old_Col
)
END)/Count(DISTINCT(ID
))
Name: Total_Failed
Calculation: (CASE WHEN ReleasedDate
>= '2023-04-01T00:00:00' THEN SUM(New_Col
)
WHEN ReleasedDate
< '2023-04-01T00:00:00' THEN SUM(Old_Col
)
END)
But it displays something odd:
1. why in Data Table the value is 0.29, but Card display 2.4?
2. Total_Failed should be 4, not 2
I think somehow my calculated field as Failed_Rate not correct, but I am not sure how to fix it.
Thank you for any help
Best Answer
-
Good Morning Liuli,
Try this…
SUM(CASE WHEN
ReleasedDate
>= '2023-04-01T00:00:00' THENNew_Col
WHENReleasedDate
< '2023-04-01T00:00:00' THENOld_Col
END)The reason your original calculation did not work is because a CASE statement is granular and a SUM statement is aggregate. Using aggregate statements inside granular statements is very unpredictable.
Hope this helps!
Samantha
Brought to you by Arigato Analytics.
2
Answers
-
Good Morning Liuli,
Try this…
SUM(CASE WHEN
ReleasedDate
>= '2023-04-01T00:00:00' THENNew_Col
WHENReleasedDate
< '2023-04-01T00:00:00' THENOld_Col
END)The reason your original calculation did not work is because a CASE statement is granular and a SUM statement is aggregate. Using aggregate statements inside granular statements is very unpredictable.
Hope this helps!
Samantha
Brought to you by Arigato Analytics.
2 -
@Sam_Arigato , you solution works!
it totally saved my day and thank you so much for you quick response.0
Categories
- All Categories
- 1.7K Product Ideas
- 1.7K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 292 Workbench
- 4 Cloud Amplifier
- 8 Federated
- 2.8K Transform
- 95 SQL DataFlows
- 600 Datasets
- 2.1K Magic ETL
- 3.7K Visualize
- 2.4K Charting
- 685 Beast Mode
- 43 App Studio
- 38 Variables
- 655 Automate
- 170 Apps
- 438 APIs & Domo Developer
- 42 Workflows
- 5 DomoAI
- 32 Predict
- 12 Jupyter Workspaces
- 20 R & Python Tiles
- 384 Distribute
- 110 Domo Everywhere
- 268 Scheduled Reports
- 6 Software Integrations
- 111 Manage
- 108 Governance & Security
- 8 Domo University
- 25 Product Releases
- Community Forums
- 39 Getting Started
- 29 Community Member Introductions
- 98 Community Announcements
- Domo Community Gallery
- 4.8K Archive