Sub topic 13
Ranks in total rows: guarding with HASONEVALUE
This chapter assumes you can write RANKX measures with ALL and ALLSELECTED, and that you know the difference between row context and filter context. The measures below use [Sales Amount], the SUMX measure defined earlier in this tutorial. They also use a Product[Product Name] column in a Product dimension table.
The problem: a rank in a place where no single item exists
A RANKX measure answers the question "where does the current item stand among all items?" That question only makes sense when the filter context holds one item. In a table visual, each product row has one product in context. The total row has none. It has every visible product at once.
Here is the unguarded measure from the previous chapter:
Product Rank =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)RANKX does not know it is in a total row. It evaluates [Sales Amount] in the current filter context, then looks for that value among the per-product values of the ranking table. In the total row, the value is the grand total. That total is larger than any single product's sales, so it lands at the top of the list.
| Row | Sales Amount | Product Rank |
|---|---|---|
| Product A | 500 | 1 |
| Product B | 300 | 2 |
| Product C | 200 | 3 |
| Total | 1,000 | 1 |
The total row shows rank 1 even though the total is not a product. The result is not an error, so nothing warns you. It is a wrong number that looks plausible.
The fix: test for a single product first
HASONEVALUE takes a column and returns TRUE when the filter context leaves exactly one distinct value in that column. It returns FALSE for zero values or for two or more. Wrapping RANKX in an IF with no else branch returns BLANK whenever the test fails:
Product Rank Selected =
IF (
HASONEVALUE ( Product[Product Name] ),
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)
)The total row now shows nothing, and each product row is unchanged. The same guard also helps in a card visual or a chart with no product on an axis, where there is no product to rank either.
The guard is a test on the filter context. It does not change how RANKX ranks. The ranking table (ALLSELECTED here) still decides which products are compared. The guard only decides whether the question is valid.
Test the same column you rank
The column in HASONEVALUE should match the column in the ranking table. Consider this version:
Product Rank Wrong Guard =
IF (
HASONEVALUE ( Product[Category] ),
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)
)In a visual that lists products under a category slicer set to one category, HASONEVALUE ( Product[Category] ) is TRUE in every row, including the total row. The guard passes and the total row is ranked again. The guard has to ask the question RANKX depends on: is exactly one product in context?
Hierarchies and subtotals
HASONEVALUE looks at the filter context, not at the position in the visual. This matters in a matrix with more than one field on the rows, such as Category and then Product Name.
- At a product row, one product is in context, so HASONEVALUE is TRUE.
- At a category subtotal, several products are usually in context, so it is FALSE and the rank is blank.
- At a category subtotal where the category contains only one product, one product is in context. HASONEVALUE is TRUE, and the subtotal shows that product's rank.
The last case is not wrong, because the number is the rank of that single product. It can still confuse readers, since a subtotal row now shows a rank while other subtotal rows are blank.
ISINSCOPE avoids this. It returns TRUE only when the column is the level currently being grouped by in the visual:
Product Rank In Scope =
IF (
ISINSCOPE ( Product[Product Name] ),
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)
)Subtotal rows and the grand total are never "in scope" for the product column, so they stay blank regardless of how many products they contain.
| Test | TRUE when | Typical use |
|---|---|---|
| `HASONEVALUE ( col )` | Exactly one distinct value of `col` is in the filter context | Flat tables, cards, general use |
| `ISINSCOPE ( col )` | The visual is grouping by `col` at the current row | Matrices with hierarchies, where subtotals must stay blank |
| `COUNTROWS ( VALUES ( col ) ) = 1` | Same as HASONEVALUE, written out | Reading older code |
| `NOT ISBLANK ( SELECTEDVALUE ( col ) )` | One value is in context and it is not blank | When you also need the value itself |
Comparing a blank rank: a second trap
Once the total row returns BLANK, you may build measures on top of the rank, such as "show sales for the top 3 products only". This is where blank behaves unexpectedly. In DAX numeric comparisons, BLANK is treated as 0, so BLANK () <= 3 is TRUE.
Top 3 Sales Wrong =
IF (
[Product Rank Selected] <= 3,
[Sales Amount]
)In the total row, the rank is blank, the comparison is TRUE, and the measure returns the grand total. That value is misleading, because the total of the top 3 products is not the grand total. Check for blank explicitly:
Top 3 Sales =
VAR ProductRank = [Product Rank Selected]
RETURN
IF (
NOT ISBLANK ( ProductRank ) && ProductRank <= 3,
[Sales Amount]
)With this version, the total row shows BLANK as well. If you want a total for the top 3 products, that needs its own calculation, for example a SUMX over a TOPN table, and is a different problem from guarding a rank.
Slicers, filters, and what the guard does not fix
The guard handles total rows. It does not change which products are ranked, and several related mistakes need other fixes:
- Ranking table ignores slicers.
ALL ( Product[Product Name] )ranks against every product, so a slicer that hides some products leaves gaps in the visible ranks. UseALLSELECTEDwhen the rank should be relative to what the user sees. - Filters inside the visual.
ALLSELECTEDkeeps filters that come from slicers and from the visual's own filter pane, but it still removes the row-level filtering that creates each row. Test a visual with a Top N filter on the product column to confirm that the ranks are what you expect. - Ties. Two products with equal sales get equal ranks.
Skipleaves a gap after them (1, 2, 2, 4), andDensedoes not (1, 2, 2, 3). The guard has no effect on this choice. - Filtering a different column. If a visual puts
Product[Brand]on rows and your measure ranksProduct[Product Name], each brand row usually contains many products. HASONEVALUE returns FALSE and the rank is blank. This is correct behavior: there is no single product to rank. Rank by brand instead, usingALLSELECTED ( Product[Brand] )and a guard onProduct[Brand].
Checking your work
A reliable way to test a rank measure is to build one table visual with the product name, [Sales Amount], and the rank, and then check four things:
- The total row is blank.
- Ranks run in the order of the sales values.
- Applying a slicer changes ranks in the way you chose with ALL or ALLSELECTED.
- Ties show the Skip or Dense numbering you chose.
If the total row is blank but a card visual with no filters shows a number, the measure is probably relying on a filter that exists only in the table. Check the guard column again.
Exercise
A matrix has Category and then Product Name on its rows. Your measure is guarded with HASONEVALUE ( Product[Product Name] ). The category "Accessories" contains only one product, and its subtotal row shows a rank while all other subtotal rows are blank. Why does this happen, and which function would keep every subtotal blank?
Show answer
In the Accessories subtotal, the filter context contains exactly one product, so HASONEVALUE returns TRUE and RANKX runs. The other categories contain several products, so their guards return FALSE. ISINSCOPE ( Product[Product Name] ) fixes this, because it is TRUE only at the row level where the matrix groups by product name. Subtotal rows are never in scope for that column, whatever number of products they contain.
Exercise
Your measure is IF ( [Product Rank Selected] <= 3, [Sales Amount] ). The rank is guarded and blank in the total row. Why does the total row still show a value, and how do you fix it?
Show answer
DAX treats BLANK as 0 in numeric comparisons, so BLANK () <= 3 is TRUE and the IF returns [Sales Amount] for the total row. Store the rank in a variable and test it explicitly, for example IF ( NOT ISBLANK ( ProductRank ) && ProductRank <= 3, [Sales Amount] ).