Forum Discussion
How to SUM by subgroups based on conditional criteria?
- 3 years ago
Hey Anonymous ,
your last explanation is exactly what is needed to avoid misunderstandings.
First I created a calculated column to extract the year to have a numeric representation of the FY, this makes checking rule 2 more simple:Giftdata_FiscalYear_Value = RIGHT( 'Gifts'[Giftdate_FiscalYear] , 4)It is recommended creating this column using Power Query, or even better already in the source system. Nevertheless, I use DAX because because simplicity.
Then I use DAX to create this measure:Measure = var thevalue = SUMX( FILTER( ADDCOLUMNS( FILTER( ADDCOLUMNS( SUMMARIZE( 'Gifts' , Gifts[Person_Id] , Gifts[Giftdata_FiscalYear_Value] ) , "# of entries" , CALCULATE( COUNTROWS( 'Gifts' ), ALL( Gifts[Giftdata_FiscalYear_Value] , Gifts[Giftdate_FiscalYear] ) ) ) , [# of entries] >= 3 && [Giftdata_FiscalYear_Value] >= 2021 ) , "sum of rev" , CALCULATE( SUM(Gifts[Revenue] ) ) ) , [sum of rev] >= 60 ) , [sum of rev] ) return IF( ISBLANK( thevalue ) , 0 , thevalue )This allows to create this table visual:
Please be aware that showing 0 instead of BLANK (meaning an empty cell) can become costly the larger the table gets.
Hopefully, this provides what you are looking for.
Regards,
Tom
Hey Anonymous ,
can you please add to each row of the expected result table which of the three roles is met like so
- row 1
- rule 1 true
- rule 2 true
- rule 3 true
- row 2
- ...
The reason for this, simply avoid misunderstandings like
- checking for 3 entries on or after 2021 or
- checking for 3 entries also before 2021 but the revenue is only considered for years on or after 2021
Regards,
Tom
- Anonymous3 years agoNot applicable
Hello Tom,
Thank you for your reply. A suggestion that sounds like a good idea! But how would I go about doing a proper grouping at the [Person Id] level while still checking each row to see it matches each criteria? I am new to DAX/Power BI. Is there a way to use a IF function for this properly?
- TomMartens3 years ago
Super User
Hey Anonymous ,
sorry, my bad! When I ask for the rules for each row of the expected result table, this is not part of the measure, but instead part of the business requirement. I consider your initial description not 100% unambigous. Explaining how the values you are looking for are calculated row by row helps us to define a measure.
Regards,
Tom
- Anonymous3 years agoNot applicable
Tom,
The rows are already defined in the table with data similar to the example. Basically each person in the table has their own ID, a donation gift amount, and the fiscal year that the gift occurred within.
What I am unsure of is how to look at the table in terms of grouping the person Ids together as groups and within that, to check for any donations that occurred in 2021 or later and then summing all donations that are 60.00 or but totalling them within each financial year. Basically it would be main group of person Ids -> subgroups of fiscal years for each person -> and then looking at all amounts within the fiscal year subgroups still by person. When all criteria are met (person has to have more three or more entries existing first, then those entries for fiscal years 2021 and beyond, and then checking to see that the sum of all donations within the fiscal year are 60.00 or more) then the donations within each fiscal year per person are either summed or it is to show a 0.00 amount.
I hope that helps. Sorry if I am confusing! I appreciate your assistance.
I hope that