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
gluizqueiroz
Resolver I
Resolver I

RANKX over 2 columns

I have the following table:

 

ProductGroupQty Sales
MousePC15
KeyboardPC20
MonitorPC12
CablePC28
BananaFruits64
AppleFruits52
WatermelonFruits41
OvenKitchen  Appliances5
FridgeKitchen  Appliances3
Microwave ovenKitchen  Appliances4

 

I need to make a RANKX about Products but ignoring the column Group, like the following table:

 

RANKXProductGroupQty Sales
1BananaFruits64
2AppleFruits52
3WatermelonFruits41
4CablePC28
5KeyboardPC20
6MousePC15
7MonitorPC12
8OvenKitchen  Appliances5
9Microwave ovenKitchen  Appliances4
10FridgeKitchen  Appliances3

 

If I make a basic measure like the following: 

Ranking = RANKX(ALLSELECTED(Fact_Sales[Product]);[QtySakes])

PowerBI will calculate a rank over Product but considering Group, in other words, the PowerBI will calculate "3 ranks" in 1 table. 1 rank for Fruits, 1 rank for PC and 1 rank for Kitchen Appliances, and it is not correct for my case.

3 ACCEPTED SOLUTIONS
Anonymous
Not applicable

You can do this in Power Query with a few steps. Can also be done in DAX, but went with the Power Query version first.

 

Steps:

  1. Group the table by Group, and want All Rows as the aggregation
  2. Groupby.png
  3. Add a custom column to sort each sub-table by Sales high to low with this code:
Table.Sort( 
[All Data], {"Qty Sales", Order.Descending})
  • Then add another column with will but an idex (or rank in this case) to each subtable starting at one and incrementing from there. This is why we needed to sort in the previous step:
Table.AddIndexColumn(
[SortSubTable], "Rank", 1,1)
  • Remove all the other columns except the one just created
  • Expand that column
  • Set data types and sort if you want

Final Table:

Final Table.png

 

But now looking at your request, that may not be what you had in mind!  If you want to rank over the entire table this will work:

Rank = RANKX( ALL( BasicTable), [Total Qty],,DESC,Dense)

Basic Table Matrix.png

 

Hopefully some of that was helpful 🙂

View solution in original post

@gluizqueiroz  add following column in your model and this will do

 

Rank = RANKX(  ALL( Table5 ) ,
    Table5[Qty Sales], , DESC, Dense )

here is the output

 

image.png



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

Zubair_Muhammad
Community Champion
Community Champion

@gluizqueiroz 

 

As a MEASURE, we could use

 

Ranking =
RANKX (
    ALLSELECTED ( Fact_Sales[Product] ),
    CALCULATE ( SUM ( Fact_Sales[Qty Sales] ), ALL ( Fact_Sales[Group] ) ),
    CALCULATE ( SUM ( Fact_Sales[Qty Sales] ) )
)

Regards
Zubair

Please try my custom visuals

View solution in original post

7 REPLIES 7
Zubair_Muhammad
Community Champion
Community Champion

@gluizqueiroz 

 

As a MEASURE, we could use

 

Ranking =
RANKX (
    ALLSELECTED ( Fact_Sales[Product] ),
    CALCULATE ( SUM ( Fact_Sales[Qty Sales] ), ALL ( Fact_Sales[Group] ) ),
    CALCULATE ( SUM ( Fact_Sales[Qty Sales] ) )
)

Regards
Zubair

Please try my custom visuals

Hey @Zubair_Muhammad@parry2k  and @Anonymous.

I tried all solutions ang I got the same result in all of them:

RANKXProductGroupQty Sales
1BananaFruits64
1CablePC28
1OvenKitchen  Appliances5
2AppleFruits52
2KeyboardPC20
2Microwave ovenKitchen  Appliances4
3WatermelonFruits41
3MousePC15
3FridgeKitchen  Appliances3
4MonitorPC12

@gluizqueiroz well then you have solution in place 🙂



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Thanks for all your input so far, really helped me out to get the ranking/sorting correct in 99,9% of all cases.

But is there a possibilty to add an extra ordering option which should be used if the totals are identical? If the Total QTY for Banana and Apple is equal I would like to add an extra sort on alpabetical order resulting in Apple with Rank 1 and Banana with Rank 2.

At the moment they both get Rank 1.

Anonymous
Not applicable

You can do this in Power Query with a few steps. Can also be done in DAX, but went with the Power Query version first.

 

Steps:

  1. Group the table by Group, and want All Rows as the aggregation
  2. Groupby.png
  3. Add a custom column to sort each sub-table by Sales high to low with this code:
Table.Sort( 
[All Data], {"Qty Sales", Order.Descending})
  • Then add another column with will but an idex (or rank in this case) to each subtable starting at one and incrementing from there. This is why we needed to sort in the previous step:
Table.AddIndexColumn(
[SortSubTable], "Rank", 1,1)
  • Remove all the other columns except the one just created
  • Expand that column
  • Set data types and sort if you want

Final Table:

Final Table.png

 

But now looking at your request, that may not be what you had in mind!  If you want to rank over the entire table this will work:

Rank = RANKX( ALL( BasicTable), [Total Qty],,DESC,Dense)

Basic Table Matrix.png

 

Hopefully some of that was helpful 🙂

Anyone else use this approach. The steps here arent clear however.

@gluizqueiroz  add following column in your model and this will do

 

Rank = RANKX(  ALL( Table5 ) ,
    Table5[Qty Sales], , DESC, Dense )

here is the output

 

image.png



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.