Short answer: RANKX ( table, expression [, value [, order [, ties ]]] ) evaluates the expression for every row of the table, then returns the position of the current value among them. To rank products by revenue in a visual, use RANKX ( ALL ( 'Product'[Product] ), [Revenue] ) (rank among all products) or ALLSELECTED (among the selected ones). Order is descending by default (largest is 1). Ties are Skip by default (1, 2, 2, 4); pass DENSE for 1, 2, 2, 3.
"Top 10 customers" and "where does this product rank" are among the most asked report questions, and RANKX returns the wrong number in a surprisingly common way: everything ranks 1.
Why does everything rank 1?
Rank Wrong = RANKX ( VALUES ( 'Product'[Product] ), [Revenue] )
Rank = RANKX ( ALL ( 'Product'[Product] ), [Revenue] )On each row of a table by product, VALUES ( 'Product'[Product] ) contains only that product, so it is first of one. The table argument must list all the products to compare with, so filters on the product must be removed: ALL or ALLSELECTED.
What do the arguments do?
| Argument | Meaning | Default |
|---|---|---|
| table | the rows to rank among | required |
| expression | evaluated per row of table, the values to rank | required |
| value | the value whose rank you want | expression in the current context |
| order | DESC (0, FALSE): largest is 1; ASC (1, TRUE): smallest is 1 | DESC |
| ties | Skip: 1, 2, 2, 4; Dense: 1, 2, 2, 3 | Skip |
Skip arguments with empty commas: RANKX ( ALL ( 'Product'[Product] ), [Revenue], , , DENSE ). If value is not among the values, RANKX adds it temporarily to find where it would rank.
How do you rank within a group?
Rank products within their category, so each category has its own 1:
Rank in Category =
RANKX (
ALLEXCEPT ( 'Product', 'Product'[Category] ),
[Revenue]
)ALLEXCEPT removes the product filter but keeps the filter on Category, so each product is compared with the products of its own category. That needs Category on the rows or in a filter: with only Product on the rows there is no category filter to keep, and the rank is among all products. A version that works either way:
Rank in Category =
VAR Cat = SELECTEDVALUE ( 'Product'[Category] )
RETURN
RANKX (
FILTER ( ALL ( 'Product'[Product], 'Product'[Category] ), 'Product'[Category] = Cat ),
[Revenue]
)How do you hide the total and show top N?
Product Rank =
IF (
ISINSCOPE ( 'Product'[Product] ),
RANKX ( ALLSELECTED ( 'Product'[Product] ), [Revenue], , DESC, DENSE )
)
Is Top 10 = IF ( [Product Rank] <= 10, 1 )ISINSCOPE blanks the rank on the total row, where it means nothing. Use Is Top 10 as a visual filter (is 1), or use the visual's Top N filter type, which does the same without DAX. To keep the rest as one line, see TOPN.
What does the result look like?
| Product | Revenue | Rank (Skip) | Rank (Dense) |
|---|---|---|---|
| Road-150 | 1,200 | 1 | 1 |
| Mountain-200 | 900 | 2 | 2 |
| Touring-1000 | 900 | 2 | 2 |
| Helmet | 500 | 4 | 3 |
| Bottle | 320 | 5 | 4 |
What goes wrong?
- VALUES in the table argument. Everything ranks 1; use
ALLorALLSELECTED. - Blank revenue. BLANK is treated as 0, so products without sales still get a rank (last); filter them out with
IF ( NOT ISBLANK ( [Revenue] ), ... ). - Decimal comparisons. Microsoft warns that comparisons of Decimal Number values can give unexpected results because of precision; round the expression if ties look wrong.
- Ranking over the fact table.
RANKX ( Sales, ... )ranks sales lines; rank the column you show. - Slow ranks. The expression runs once per row of the table, for every cell; keep the table small.
Where can you check this?
- Microsoft Learn: RANKX function (DAX)
- Microsoft Learn: ISINSCOPE function (DAX)
- Microsoft Learn: RANK function (DAX)
Where do you start?
Write Product Rank with ALLSELECTED, DENSE and ISINSCOPE, and add it to your product table. INDEX, RANK and ROWNUMBER covers the newer window-function way to rank by columns.