Sub topic 6
Iterators: SUMX, AVERAGEX, and FILTER
This chapter assumes you know the difference between a measure and a calculated column, can write simple aggregations such as SUM and COUNTROWS, and have read the earlier chapters on filter context and row context.
What an iterator does
An iterator is a DAX function that takes a table and an expression. It visits each row of that table, evaluates the expression with that row as the current row, and combines the results. While it visits a row, DAX holds a row context for that row, so you can read the row's columns directly.
The iterator's table argument is evaluated in the current filter context. The filter context therefore decides which rows are visited, and the row context decides which row the expression sees.
| Function | Per-row expression returns | Combined by |
|---|---|---|
| SUMX | a number | sum |
| AVERAGEX | a number | average |
| FILTER | true or false | keeps rows where true |
SUMX: sum of a per-row expression
SUM adds up one column. When you need a calculation per row first, such as quantity times price, use SUMX.
Sales Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )SUMX creates a row context over Sales and adds up the per-row product. If a slicer selects the year 2025, the filter context limits Sales to 2025 rows, and only those rows are iterated.
AVERAGEX: average of a per-row expression
AVERAGEX works like SUMX but averages the per-row results. It is useful when the thing you want to average does not exist as a column. Averaging over customers is a typical case.
Average Sales per Customer =
AVERAGEX ( Customer, [Sales Amount] )Here the expression is a measure reference. A measure reference inside a row context triggers context transition, so each Customer row becomes a filter on Sales. Each customer's sales are computed separately, then averaged. Later chapters explain this mechanism. For now, notice that the result is an average per customer, not per sales line.
FILTER: an iterator that returns a table
FILTER also iterates, but it returns a table instead of a single value. It keeps the rows for which the condition is true.
Products Over 1000 =
COUNTROWS (
FILTER ( Product, [Sales Amount] > 1000 )
)FILTER walks through each Product row. Context transition makes [Sales Amount] evaluate for that product alone. Products over the threshold stay in the table, and COUNTROWS counts them.
Products Over 1000 Check =
COUNTROWS (
FILTER ( Product, [Sales Amount] > 1000 )
)FILTER is most often used as a filter argument inside CALCULATE, where its output table becomes the new filter. You will use it that way in the CALCULATE chapter.
Common errors
- Using SUM where a per-row calculation is needed.
SUM ( Sales[Quantity] * Sales[Unit Price] )is invalid, because SUM accepts only a column. Use SUMX. - Expecting a row context to filter other tables. A row context does not filter related tables by itself. Inside an iterator over Product,
SUMX ( Sales, ... )still sees every visible Sales row unless context transition applies. - Iterating a large table when a column filter would do.
FILTER ( Sales, Sales[Quantity] > 5 )iterates every Sales row. A simple column predicate in CALCULATE is usually faster and easier to read. - Confusing the iterated table with the result. The table you pass to SUMX sets which rows are visited. If you pass
Product, the expression runs once per product, even though the data lives in Sales.
Sales Per Product Row =
SUMX ( Product, [Sales Amount] )This measure visits each visible product, computes its own sales through context transition, and adds the values. Compare the choice of table with AVERAGEX ( Customer, [Sales Amount] ): the table you iterate defines the grain of the calculation.
Exercise
A report shows only the year 2025. You write Total = SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ). Which Sales rows does SUMX visit, and why can the expression read Sales[Quantity] directly?
Show answer
SUMX visits only the Sales rows that are visible in the current filter context, which here are the 2025 rows. For each of those rows SUMX creates a row context, and that row context lets the expression read the columns of the current row directly.
Exercise
What is the difference between SUMX and FILTER in what they return, and how would you count the customers whose [Sales Amount] exceeds 500?
Show answer
SUMX returns a single number, the sum of the per-row results. FILTER returns a table containing the rows where its condition is true. To count the customers, write COUNTROWS ( FILTER ( Customer, [Sales Amount] > 500 ) ).