Sub topic 10
RANKX: ranking items by a measure
This chapter assumes you can write basic DAX measures, know the difference between row context and filter context, and have met context transition in the previous chapters. Here you build the first working RANKX measure and see why it behaves the way it does.
What RANKX does
RANKX is an iterator that returns a position number. It takes a table of items, evaluates an expression for every item, and reports where the current item's value falls among all of them.
The syntax is:
RANKX ( <Table>, <Expression> [, <Value> [, <Order> [, <Ties>]]] )Tableis the set of items to rank against, for example all products.Expressionis evaluated once for each row of that table. It is usually a measure.Valueis the value to look up in the ranking. When you omit it, RANKX evaluatesExpressionin the current context.OrderisDESC(the default, largest value is rank 1) orASC.TiesisSkip(the default) orDense. The next chapters cover both arguments in detail.
A first ranking measure
The measure below ranks each product by sales amount. It assumes a [Sales Amount] measure like the one built earlier in this tutorial.
Product Rank =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)Place Product[Product Name] and [Product Rank] in a table visual. The product with the highest sales shows 1, the next shows 2, and so on.
How the evaluation works
RANKX runs in two steps for each cell of the visual.
- Evaluate the current value. Because
Valueis omitted, RANKX evaluates[Sales Amount]in the filter context of the current row of the visual. For the row showing "Contoso Laptop", this is the sales of that one product. - Build the ranking list. RANKX iterates over
ALL ( Product[Product Name] ). For each product it evaluates[Sales Amount]. That works because the iterator creates a row context and the measure reference triggers context transition, so each product becomes a filter on the measure. The result is a list of one sales value per product.
RANKX then counts where the current value sits in that list, in descending order.
Why the table argument uses ALL
In the visual, each row already filters Product[Product Name] to one product. If you passed Product[Product Name] or VALUES ( Product[Product Name] ) as the table, RANKX would iterate over only that one product. Every product would then be compared against itself and show rank 1.
ALL ( Product[Product Name] ) removes the filter from that column. The ranking list then contains every product, whatever row the visual is drawing.
Wrong Rank =
RANKX (
VALUES ( Product[Product Name] ),
[Sales Amount]
)In a table visual by product name, Wrong Rank returns 1 on every row.
The Value argument
Usually you leave Value empty. You can supply it when the number to look up differs from the ranking expression. For example, RANKX ( ALL ( Product[Product Name] ), [Sales Amount], [Budget Amount] ) finds where the budget falls among the sales values. Most ranking measures never need this.
Common errors
| Symptom | Likely cause |
|---|---|
| Every row shows rank 1 | The table argument is filtered to the current item. Use ALL or ALLSELECTED. |
| Ranks skip numbers (1, 2, 2, 4) | The default `Skip` tie handling is in effect. Use `Dense` for 1, 2, 2, 3. |
| A rank appears on the Total row | Nothing guards against multiple items in context. The pitfall is covered later in this tutorial. |
| Ranking the wrong column | The table argument names a different column than the one on the visual rows. |
Exercise
Exercise
A table visual lists Product[Product Name] and a measure defined as RANKX ( VALUES ( Product[Product Name] ), [Sales Amount] ). Every row shows 1. Why, and how do you fix it?
Show answer
In each visual row, the filter context limits Product Name to one product, so VALUES returns a one-row table. RANKX compares that product's sales with only its own sales, which always gives rank 1. Replace VALUES with ALL ( Product[Product Name] ) so the ranking list contains every product regardless of the row.
Exercise
In RANKX ( ALL ( Product[Product Name] ), [Sales Amount] ), what makes [Sales Amount] return a different value for each product while RANKX iterates?
Show answer
The iterator creates a row context for each product. A measure reference is wrapped in an implicit CALCULATE, so context transition turns that row context into a filter on Product Name, and the measure returns that product's sales.