Sub topic 11
Which items to rank against: ALL vs ALLSELECTED
This chapter assumes you can write RANKX basics, know the difference between row context and filter context, and understand that a measure called inside an iterator is evaluated per row through context transition. Here you choose the table that RANKX ranks against.
The ranking table defines the field
RANKX takes a table as its first argument. For every row of the report, it evaluates your measure for each member of that table, then finds where the current row's value falls among those results. The table you pass is the set of competitors.
That table is itself evaluated in the current filter context. If you pass VALUES ( Product[Product Name] ), the competitors are only the products visible in the current cell. In a row that shows one product, that is a single product, so every row ranks 1. This is the most common RANKX error.
To rank against more than the current cell, you must widen the filters on the ranking table. ALL and ALLSELECTED do this in different ways.
ALL: rank against everything
ALL ( Product[Product Name] ) removes every filter from that column. Each product is ranked against the whole catalogue, whatever slicers or filters are active.
Product Rank All =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)Other filters still apply to the measure, such as a year slicer. The ranking compares each product's sales in the selected year, but it compares all products, including ones a slicer has hidden from the visual. If a Category slicer shows only Bikes, a bike can still be ranked 14 because accessories and clothing outrank it.
ALLSELECTED: rank against what the user sees
ALLSELECTED ( Product[Product Name] ) removes the filters from the visual itself, such as row headers, but keeps filters from slicers and outer filters. The competitors are the products the user has left selected.
Product Rank Selected =
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)With the Category slicer on Bikes, the ranks now run from 1 to the number of bikes. Change the slicer and the ranks recalculate for the new group.
Choosing between them
| Question | Use |
|---|---|
| Should a product keep the same rank when a slicer changes? | ALL |
| Should ranks run 1 to N over the visible items? | ALLSELECTED |
| Is the rank a fixed fact, such as an all-time ranking? | ALL |
| Is the rank for browsing and comparing a chosen group? | ALLSELECTED |
Common errors
- Using
VALUESas the ranking table. Each row then ranks only itself. - Ranking at the wrong grain. If the visual shows Product Name but you pass
ALL ( Product ), the competitors are whole product rows, and the ranks may not match the rows shown. Use the column that matches the visual. - Expecting ALLSELECTED to ignore row-level filters from the visual. It removes them on purpose, which is why it works in a table with one row per product.
- Forgetting total rows. Both versions return a rank in the total row, which makes little sense. The next chapter guards against it with HASONEVALUE.
Exercise
A matrix lists products by name. A Color slicer is set to Red, showing 8 products. Your measure uses RANKX ( ALL ( Product[Product Name] ), [Sales Amount] ), and the lowest-selling red product shows rank 41. Why, and which change makes the ranks run from 1 to 8?
Show answer
ALL removes the Color filter from the ranking table, so each red product is ranked against all products, and 40 products outsell the lowest red one. Replacing ALL with ALLSELECTED keeps the slicer filter, so only the 8 red products compete and the ranks run from 1 to 8.