INDEX, RANK and ROWNUMBER in DAX window functions

How the DAX window functions INDEX, RANK and ROWNUMBER work, the first or last row of a sorted table with INDEX, rank with DENSE or SKIP ties, unique row numbers, PARTITIONBY for groups, totals, and when RANKX is still the better choice.

Short answer: INDEX ( position, relation, ORDERBY ( ... ) ) returns the row at an absolute position of a sorted table: 1 is the first, -1 the last. In CALCULATE, it gives "the first month's revenue" or "the best product's revenue". RANK ( [ties], relation, ORDERBY ( ... ) ) returns the current row's rank by columns, with DENSE or SKIP ties. ROWNUMBER ( relation, ORDERBY ( ... ) ) gives a unique number, and needs an order without ties. All take PARTITIONBY to restart per group. To rank by a measure in a model measure, RANKX remains the tool.

These functions bring SQL's window functions to DAX. They shine with sorted columns (dates, codes, list prices) and in visual calculations.

What does each return?

ProductList PriceRANK (SKIP)RANK (DENSE)ROWNUMBER
Road-1503,578111
Mountain-2002,320222
Touring-10002,320223
Helmet35434
Bottle5545
Products sorted by list price

ROWNUMBER gives every row a unique number. When the order has ties, Microsoft's reference says it looks for the fewest extra columns of the relation that make each row unique and appends them to the order; if none exist and a tie is found, it returns an error. Name the tie-breaker yourself so the order is the one you want (here ORDERBY ( 'Product'[List Price], DESC, 'Product'[Product], ASC )).

-- calculated column on Product
Price Rank = RANK ( DENSE, ALL ( 'Product' ), ORDERBY ( 'Product'[List Price], DESC ) )

Price Row =
ROWNUMBER (
    ALL ( 'Product' ),
    ORDERBY ( 'Product'[List Price], DESC, 'Product'[Product], ASC )
)

How does INDEX pick a row?

-- revenue of the first month in the selection
First Month Revenue =
CALCULATE (
    [Revenue],
    INDEX ( 1, ALLSELECTED ( 'Date'[Year Month] ), ORDERBY ( 'Date'[Year Month] ) )
)

-- the last month
Last Month Revenue =
CALCULATE (
    [Revenue],
    INDEX ( -1, ALLSELECTED ( 'Date'[Year Month] ), ORDERBY ( 'Date'[Year Month] ) )
)

-- growth from first to last month of the selection
Growth First to Last = DIVIDE ( [Last Month Revenue] - [First Month Revenue], [First Month Revenue] )

INDEX returns an empty table for position 0, BLANK or out of range.

How do they behave in visuals and totals?

The current row is matched by the visual's values for the relation's columns, so the relation must contain the columns on the visual. Microsoft notes that RANK returns blank for total rows. In visual calculations the same functions work on the visual's axis without a relation:

Sales Rank Within Year = RANK ( DENSE, ORDERBY ( [SalesAmount], DESC ), PARTITIONBY ( [CalendarYear] ) )

That is Microsoft's visual calculation example, and in visual calculations ordering by a measure is natural, because the visual's rows already hold the values.

RANK or RANKX?

NeedUse
rank by a column (price, date, code)RANK
rank by a measure in a model measureRANKX ( ALLSELECTED ( ... ), [Revenue] )
rank by a measure on one visualRANK in a visual calculation
unique sequence numbersROWNUMBER

Microsoft states that RANK does not relate to RANKX the way SUM relates to SUMX: they are different functions for different jobs.

What goes wrong?

  • ROWNUMBER with ties. It adds tie-breaker columns itself, or errors when it cannot; name the tie-breaker so the order is predictable.
  • Relation without the visual's columns. Rows cannot be matched; results repeat or are blank.
  • Ranking by a measure with RANK in a model measure. Use RANKX, or a visual calculation.
  • Sorting text months. Order by a sortable key.

Where can you check this?

Where do you start?

Write First Month Revenue and Last Month Revenue with INDEX and show the growth across whatever period the reader selects. Visual calculation functions cover the same ideas on one visual.

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.