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.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 616 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 738 Beast Mode
- 57 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
- 394 Distribute
- 113 Domo Everywhere
- 275 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