Forum Discussion
Create measure that replicates SUMIF/matrix aggregation
- 1 year ago
Hi Y1009666
% of something =VAR SYTD = SUM('Table'[YTD])VAR FYTD = SUM('Table'[Final])RETURNSWITCH(TRUE(),ISINSCOPE('Table'[Level 5]) && SYTD > 0 --if on the 5 row and has sales,SUM('Table'[YTD])/SUM('Table'[Final]),ISINSCOPE('Table'[Level 4]) && SYTD <> 0 || ISFILTERED('Table'[Level 5]) --row 4 and sales or level 5,CALCULATE(SUM('Table'[YTD])/SUM('Table'[Final]),ALLSELECTED('Table'[Level 5])),ISINSCOPE('Table'[Level 3]) && SYTD <> 0 || ISFILTERED('Table'[Level 4]),CALCULATE(SUM('Table'[YTD])/SUM('Table'[Final]),ALLSELECTED('Table'[Level 4])),ISINSCOPE('Table'[Level 2]) && SYTD <> 0 || ISFILTERED('Table'[Level 3]),CALCULATE(SUM('Table'[YTD])/SUM('Table'[Final]),ALLSELECTED('Table'[Level 3])),ISINSCOPE('Table'[Level 1]) && SYTD <> 0 || ISFILTERED('Table'[Level 2]),CALCULATE(SUM('Table'[YTD])/SUM('Table'[Final]),ALLSELECTED('Table'[Level 2])))
Hi Y1009666 ,
To handle this scenario in DAX, we want a measure that first calculates the percentage of YTD over Final at the most granular level (Level 5). However, if the YTD value is zero, we want the calculation to fall back to the next level up (Level 4), summing the YTD and Final values for the entire Level 4 group while still respecting any external filters like Country. Here's how you can write the DAX measure to accomplish that:
Projection % =
VAR YTD_Level5 = CALCULATE(SUM('YourTable'[YTD]))
VAR Final_Level5 = CALCULATE(SUM('YourTable'[Final]))
VAR YTD_Level4 = CALCULATE(SUM('YourTable'[YTD]), REMOVEFILTERS('YourTable'[Level 5]))
VAR Final_Level4 = CALCULATE(SUM('YourTable'[Final]), REMOVEFILTERS('YourTable'[Level 5]))
RETURN
IF(
YTD_Level5 <> 0 && Final_Level5 <> 0,
DIVIDE(YTD_Level5, Final_Level5),
DIVIDE(YTD_Level4, Final_Level4)
)
This measure checks if the Level 5 YTD is non-zero and uses that value directly. If the YTD is zero (or Final is zero), it removes the Level 5 filter to compute the totals for Level 4 instead, while still keeping all other filters in place (like Country or anything else selected in slicers). This ensures that you get accurate fallback aggregation that matches what the matrix visual would show if you collapsed Level 5. Let me know if you need a fallback to Level 3 as well—this can be extended further with the same logic.
Best regards,
- Y10096661 year agoFrequent Visitor
Thanks for the reply - this didn't seem to remove the row context from Level 5 and instead was producing the same result as doing the calculation on the standard values. The other solution has worked anyway!