Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

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

  • 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.

4 Replies

  • 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.

    • Anonymous's avatar
      Anonymous
      Not applicable

      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! 

      • johnt75's avatar
        johnt75
        Icon for Super User rankSuper User

        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.

  • CNENFRNL's avatar
    CNENFRNL
    Icon for Community Champion rankCommunity Champion

    Believe me, the intricacy is way beyond your comprehension.

    This is the underlying dataset with ranks within respective category.