Sub topic 3
How iterators work: table, expression, aggregation
This chapter assumes you can write simple measures with SUM, COUNT, and CALCULATE. It also assumes you know what a fact table, a dimension table, and a relationship are. It builds on the previous two chapters, which covered row context and filter context.
Aggregators and iterators
An aggregator such as SUM takes one column and combines its values. It cannot calculate anything first. If revenue is quantity times price and no revenue column exists, SUM ( Sales[Quantity] ) cannot help you.
An iterator takes a table and an expression. It visits each row of the table and evaluates the expression for that row. Then it combines the per-row results. The name ends in X: SUMX, AVERAGEX, COUNTX, MINX, MAXX, and RANKX.
The three parts
Every iterator has the same shape.
| Part | Where it appears | What it does |
|---|---|---|
| Table | First argument | Defines the rows to visit |
| Expression | Second argument | Is evaluated once per row, with a row context on that row |
| Aggregation | The function name | Combines the per-row results (sum, average, and so on) |
Here is the standard example:
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)Sales is the table. Sales[Quantity] * Sales[Net Price] is the expression. SUMX is the aggregation. For each row, DAX multiplies the two columns, and then it adds up all the products.
placeholderThe diagram below shows the same sequence.
How the two contexts combine
The table argument is evaluated in the current filter context. In a matrix cell for 2025, Sales contains only the 2025 rows. The iterator then opens a row context on each of those rows. It does not remove the filter context. The filter context decides which rows exist, and the row context decides which row the expression reads.
This is why a column reference such as Sales[Quantity] works inside the expression. A bare column reference needs a row context, and the iterator supplies one. Outside an iterator, in a measure, the same reference fails because no row context exists.
Same result, different path
An iterator can return exactly what an aggregator returns:
Quantity Sold =
SUMX (
Sales,
Sales[Quantity]
)This gives the same number as SUM ( Sales[Quantity] ). In fact, DAX treats SUM as shorthand for a SUMX over one column. Use the plain aggregator when the expression is a single column. Use an iterator when the expression needs a calculation per row.
Common errors
- Referencing a column from another table without RELATED. If the expression reads
Product[Unit Cost]while iteratingSales, you get an error or a wrong result. The next chapters coverRELATED. - Iterating the wrong grain. The expression runs once per row of the table. Iterating a table of orders instead of order lines changes what "per row" means, and the result changes with it.
- Expecting the iterator to ignore slicers. The table argument respects the filter context. To change which rows are visited, you must change the table expression, which a later chapter covers.
- Writing the average of a ratio when you want a ratio of sums.
AVERAGEX ( Sales, Sales[Margin %] )weights every row equally. Compute the numerator and denominator separately if you need a weighted figure.
Exercise
A model has a Sales table with columns Quantity and Net Price, and no revenue column. Write a measure named Revenue that returns total revenue, and say which part of your formula is the table, the expression, and the aggregation.
Show answer
Revenue = SUMX ( Sales, Sales[Quantity] Sales[Net Price] ). The table is Sales, the expression is Sales[Quantity] Sales[Net Price], and the aggregation is the sum performed by SUMX. In a cell filtered to one year, Sales contains only that year's rows, so only those rows are visited.