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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
JKPBI
Frequent Visitor

Top 3 Categories Based on Spending Totals

Hello:

 

I am looking to create 3 calculated columns returning the top 1st, 2nd, and 3rd categories which a customer has spent the highest amounts on (over multiple transactions).

 

Below find the kind of data I'm working with, and the result I'm looking for.

 

Category Spending
PersonDateCategoryAmount
Bill1/1/2022A $                             75.00
Bill1/2/2022B $                          100.00
Bill1/3/2022C $                             50.00
Bill1/4/2022A $                          100.00
Bill1/5/2022C $                          100.00
Sue1/6/2022B $                          100.00
Sue1/7/2022A $                             50.00
Sue1/8/2022B $                             50.00
Sue1/9/2022C $                          125.00
Sue1/10/2022B $                             50.00
Greg1/11/2022A $                          100.00
Greg1/12/2022C $                          200.00
Greg1/13/2022C $                             25.00
Greg1/14/2022B $                          100.00
Greg1/15/2022A $                             50.00

 

Customer Lookup
Person1st Highest2nd Highest Category3rd Highest Category
BillACB
SueBCA
GregCAB

 

 

thank you so much for any assistance

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You can create measures like

Top product = 
var summaryTable = ADDCOLUMNS( SUMMARIZE('Table','Table'[Category]), "@amt", CALCULATE( SUM('Table'[Amount])) )
return CONCATENATEX( FILTER( summaryTable, RANKX(summaryTable, [@amt]) = 1 ), [Category], ", ")

and just change the 1 to 2 or 3 for the other measures.

In the event of a tie this will return all the products which are at that rank separated by a comma.

View solution in original post

4 REPLIES 4
CNENFRNL
Community Champion
Community Champion

Believe me, the intricacy is way beyond your comprehension.

This is the underlying dataset with ranks within respective category.

CNENFRNL_1-1655133153708.png

CNENFRNL_0-1655133004400.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

johnt75
Super User
Super User

You can create measures like

Top product = 
var summaryTable = ADDCOLUMNS( SUMMARIZE('Table','Table'[Category]), "@amt", CALCULATE( SUM('Table'[Amount])) )
return CONCATENATEX( FILTER( summaryTable, RANKX(summaryTable, [@amt]) = 1 ), [Category], ", ")

and just change the 1 to 2 or 3 for the other measures.

In the event of a tie this will return all the products which are at that rank separated by a comma.

JKPBI
Frequent Visitor

@johnt75 Wow this is so compact, thank you!

a follow-up (isn't there always?) what would I plug into this formula to also get a corrosponding measure to sum up all their spending on the ranked category (3 more)?

Thank you again! 

You could get the amount with

Spend on Top product = 
var summaryTable = ADDCOLUMNS( SUMMARIZE('Table','Table'[Category]), "@amt", CALCULATE( SUM('Table'[Amount])) )
return MAXX( FILTER( summaryTable, RANKX(summaryTable, [@amt]) = 1 ), [@amt])

In case of ties that will give the amount spent per category, not the total spent.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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