Sub topic 3
COUNTX, MINX, and MAXX at a Glance
This chapter assumes you know tables, relationships, filter context, and basic measures such as SUM and CALCULATE.
Other Iterators
COUNTX, MINX, and MAXX evaluate an expression for every row in a table, then combine the results.
COUNTXcounts nonblank results.MINXreturns the smallest result.MAXXreturns the largest result.
Line Count = COUNTX ( Sales, Sales[Quantity] )
Lowest Line Value =
MINX (
Sales,
Sales[Quantity] * Sales[Net Price]
)
Highest Line Value =
MAXX (
Sales,
Sales[Quantity] * Sales[Net Price]
)The table argument controls which rows are visited. For example, FILTER ( Sales, Sales[Quantity] > 0 ) restricts the iterator to positive-quantity rows.
RANKX and Its Ranking Table
RANKX evaluates an expression for every item in a table and ranks the current item among those results.
Product Rank =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)DESC gives rank 1 to the largest value. The ALL expression removes the current product filter, so every product is considered.
ALL Versus ALLSELECTED
Use ALL when the ranking should compare against the complete product list. Use ALLSELECTED when slicers should limit the comparison set.
Product Rank Selected =
RANKX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Skip
)For example, a category slicer can make ALLSELECTED rank only products in the selected category.
Sort Order and Ties
The final argument controls ties.
| Method | Values | Ranks |
|---|---|---|
| `Skip` | 100, 80, 80, 50 | 1, 2, 2, 4 |
| `Dense` | 100, 80, 80, 50 | 1, 2, 2, 3 |
Use Dense when rank numbers should not have gaps.
Total Rows
A total row has no single product in context. Guard the measure so it does not display a misleading rank.
Product Rank Display =
IF (
HASONEVALUE ( Product[Product Name] ),
[Product Rank]
)Iterator Performance
Iterators can be slower when they scan a large fact table and perform complex calculations for every row. Prefer a simple column aggregation when it produces the same result, such as SUM ( Sales[Sales Amount] ). Iterate over a smaller table, filter early, and avoid repeating expensive measure calculations unnecessarily.
Exercise
Which function ranks products only against products left visible by slicers, and which function prevents a rank from appearing on a total row?
Show answer
Use ALLSELECTED for the ranking table and HASONEVALUE to guard the result.