Sub topic 11
Which items to rank against: ALL vs ALLSELECTED
This chapter assumes you can write basic measures with SUM and CALCULATE, and that you know what row context, filter context, and context transition are from earlier chapters. You should also be comfortable with a star schema (a fact table Sales linked to dimension tables such as Product and Date).
The previous chapter showed how RANKX is built. This chapter covers one decision: the table you pass as the first argument. That argument decides which items count as competitors. ALL and ALLSELECTED give different rankings from the same measure, and picking the wrong one is the most common RANKX mistake.
What the first argument controls
RANKX has this signature:
RANKX ( <Table>, <Expression> [, <Value> [, <Order> [, <Ties>]]] )It works in three steps:
- It builds the table in the first argument. Each row is one competitor.
- It evaluates the expression once per competitor, using context transition (each row's column values become filters). This produces a list of scores.
- It evaluates the expression (or the
Valueargument) for the current cell of the report. It then returns that value's position in the list of scores.
The first argument therefore defines the population you rank against. The filter context of the cell only decides which score is being placed in that list. The table you pass decides which scores it is compared with.
The two shapes of the answer
Suppose a matrix lists products as rows. A slicer on Product[Category] limits the report to one category, and another slicer on 'Date'[Year] limits it to one year. Two reasonable questions are:
- "Where does this product stand among all products in the model?" This is an absolute rank.
- "Where does this product stand among the products I am looking at?" This is a relative rank. The ranks usually run 1, 2, 3 and so on down the visible list.
ALL answers the first question. ALLSELECTED answers the second.
ALL: rank against everything
Start with the base measure used throughout this tutorial:
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)Then rank products with ALL:
Product Rank =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)ALL ( Product[Product Name] ) returns every distinct product name and ignores any filter on that column. A slicer or visual filter on Product Name cannot shrink the population. If you filter the visual to products 5, 9, and 14 of 200, they might show ranks 5, 9, and 14. They keep the position they hold among all 200.
ALL on one column only clears filters on that column. Filters on other columns still apply when each competitor's score is calculated. With a Category slicer set to Audio, a Cameras product is still a row in the ranking table, but its [Sales Amount] evaluates to blank. The Audio and Cameras filters have no rows in common. A blank score does not outrank any real value. So the Audio products are in effect ranked among Audio products, even though you used ALL.
If you want a rank across the whole catalogue whatever the slicers say, clear the other filters as well, for example with ALL ( Product ). Whether that is what you want depends on the report.
ALLSELECTED: rank against what the user selected
The ALLSELECTED version looks almost the same:
Product Rank Selected =
IF (
HASONEVALUE ( Product[Product Name] ),
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)
)ALLSELECTED removes the filters that come from the visual itself, such as the rows of the matrix. It keeps the filters that come from outside the visual: slicers, page and report filters, and filters on the visual. The result is the set of products the user has narrowed down to, whichever products appear on a given row.
Filtering the visual to products 5, 9, and 14 with a slicer on Product Name makes the ranking table contain only those three products. They now show ranks 1, 2, and 3. That is usually what a user expects from "rank within my selection".
The IF ( HASONEVALUE ( ... ) ) guard keeps the total row from showing a rank. Without it, the total row evaluates in a context with many products and returns a meaningless value. A later chapter covers this guard in detail. It is included here only so the example is safe to paste into a report.
How the filters flow
The diagram shows what each function keeps when a cell is evaluated.
A side-by-side comparison
Here is how the two measures behave in a few common situations.
| Situation | `ALL` ranking table | `ALLSELECTED` ranking table |
|---|---|---|
| No slicers, one product per row | All products | All products |
| Slicer on `Product Name` picks 3 products | All products, ranks stay absolute | The 3 products, ranks run 1 to 3 |
| Slicer on `Category` | All names, but products outside the category score blank | Names visible after the category filter |
| Slicer on `'Date'[Year]` | All products, scores recalculated for the chosen year | All products, scores recalculated for the chosen year |
| Top N filter on the visual | All products, ranks stay absolute | Depends on how the filter is applied. Test it |
The Date row is easy to misread. A year slicer does not change which products compete. It changes the scores they get. Both measures react to it identically, because the date filter is not on the column that the ranking table covers.
The last row carries a warning. A Top N filter on a visual is applied after the measure is calculated, and it is not always visible to ALLSELECTED in the way a slicer is. Check the result in your own report before relying on it.
Matching the ranking table to the visual
The column in the first argument must be the same column that defines the rows of the visual. Three mismatches cause most wrong ranks:
- Rows are products but the table is brands. The ranking table holds only a few brand names. Each product row is scored with context transition on
Brand, so every product in a brand gets the brand's score, not its own. Ranks look plausible but are wrong. - Rows are product names but the data has two products with the same name.
ALL ( Product[Product Name] )has one row per distinct name, which merges those products. UseProduct[ProductKey]as the rank column and show the name in the visual, or rank overALL ( Product ). - The rank column is in a different table from the rows. The competitors' scores come from filters on a column that the visual does not filter. Every row then gets the same score and the same rank.
A good habit is to put the same column in the matrix and in the ALL or ALLSELECTED call.
Seeing both ranks together
Putting both measures in one table is the quickest way to see the difference. This measure shows the gap:
Rank Gap =
VAR AbsoluteRank =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)
VAR SelectedRank =
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)
RETURN
IF (
HASONEVALUE ( Product[Product Name] ),
AbsoluteRank - SelectedRank
)With no slicers the gap is zero for every product. Select a few products with a slicer and the gap shows how many places each one moves.
Choosing between them
Use ALL when the rank is a fixed property of the product, for example "best seller of the year across the catalogue". Use ALLSELECTED when the rank is part of an interactive analysis and the user expects it to follow the slicers. Keep in mind that neither removes filters on columns outside the one you name, and the scores always respond to those filters.
Exercise
A matrix lists products by Product[Product Name]. A slicer on Product[Product Name] has three products selected: the 10th, 40th, and 75th best sellers overall. The matrix shows Product Rank (using ALL ( Product[Product Name] )) and Product Rank Selected (using ALLSELECTED ( Product[Product Name] )). What does each measure show for the three products, and why?
Show answer
Product Rank shows 10, 40, and 75. ALL removes the slicer's filter on Product Name, so each product is placed among every product name. Product Rank Selected shows 1, 2, and 3. ALLSELECTED keeps the slicer's filter and removes only the matrix's row filter, so the ranking table holds just the three selected products.
Exercise
A report has a slicer on Product[Category] set to Audio and a matrix of products. The measure ranks over ALL ( Product[Product Name] ). Why do the Audio products still show ranks that run 1, 2, 3 and so on, even though ALL was used?
Show answer
ALL ( Product[Product Name] ) clears only the filter on the Product Name column. The Category filter stays in place. Products from other categories are still in the ranking table, but their [Sales Amount] evaluates to blank because their category is excluded. Blank scores do not outrank real values, so the Audio products are effectively ranked among themselves.