Ravindra BagaleCourses & study guides मराठी Track your progress

13. DAX: Data Analysis Expressions

13.17 RANKX and TOPN in Power BI DAX with Example

RANKX in Power BI with example — in 30 seconds

What: RANKX gives a rank number to each item, for example each city by sales.

Why: "Top 3 cities this month" is the first slide in many monthly reviews.

Tiny example: Mumbai 1, Pune 2, Nashik and Nagpur tie at 3.

First look at the data (before → after):

RANKX in Power BI with example: before and after tables, fictional Blinkit Maharashtra data

Same before / after tables as text

Before — Total Sales by City

City Total Sales
Pune ₹1,200
Mumbai ₹1,500
Nashik ₹300
Nagpur ₹300

About this data: This table uses columns: City, Total Sales. This table uses columns: City, Total Sales, Rank (Skip), Rank (Dense). Values shown are the lesson examples already on this page (not a new dataset).

Formula / click path

City Rank = RANKX(ALL(DarkStore[City]), [Total Sales], , DESC, Skip)
City Rank Dense = RANKX(ALL(DarkStore[City]), [Total Sales], , DESC, Dense)

After — Ranks

City Total Sales Rank (Skip) Rank (Dense)
Mumbai ₹1,500 1 1
Pune ₹1,200 2 2
Nashik ₹300 3 3
Nagpur ₹300 3 3

Do it step by step:

  1. New measure: City Rank (formula above).
  2. Use ALL(DarkStore[City]) so every row ranks against all cities.
  3. Put City, Total Sales and City Rank in a table. Sort by rank.
  4. If a 5th city had less sales, Skip would give it 5 and Dense would give it 4.

RANKX

RANKX(<table>, <expression>, [value], [order], [ties])

Product Rank =
IF(
    HASONEVALUE(Product[Product Name]),
    RANKX(ALL(Product[Product Name]), [Total Sales], , DESC, DENSE)
)

-- Rank dark stores by speed: fastest (lowest time) = rank 1
Store Speed Rank =
IF(
    HASONEVALUE(DarkStore[Store Name]),
    RANKX(ALL(DarkStore[Store Name]), [Avg Delivery Time (mins)], , ASC, DENSE)
)

Customer Rank in City =
RANKX(ALLSELECTED(Customer[Customer Name]), [Total Sales], , DESC)
  • Use ALL(...) so every item is ranked against all items, not only itself.
  • HASONEVALUE hides the meaningless rank on the total row.
  • Order: DESC (highest = 1) or ASC (lowest = 1, useful for delivery time).
  • Ties: SKIP (default: 1, 2, 2, 4) or DENSE (1, 2, 2, 3).

TOPN

TOPN(<n>, <table>, <orderBy expression>, [order]) returns a table with the top N rows.

Top 5 Products Sales =
CALCULATE(
    [Total Sales],
    TOPN(5, ALL(Product[Product Name]), [Total Sales], DESC)
)

Top 5 Share % = DIVIDE([Top 5 Products Sales], CALCULATE([Total Sales], ALL(Product)))

-- Calculated table of the 10 busiest dark stores
Top 10 Stores =
TOPN(
    10,
    ADDCOLUMNS(VALUES(DarkStore[Store Name]), "Orders", [Total Orders]),
    [Orders], DESC
)

Top N without DAX

For a simple "show fakt the top 10 products in this chart", use the Top N filter in the Filters pane (Module 22). Use DAX TOPN when you need a number (like the share of the top 5) or a table.

Ravindra Bagale's Tip

Friends, many students use RANKX(Product, …) and get rank 1 for every row, because the table argument still has the current filter. Use ALL(Product[Product Name]) or ALLSELECTED as the table, and remember that RANKX ties are Skip by default, so pass Dense if you want 1, 2, 2, 3. Never forget this.

Loading DAX practice tasks...

  1. Shop rank (Easy): Shown per ShopName. Measure: Shop Rank = rank of [Revenue] among all shop names (1 = highest).
  2. Barista rank (Easy): Shown per Barista. Measure: Barista Rank = rank of [Revenue] among all baristas.
  3. Smallest city first (Easy): Shown per City. Measure: Low Rank = rank of [Revenue] with the lowest as 1.
  4. Dense rank of items (Medium): Shown per Item. Measure: Item Rank = dense rank of [Revenue] among all items, highest = 1.
  5. Top 2 shops revenue (Medium): Measure: Top 2 Revenue = [Revenue] added over the 2 shops with the highest [Revenue].
  6. Top 3 baristas revenue (Medium): Measure: Top 3 Baristas = [Revenue] added over the 3 baristas with the highest [Revenue].
  7. Best shop name (Medium): Measure: Best Shop = the ShopName with the highest [Revenue].
  8. Most cups of one item (Hard): Measure: Max Item Cups = the highest total Cups of any single Item.
  9. Top 2 label (Hard): Shown per ShopName. Measure: Tier = "Top 2" if the shop rank by [Revenue] is 2 or better, else "Other".
  10. Best item per country (Hard): Shown per Country. Measure: Best Item Revenue = the highest [Revenue] of any Item.

Ravi Sir's Cards for this lesson

मराठी cards