Sub topic 9
COUNTX, MINX, and MAXX at a glance
This chapter assumes you have read the earlier chapters on SUMX and AVERAGEX. You should know how row context, filter context, and context transition work. COUNTX, MINX, and MAXX use the same pattern as those functions: a table, an expression evaluated once per row, and a final aggregation.
The shared pattern
Each function takes a table and an expression. It evaluates the expression for every row of the table, then combines the results.
| Function | Combines the per-row results by | Blank results |
|---|---|---|
| COUNTX | Counting them | Not counted |
| MINX | Taking the smallest | Ignored |
| MAXX | Taking the largest | Ignored |
The blank handling is the same as in AVERAGEX. A row whose expression returns blank does not take part in the result. If every row returns blank, the function returns blank.
COUNTX: counting non-blank results
COUNTX counts the rows where the expression is not blank. A value of 0 is not blank, so it is counted.
The measure below counts the customers who bought something in the current filter context. It reuses the [Sales Amount] measure from earlier in the chapter series.
Customers With Sales =
COUNTX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)VALUES returns the customers visible in the current filter context. For each one, context transition turns the [Sales Amount] reference into a calculation for that single customer. Customers with no sales return blank and are skipped.
COUNTX is only needed when the expression can return blank for some rows. To count the rows of a table, use COUNTROWS, which is clearer and usually faster.
Sales Lines = COUNTROWS ( Sales )MAXX and MINX: the extreme per-row value
MAXX and MINX return the largest or smallest result of the expression. The iterated rows can be fact-table rows or members of a dimension.
The first measure finds the biggest single sales line. The expression is evaluated in row context, so no measure is involved.
Largest Line Amount =
MAXX (
Sales,
Sales[Quantity] * Sales[Net Price]
)The next two measures find the best and worst day in the current selection. They call a measure inside the iterator, so context transition applies.
Best Day Sales =
MAXX (
VALUES ( 'Date'[Date] ),
[Sales Amount]
)
Worst Day Sales =
MINX (
VALUES ( 'Date'[Date] ),
[Sales Amount]
)MINX skips days with no sales. Worst Day Sales is therefore the lowest day that had sales, not 0. If you want days without sales to count as 0, change the expression to [Sales Amount] + 0. This also makes every date in the filter context count, so be sure that is what you want.
Common errors
- Using COUNTX where COUNTROWS is enough. If the expression never returns blank, both give the same answer. COUNTROWS is simpler to read.
- Expecting MINX to return 0 for empty days. Blank results are ignored, not treated as 0.
- Iterating over
ALLby accident.ALL ( 'Date'[Date] )ignores the current filters, so the best day is found across all dates. UseVALUESwhen the result should follow the report's filters. - Wrapping a column in MAXX when MAX will do.
MAXX ( Sales, Sales[Net Price] )returns the same value asMAX ( Sales[Net Price] ). The plain aggregator is the better choice.
Exercise
A report is filtered to one month. Which table expression should MAXX iterate over to find the best day in that month, and why does MAXX ( ALL ( 'Date'[Date] ), [Sales Amount] ) give a different answer?
Show answer
Use VALUES ( 'Date'[Date] ), which returns only the dates visible in the current filter context, so the result is the best day within the selected month. ALL ( 'Date'[Date] ) removes the filter and iterates over every date in the table, so it returns the best day across the whole date table.