Sub topic 13
Ranks in total rows: guarding with HASONEVALUE
This chapter assumes you can write a basic RANKX measure and know the difference between row context and filter context. If you need a refresher on RANKX, see the earlier chapters on RANKX syntax and on choosing between ALL and ALLSELECTED.
Why the total row shows a rank
Put a product rank measure in a table visual with one row per product. The total row at the bottom often shows a rank too, usually 1. That number is misleading, because a total is not a product.
The cause is how filter context works in the total row. Each product row filters Product[Product Name] to one product. The total row applies no filter on product name, so every product is visible. RANKX still does its job:
- It evaluates
[Sales Amount]in the current context. In the total row, this is the sum across all products. - It evaluates
[Sales Amount]for each product in the ranking table. - It finds where the first value falls among the second set.
The grand total is larger than any single product, so it lands in first place. Nothing is wrong with the formula. It is answering a question nobody asked.
Product Rank Unguarded =
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)The guard: HASONEVALUE
HASONEVALUE ( column ) returns TRUE when the filter context leaves exactly one distinct value in that column. In a product row it is TRUE. In the total row, many products are visible, so it is FALSE.
Wrap RANKX in an IF that tests it:
Product Rank Selected =
IF (
HASONEVALUE ( Product[Product Name] ),
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)
)IF has no third argument here, so it returns BLANK when the test fails. The total row shows an empty cell, which is what you want.
What the guard does and does not cover
The guard tests the filter context on the column you name. Keep these points in mind:
| Situation | HASONEVALUE result | Rank shown |
|---|---|---|
| Row for one product | TRUE | The product's rank |
| Total row, many products visible | FALSE | Blank |
| Slicer limits the report to one product | TRUE, even in the total row | 1 |
| Visual groups by Category, not Product Name | FALSE in every row | Blank everywhere |
The last row is a common mistake. If the visual does not put the guarded column on the rows, the guard never passes. Make sure the column in HASONEVALUE and in the RANKX table is the one the visual shows.
Checking the result
Add Product Rank Unguarded and Product Rank Selected to the same table next to [Sales Amount]. The product rows match. Only the total row differs: the first shows 1, the second is blank. Seeing both side by side is the quickest way to confirm the guard is in place.
Exercise
A matrix shows Category on rows. You add the guarded measure Product Rank Selected, which tests HASONEVALUE on Product[Product Name]. Every cell is blank. What is the most likely cause, and how would you fix it?
Show answer
Each category row contains many products, so HASONEVALUE on Product Name is FALSE in every row and IF returns BLANK. The guard is testing a column the visual does not show. Either put Product Name on the rows, or write a category-level rank that guards on Product[Category] and ranks over ALLSELECTED ( Product[Category] ).