Sub topic 10
RANKX: ranking items by a measure
This chapter assumes you can write basic DAX measures with SUM and CALCULATE. It also assumes you know what a star schema is, and that you have read the earlier chapters on row context, filter context, and context transition. RANKX is an iterator, so everything you learned about SUMX and AVERAGEX applies here as well.
What RANKX does
RANKX returns the position of the current item in a list of items, ordered by a value you choose. A typical use is "rank each product by sales amount". The function does three things:
- It builds a table of items to rank. This is the first argument.
- It evaluates an expression once for each item in that table. This is the second argument, usually a measure.
- It evaluates the same expression for the current item, then reports where that value falls among the values from step 2.
That is the same iterator pattern as SUMX: a table, an expression evaluated per row, and a final aggregation. The aggregation in RANKX is "find the position of this value".
The full syntax is:
RANKX ( <Table>, <Expression> [, <Value> [, <Order> [, <Ties>]]] )| Argument | Meaning | Default |
|---|---|---|
| Table | The items to rank against. Can be a table or a table expression. | Required |
| Expression | The value computed for each item. Usually a measure. | Required |
| Value | The value to look up in the ranking. | The expression evaluated in the current filter context |
| Order | `DESC` (largest value is rank 1) or `ASC` (smallest value is rank 1). | `DESC` |
| Ties | `Skip` or `Dense`. How ranks continue after a tie. | `Skip` |
The third argument is almost always left empty, so you will see two consecutive commas in most RANKX code. You only supply it when you want to rank a value other than the expression evaluated for the current item.
A basic ranking measure
Assume a model with a Sales fact table, a Product dimension, and a [Sales Amount] measure defined as SUMX ( Sales, Sales[Quantity] * Sales[Net Price] ).
Product Rank =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)Place Product[Product Name] in the rows of a table visual, add [Sales Amount] and [Product Rank], and each product gets a rank.
How the evaluation proceeds
Suppose the visual is evaluating the row for "Contoso Laptop".
- The filter context contains
Product[Product Name] = "Contoso Laptop". ALL ( Product[Product Name] )removes that filter on the column and returns every product name. This is the table to rank against.- RANKX iterates over those names. For each name it creates a row context, and the measure reference
[Sales Amount]is wrapped in an implicit CALCULATE. Context transition turns the current name into a filter, so[Sales Amount]returns the sales of that one product. - RANKX then evaluates
[Sales Amount]once more, in the original filter context, to get the value for "Contoso Laptop". - It counts how many of the values from step 3 are larger than that value, and adds one.
The diagram shows this sequence.
Step 3 is where the earlier chapter on context transition matters. If the second argument were a plain column expression rather than a measure, there would be no context transition. The expression would be evaluated in row context only, and every item could end up with the same value. Using a measure, or wrapping the expression in CALCULATE, is what makes the per-item evaluation work.
Choosing the ranking table
The first argument decides which items are compared. It is the most common source of wrong ranks.
ALL: rank against every item
ALL ( Product[Product Name] ) ignores all filters on the product name column, so every product is in the ranking table. A product's rank stays the same when a slicer hides other products. If "Contoso Laptop" is third in the full catalogue, it shows rank 3 even when the slicer leaves only five products visible. Ranks in the visible rows may then skip numbers, such as 1, 3, 4, 9, 12.
ALLSELECTED: rank against what the user selected
ALLSELECTED ( Product[Product Name] ) removes the filters that come from the visual itself, such as the row being evaluated, but keeps filters from slicers and from outside the visual. The ranking table is then the set of products the user left visible. Ranks run 1, 2, 3, ... across exactly the rows shown.
| Ranking table | Slicer hides some products | Typical use |
|---|---|---|
| `ALL ( Product[Product Name] )` | Rank is unchanged | Fixed leaderboard across the whole catalogue |
| `ALLSELECTED ( Product[Product Name] )` | Rank is recalculated among the visible products | Interactive reports where ranks should look consecutive |
| `VALUES ( Product[Product Name] )` | Only the current row is visible, so the rank is always 1 in a row of a visual | Rarely what you want |
The VALUES row in the table deserves attention. Inside a visual, the filter context for each row already restricts the product to one name. VALUES respects that filter, so the ranking table has a single item and every row gets rank 1. If you see a column of 1s, check whether the first argument still carries the row's filter.
Ranking a different level
The column you pass to ALL must match the level shown in the visual. If the visual shows Product[Category] but the rank measure uses ALL ( Product[Product Name] ), the rank compares each category's total against the sales of individual products. The numbers are valid, but they mean nothing useful. Rank the same column that sits in the rows.
Sort order and ties
Order
DESC gives rank 1 to the largest value. It suits sales, profit, and counts. ASC gives rank 1 to the smallest value, which suits rankings such as delivery time or defect rate. Because DESC is the default, you can omit it, but writing it makes the intent obvious to the next reader.
Ties
When two items have the same value, they get the same rank. The Ties argument controls the number given to the next item.
| Values (DESC) | Skip | Dense |
|---|---|---|
| 500 | 1 | 1 |
| 400 | 2 | 2 |
| 400 | 2 | 2 |
| 300 | 4 | 3 |
| 100 | 5 | 4 |
Skipcounts the items ahead of you, so after two items tied at rank 2, the next rank is 4. This is the default.Densenever leaves gaps. After the tie at rank 2, the next rank is 3.
Use Dense when you want to show "top 3" with tied items counted as one position, or when a gap in the sequence would confuse readers. Use Skip when the rank should mean "number of items that beat this one, plus one", as in most sports tables.
Ties are common when a measure returns blank or zero for many items. With DESC, every item with a blank value gets the same rank, below all the valued items.
Ranks in total rows
A rank only makes sense for one item. In the total row of a table visual, the filter context on Product[Product Name] is empty. The measure then evaluates the total sales and looks for it in the list of per-product sales. The total is larger than every product, so the rank shows 1. That number is misleading.
Guard the measure with HASONEVALUE, which returns TRUE only when exactly one value of the column is visible in the filter context.
Product Rank Selected =
IF (
HASONEVALUE ( Product[Product Name] ),
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)
)The IF has no else branch, so in the total row it returns blank, and Power BI shows an empty cell. This also protects you when someone uses the measure in a visual with no product on the axis.
A note on filters inside the ranking table
You can restrict the items being ranked. For example, to rank only products that have sales:
Product Rank With Sales =
IF (
HASONEVALUE ( Product[Product Name] ),
RANKX (
FILTER (
ALLSELECTED ( Product[Product Name] ),
NOT ISBLANK ( [Sales Amount] )
),
[Sales Amount],
,
DESC,
Dense
)
)If the current product has no sales, its own value is blank and it is not in the filtered table. RANKX still returns a result for it in some cases, so the HASONEVALUE guard alone is not enough when you need blank ranks for products without sales. Add a second condition to the IF, such as NOT ISBLANK ( [Sales Amount] ), if those rows must stay empty.
Common errors
- Every row shows rank 1. The first argument keeps the row filter. Use
ALLorALLSELECTEDon the column in the rows, notVALUES. - Every row shows the same rank, or a nonsense rank. The second argument is a bare column expression with no context transition. Use a measure, or wrap the expression in CALCULATE.
- The total row shows 1. Add the
HASONEVALUEguard. - Ranks have gaps after a slicer is used.
ALLignores the slicer. Switch toALLSELECTEDif gaps are unwanted. - Ranks do not change with the visual's filters on another table.
ALL ( Product[Product Name] )removes only filters on that column. Filters on other columns of the same table or on related tables stay active. UseALL ( Product )to remove every filter on the Product table, orALLSELECTEDif you want the visual's selections to count.
Performance
RANKX evaluates the expression once per item in the ranking table, and each evaluation involves a context transition. For a table of 100 products this is trivial. For a ranking table of several hundred thousand customers, the visual might need that many evaluations for every row displayed. Keep the ranking table as small as the question requires, and rank at a coarser level when you can.
Exercise
A table visual shows Product Name and Sales Amount. A slicer limits the report to the Bikes category. The rank measure uses ALL ( Product[Product Name] ) and shows 14, 15, 18, 22 for the visible products. The report owner wants ranks 1, 2, 3, 4 across the visible products. What should you change, and what extra protection should the measure have for the total row?
Show answer
Replace ALL ( Product[Product Name] ) with ALLSELECTED ( Product[Product Name] ) so the ranking table contains only the products left visible by the slicer. Wrap the RANKX call in IF ( HASONEVALUE ( Product[Product Name] ), ... ) so the total row returns blank instead of rank 1.
Exercise
Four items have sales of 900, 700, 700, and 500. Using DESC, what ranks does each item receive with Ties set to Skip and with Ties set to Dense?
Show answer
With Skip the ranks are 1, 2, 2, 4. With Dense the ranks are 1, 2, 2, 3.