Sub topic 9
COUNTX, MINX, and MAXX at a glance
This chapter assumes you have read the earlier chapters on row context, filter context, and the iterator pattern. It also assumes you can write a measure with SUMX or AVERAGEX and know that a measure reference inside an iterator triggers context transition. If either idea is unclear, review the SUMX and AVERAGEX chapters first. Everything here applies them to three more functions.
The same pattern, a different last step
SUMX, AVERAGEX, COUNTX, MINX, and MAXX all take a table and an expression. They evaluate the expression once per row of the table, then combine the results. Only the final step differs.
| Function | What it returns | What it does with blank results |
|---|---|---|
| `COUNTX` | The number of rows where the expression is not blank | Skips them |
| `MINX` | The smallest value of the expression | Ignores them |
| `MAXX` | The largest value of the expression | Ignores them |
If the table has no rows, or every result is blank, all three return blank. COUNTX returns blank rather than 0 in that case, which matters when you chart the result or compare it to a number.
COUNTX: counting rows where an expression has a value
COUNTX is not the same as COUNTROWS. COUNTROWS counts the rows of a table. COUNTX counts only the rows for which the expression returns something other than blank. That difference is what makes it useful.
A common use is counting how many members of a dimension have a result in the current filter context:
Customers With Sales =
COUNTX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)For each customer key visible in the current filter context, COUNTX evaluates [Sales Amount]. Context transition turns the current customer row into a filter, so the measure returns that customer's sales. A customer with no sales rows gets a blank, and COUNTX skips it.
Note the word blank. If the expression returns 0, the row is counted. A measure written as SUM ( Sales[Amount] ) + 0 never returns blank, so COUNTX over it counts every row. When a count comes out too high, check whether the expression can return 0 where you expected blank.
You can also count rows by a per-row condition. This measure counts sales lines worth more than 1,000:
Large Order Lines =
COUNTX (
Sales,
IF ( Sales[Quantity] * Sales[Net Price] > 1000, 1 )
)The IF has no else branch, so it returns blank when the test fails, and COUNTX skips those rows. In practice you would often write this as COUNTROWS ( FILTER ( Sales, ... ) ), which expresses the same idea more directly. Both are correct. Use whichever reads better to your team.
COUNTX and non-numeric values
COUNTX is documented to count rows whose expression evaluates to a number, and it does not support logical values. If you need to count non-blank text or Boolean results, use COUNTAX. It works the same way but counts any non-blank value. For counting distinct values, DISTINCTCOUNT is the better tool.
MAXX and MINX: the extreme of a per-member result
MAXX and MINX answer questions that a plain MAX or MIN of a column cannot. MAX ( Sales[Net Price] ) finds the largest value stored in a column. It cannot find the best day, because "sales per day" is not stored anywhere. It is a calculation. With an iterator you build the per-day value and then take its extreme:
Best Day Sales =
MAXX (
VALUES ( 'Date'[Date] ),
[Sales Amount]
)Weakest Day Sales =
MINX (
VALUES ( 'Date'[Date] ),
[Sales Amount]
)Both use the same shape as the Avg Daily Sales measure from the AVERAGEX chapter. Only the aggregation changed.
You can also use them on a fact table to find the largest or smallest per-row calculation:
Largest Order Line =
MAXX (
Sales,
Sales[Quantity] * Sales[Net Price]
)Totals are not sums of the rows above
A pivot table with months on rows shows the best day within each month. The grand total row shows the best day across all visible dates. The total is the maximum of the whole set, not a sum or a maximum of the monthly figures. In this case the two views agree, because the maximum of the monthly maxima equals the overall maximum. For AVERAGEX and MINX the same reasoning holds: the total row recalculates the measure in its own filter context.
Blanks make MINX misleading
MINX ignores blank results. If some days have no sales, Weakest Day Sales skips them and reports the weakest day that had sales. That is often not what a reader assumes. To treat blank days as zero, force the expression to return a number:
Weakest Day Sales Including Zero =
MINX (
VALUES ( 'Date'[Date] ),
[Sales Amount] + 0
)Adding 0 converts a blank to 0, so those days now take part in the comparison. Whether you want this depends on the question. Decide on purpose, and name the measure so readers can tell which version they are looking at.
Choosing the table argument
The table argument works the same way as in the earlier chapters. These three variations of one measure show how it changes the meaning:
Best Product Sales =
MAXX (
VALUES ( Product[Product Name] ),
[Sales Amount]
)Best Product Sales Overall =
MAXX (
ALL ( Product[Product Name] ),
[Sales Amount]
)Best Product Sales In Selection =
MAXX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount]
)VALUESvisits the products in the current filter context. In a row for one category, it visits only that category's products.ALLignores the filters on product name. It always finds the best product in the whole model, whatever the row or slicer.ALLSELECTEDrespects slicers and outer filters but ignores the row's own filter. It finds the best product among those the user selected.
The last form is useful for "percentage of the best performer" measures:
Sales vs Best Product =
DIVIDE (
[Sales Amount],
MAXX (
ALLSELECTED ( Product[Product Name] ),
[Sales Amount]
)
)For each product row, the numerator is that product's sales and the denominator is the best product's sales among those selected. The best product therefore shows 100%. DIVIDE returns blank if the denominator is zero or blank.
Performance: when a plain function is enough
Each of these iterators runs its expression once per row. When the expression is a single column and you do not need per-row logic, a column aggregation does the same job faster:
| Instead of | Use |
|---|---|
| `COUNTX ( Sales, Sales[Quantity] )` | `COUNT ( Sales[Quantity] )` |
| `MAXX ( Sales, Sales[Net Price] )` | `MAX ( Sales[Net Price] )` |
| `MINX ( Sales, Sales[Net Price] )` | `MIN ( Sales[Net Price] )` |
The iterator earns its cost when the expression is a calculation or a measure. Be most careful when you iterate over a large fact table and the expression calls a measure. Each row then triggers a context transition, and the work grows with the row count. Iterating over a small dimension, such as dates or product names, is usually cheap.
Choosing between them
- Need the number of members that have a result? Use
COUNTXover aVALUEStable with a measure. - Need the best or worst per-member result? Use
MAXXorMINXover aVALUES,ALL, orALLSELECTEDtable. - Need the extreme of a stored column? Use
MAXorMIN. - Need the number of rows in a table? Use
COUNTROWS.
Exercise
A report shows Customers With Sales = COUNTX ( VALUES ( Customer[CustomerKey] ), [Sales Amount] ). A colleague changes [Sales Amount] to a version that ends in + 0, and the count now equals the total number of visible customers. Why?
Show answer
Adding 0 turns a blank result into 0. COUNTX skips only blank results, so customers with no sales now return 0 and are counted. To count only customers who have sales, keep the expression blank for them, or test for a nonzero result, for example IF ( [Sales Amount] <> 0, 1 ).
Exercise
Write a measure that shows each product's sales as a share of the best-selling product among those chosen in a slicer. Which table function must you use inside MAXX, and why not VALUES?
Show answer
Use DIVIDE ( [Sales Amount], MAXX ( ALLSELECTED ( Product[Product Name] ), [Sales Amount] ) ). ALLSELECTED keeps the slicer filters but removes the row's own filter, so the denominator is the same for every row. VALUES would keep the row's own product, so each row would be divided by itself and show 100%.