RANKX in DAX: rank products, customers and months

How RANKX ranks in DAX, the table, expression, value, order and ties arguments, ranking over ALL or ALLSELECTED, dense against skip ties, ranking within a category, top N with a rank, hiding the total, and common RANKX mistakes.

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?

ArgumentMeaningDefault
tablethe rows to rank amongrequired
expressionevaluated per row of table, the values to rankrequired
valuethe value whose rank you wantexpression in the current context
orderDESC (0, FALSE): largest is 1; ASC (1, TRUE): smallest is 1DESC
tiesSkip: 1, 2, 2, 4; Dense: 1, 2, 2, 3Skip
RANKX arguments

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?

ProductRevenueRank (Skip)Rank (Dense)
Road-1501,20011
Mountain-20090022
Touring-100090022
Helmet50043
Bottle32054
Products ranked with Skip and Dense

What goes wrong?

  • VALUES in the table argument. Everything ranks 1; use ALL or ALLSELECTED.
  • 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?

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.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.