Stop 09 · Advanced DAX PatternsTopics · 3/10

RANKX

Part of the Power BI Roadmap

In short

"Which are our top 10 products?" is asked in every sales review. RANKX gives each product its position, and the table you rank over decides who it competes against.

RANKX ranks items, like products by sales, over a table you provide. Using ALL or ALLSELECTED on that table controls what the ranking is compared against.

The jaadu stands on a sports day podium handing medals to runners while a long line of other runners waits behind them on the track.

Think of it like this

School sports day results

In the 100 m race, Meera can be 2nd in her class, 5th in her school and 40th in the district. Her time didn't change, only the group she's compared with. RANKX works the same: ALL ranks against every product, ALLSELECTED ranks only against the ones the user's slicers left on the field.

Watch it happen

Ranking five products by sales

Atta ₹9L
0
Rice ₹12L
1
Dal ₹9L
2
Oil ₹6L
3
Tea ₹4L
4

1/5RANKX evaluates [Total Sales] for every product in the table you give it.

The big ideas

What you need to know

Table, then expression

RANKX ( table, expression ) evaluates the expression for each row of the table, then finds where the current value fits.

ALL vs ALLSELECTED

ALL ( Product[Product Name] ) ranks against every product. ALLSELECTED respects slicers, so ranks restart from 1 after filtering.

Ties: Skip or Dense

Skip gives 1, 2, 2, 4. Dense gives 1, 2, 2, 3. Pick what your readers expect.

Hide rank on totals

The total row has no single product, so wrap RANKX in IF ( HASONEVALUE ( ... ) ) to avoid a meaningless rank of 1.

See it in code

Walk through the code

dax
1Product Rank =2IF (3    HASONEVALUE ( Product[Product Name] ),4    RANKX (5        ALL ( Product[Product Name] ),6        [Total Sales],7        ,8        DESC,9        DENSE10    )11)12 13Product Rank (Selected) =14IF (15    HASONEVALUE ( Product[Product Name] ),16    RANKX ( ALLSELECTED ( Product[Product Name] ), [Total Sales], , DESC, DENSE )17)

1/4Only rank when exactly one product is in context, so totals stay blank.

Two ranks: one against all products, one against only the products left after slicers.

Save yourself the pain

Common mistakes

Oops

RANKX ( Product, [Total Sales] ) used directly in a table that is sliced by Category.

Do this instead

Ranking over the whole Product table with filters still on can return 1 for everyone. Use ALL or ALLSELECTED on the column you rank.

Oops

Ranking with a column like SUM ( Sales[Amount] ) instead of a measure.

Do this instead

Use a measure such as [Total Sales]. A plain SUM inside RANKX doesn't get context transition for each product, so every product gets the same value.

Your mission

Rank the top products

~20 min

Quick check

A slicer keeps only the Snacks category. Which rank would show 1 for the best-selling snack even if a staple outsells it overall?

Go deeper

  1. 1Create a Product Rank measure using RANKX over ALL ( Product[Product Name] ).
  2. 2Use it as a visual filter to show only the top 10 products.
  3. 3Test how the Dense and Skip ties options change results.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge