DAX Rank Within Category Measure

Ranks each product against only the other products in its own category, instead of the whole table.

Ranking and Top NAdvancedrankxallexceptpartitioncategory

DAX code

Rank Within Category =
-- ALLEXCEPT clears every filter on Product except Category, so ranking restarts per category
RANKX(
    ALLEXCEPT('Product', 'Product'[Category]),
    [Total Sales],
    ,
    DESC,
    DENSE
)

Notes

Replace 'Product'[Category] with whatever column should define each ranking group.

Requirements

Builds on these measures

How to use this measure

  1. In Power BI Desktop, select the table that should hold the measure and choose New measure on the ribbon.
  2. Paste the code, then replace the placeholder table and column names with your own. The My table names fields above do this for you.
  3. Run it through the DAX Formatter if you want it laid out in your team's style.

Related DAX measures

More DAX resources

Browse all 175 measures in the Power BI DAX Measures Library, or search the library for something similar to Rank Within Category.