Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
ChadPortman
Frequent Visitor

Group by Measure in a Matrix

I have been searching for answers on this and have found several things that might work but I don't understand how to use them. Hoping someone can help me understand.

 

I will try to explain what all I have and what I am trying to do. I have a measure called AverageDailyWorth that is just a sum of the customers spend divided by the number of trips they have made. I have a second measure using the switch function to roup the customer into ranges based on the AverageDailyWorth. So for example into groups like $0-$14, $15-$49,$50-$99,$100+.

 

The goal is to have a Matrix that lists all of these groupings on the rows and then I can get the combined sum of values in columns for all the members in each group. This is super simple in Excel as I would just do a vlookup to assign to the Grouping and then do a pivot table based on the column from the vlookup. However recreating pivot tables in Power BI seems to be impossible when you are using a measure. At least to me.

 

I have found the following ideas in my reasearch on this and they might or might not wok but with my limited understanding of the I have not gotten a workable solution:

 

  1. disassociated table
  2. virtual table
  3. virtual relationships

I am still very new to Power BI and this foru as well. If there is a way I can provide a file with sample data and that is useful please let me know how. I will gladly make a mock sample file in Power BI as well as the expected result from Excel I would like to recreate.

1 ACCEPTED SOLUTION
ChadPortman
Frequent Visitor

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}))

View solution in original post

2 REPLIES 2
ChadPortman
Frequent Visitor

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}))

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.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors