Forum Discussion
Group by Measure in a Matrix
- 5 years ago
For anyone else that finds this later. I was able to get this to work and here's how. I used Summarize Columns, with a switch function inside for the segments, I could not figure out how to use a measure or a variable inside the summarize columns function so I just typed the calculation each time. I wrapped the whole thing in a Calculate Table function to add a filter. Below is the code
CALCULATETABLE( SUMMARIZECOLUMNS( UCS[GuestID] ,UCS ,"ADTBand" ,SWITCH(TRUE(), Sum(UCS[Orders]) = 0,"0-14", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<15,"0-14", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=15&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<25,"15-24", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=25&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<40,"25-39", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=40&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<75,"40-74", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=75&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<100,"75-99", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=100&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<150,"100-149", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=150&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<225,"150-224", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=225&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<300,"225-299", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=300&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<400,"300-399", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=400&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<500,"400-499", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=500&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<750,"500-749", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=750&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<1000,"750-999", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=1000&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<1250,"1000-1249", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=1250&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<1500,"1250-1499", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=1500&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<2000,"1500-1999", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=2000&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<2500,"2000-2499", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=2500&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<3000,"2500-2999", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=3000&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<3500,"3000-3499", CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=3500,"3500+" )) ,not( UPI[TierID] in {9,10}))
For anyone else that finds this later. I was able to get this to work and here's how. I used Summarize Columns, with a switch function inside for the segments, I could not figure out how to use a measure or a variable inside the summarize columns function so I just typed the calculation each time. I wrapped the whole thing in a Calculate Table function to add a filter. Below is the code
CALCULATETABLE(
SUMMARIZECOLUMNS(
UCS[GuestID]
,UCS
,"ADTBand"
,SWITCH(TRUE(),
Sum(UCS[Orders]) = 0,"0-14",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<15,"0-14",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=15&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<25,"15-24",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=25&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<40,"25-39",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=40&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<75,"40-74",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=75&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<100,"75-99",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=100&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<150,"100-149",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=150&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<225,"150-224",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=225&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<300,"225-299",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=300&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<400,"300-399",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=400&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<500,"400-499",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=500&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<750,"500-749",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=750&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<1000,"750-999",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=1000&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<1250,"1000-1249",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=1250&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<1500,"1250-1499",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=1500&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<2000,"1500-1999",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=2000&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<2500,"2000-2499",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=2500&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<3000,"2500-2999",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=3000&&CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))<3500,"3000-3499",
CALCULATE(Sum(UCS[TotalSpend])/CALCULATE(DistinctCount(UCS[Date]),UCS[Orders] = 1))>=3500,"3500+"
))
,not( UPI[TierID] in {9,10}))- Icey5 years agoCommunity Support
Hi ChadPortman ,
Glad to hear that you have found the solution. Please accept your reply as solution to help people who may have the same question get the solution directly.
In addition, you can use VAR to store the result of an expression as a named variable and use RETURN to give the result.
Reference: DAX: Use variables to improve your formulas - Power BI | Microsoft Docs
Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.