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?
| Product | List Price | RANK (SKIP) | RANK (DENSE) | ROWNUMBER |
|---|---|---|---|---|
| Road-150 | 3,578 | 1 | 1 | 1 |
| Mountain-200 | 2,320 | 2 | 2 | 2 |
| Touring-1000 | 2,320 | 2 | 2 | 3 |
| Helmet | 35 | 4 | 3 | 4 |
| Bottle | 5 | 5 | 4 | 5 |
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?
| Need | Use |
|---|---|
| rank by a column (price, date, code) | RANK |
| rank by a measure in a model measure | RANKX ( ALLSELECTED ( ... ), [Revenue] ) |
| rank by a measure on one visual | RANK in a visual calculation |
| unique sequence numbers | ROWNUMBER |
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?
- Microsoft Learn: INDEX function (DAX)
- Microsoft Learn: RANK function (DAX)
- Microsoft Learn: ROWNUMBER function (DAX)
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.