Sub topic 6
Choosing the table to iterate over
Every iterator takes a table as its first argument, and that table decides which rows the expression visits. The same expression over a different table can give a different number. This chapter covers the four common choices (a table, FILTER, VALUES, and ALL) and how to pick one.
This chapter assumes you know the following:
- Tables, columns, and relationships in a star schema.
- Basic measures with
SUM,COUNT, andCALCULATE. - The idea of row context and filter context from the earlier chapters.
The table argument sets the grain
An iterator evaluates its expression once per row of the table argument, then combines the results. The grain of that table (what one row represents) therefore determines what "per row" means.
Take Sales, where one row is one order line:
Avg Line Amount =
AVERAGEX (
Sales,
Sales[Quantity] * Sales[Net Price]
)This returns the average value of an order line. Now iterate over customers instead:
Avg Sales per Customer =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)This returns the average of each customer's total. Both are valid averages, but they answer different questions. Decide which question you are asking, then choose the table whose rows match it.
Option 1: a table from the model
Passing a table name iterates every row of that table that is visible in the current filter context. A slicer on Product[Category] filters Sales through the relationship, so the iterator only sees matching sales rows.
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)Use a model table when the calculation belongs at the grain of that table, and when columns from related tables can be reached with RELATED. Fact tables are the usual choice.
Option 2: FILTER to restrict the rows
FILTER takes a table and a condition and returns only the rows where the condition is true. Wrap it around the table argument to restrict the iteration.
Large Line Sales =
SUMX (
FILTER ( Sales, Sales[Quantity] >= 5 ),
Sales[Quantity] * Sales[Net Price]
)FILTER is itself an iterator. It evaluates the condition for each row of Sales, and then SUMX iterates the surviving rows. The filter respects the current filter context, because Sales is the table as filtered by slicers and visuals.
FILTER can also test a measure. A measure reference inside the condition triggers context transition, so the measure is evaluated for each row of the table being filtered:
Sales of Big Categories =
SUMX (
FILTER (
ALL ( Product[Category] ),
[Sales Amount] > 1000000
),
[Sales Amount]
)Here ALL returns every category, FILTER keeps those whose total exceeds one million, and SUMX adds their totals. Because of ALL, a slicer on category does not change which categories are tested. The rest of the filter context (for example, a year slicer) still applies to [Sales Amount].
Option 3: VALUES for the members in the current context
VALUES ( Table[Column] ) returns the distinct values of one column that are visible in the current filter context. It is the standard way to say "iterate over each customer / product / month that is currently in scope."
Avg Daily Sales =
AVERAGEX (
VALUES ( 'Date'[Date] ),
[Sales Amount]
)In a visual with one row per month, each cell iterates only the dates in that month. The measure [Sales Amount] is evaluated for each date through context transition. AVERAGEX ignores dates where the measure returns blank, so days with no sales do not pull the average down.
Two details matter here:
- Blank row. If a fact row has no matching dimension row,
VALUESincludes an extra blank member. Check your relationships if totals look off by an unexplained blank. - Dimension filters do not flow upward. A filter on
DatereachesSales, but it does not reduce the rows ofCustomer. InAvg Sales per Customer, a date slicer leaves all customers inVALUES ( Customer[CustomerKey] ). Customers without sales in that period return blank and are skipped. The result is still correct, but the iterator visits more rows than it needs to.
Option 4: ALL to ignore the current filters
ALL ( Table[Column] ) returns every value of the column and removes existing filters on it. Use it when the iteration set must not shrink with the visual or slicers. Ranking is the typical case, because every product needs a place in the list even when the cell is for a single product:
Product Rank =
RANKX (
ALL ( Product[Product Name] ),
[Sales Amount],
,
DESC,
Dense
)A variant is ALLSELECTED, which restores the filters that come from outside the visual, such as slicers. It iterates only the items the user left selected. Use ALL when you want a fixed universe, and ALLSELECTED when the universe should follow the slicers.
Here is a measure that finds the best category regardless of what is selected in the visual's rows:
Best Category Sales =
MAXX (
ALL ( Product[Category] ),
[Sales Amount]
)Comparing the four choices
| Table argument | Rows visited | Typical use |
|---|---|---|
| `Sales` (model table) | Rows visible in the filter context | Per-line math such as quantity times price |
| `FILTER ( Sales, ... )` | Visible rows that pass a condition | Calculations on a subset of rows |
| `VALUES ( Dim[Column] )` | Distinct members in the current context | Averages and totals per customer, day, or product |
| `ALL ( Dim[Column] )` | Every member, ignoring filters on that column | Rankings and benchmarks across the full list |
How to decide
Common errors
- Iterating the wrong grain.
AVERAGEX ( Sales, ... )gives an average per line, not per customer or per order. If the number looks too small, check the grain. - Using a column from another table without
RELATED. Inside an iterator overSales,Product[Unit Cost]raises an error until you writeRELATED ( Product[Unit Cost] ). - Expecting
FILTERto override filter context.FILTER ( Sales, ... )can only narrow rows already visible. To widen them, useALL(or wrap the call inCALCULATE). - Using
ALLwhen a slicer should apply. A rank built onALLignores a category slicer, so ranks can be larger than the number of visible products. Switch toALLSELECTEDif that is not what you want. - Using
FILTERover a whole table when a column predicate would do.FILTER ( Sales, Sales[Quantity] >= 5 )iterates the entire fact table. InCALCULATE, a plain predicate such asSales[Quantity] >= 5is usually cheaper, but inside an iterator you needFILTERwhen the condition involves a row-by-row calculation.
Putting it together
The next measure combines two of the choices. It counts the average sales of a customer, but only for customers who bought at least once in the selected period:
Avg Sales per Active Customer =
AVERAGEX (
FILTER (
VALUES ( Customer[CustomerKey] ),
[Sales Amount] > 0
),
[Sales Amount]
)VALUES sets the member list from the current context. FILTER removes customers whose total is zero or blank. AVERAGEX averages the rest. The explicit filter is redundant here, since AVERAGEX already skips blanks, but it makes the intent visible and it matters if the measure can return zero instead of blank.
Exercise
A report shows sales by product category, with a slicer for year. You want each category's rank by [Sales Amount] among the categories that appear in the report after the user filters other slicers, such as Region, but you do not want the rank to change when rows are removed by a visual-level filter. Which table argument fits best for RANKX, ALL ( Product[Category] ) or ALLSELECTED ( Product[Category] ), and what happens to a category's rank if you use the other one?
Show answer
Use ALL ( Product[Category] ) when the rank must stay stable. It ranks against every category, so removing rows with a visual-level filter does not change anyone's rank, although a category can show a rank larger than the number of visible rows. With ALLSELECTED the ranking universe is only the categories left visible by outer filters and slicers, so ranks run from 1 to the visible count and shift as the selection changes. Choose ALLSELECTED when the rank should be relative to what the user is looking at.
Exercise
You write Avg per Customer = AVERAGEX ( Sales, [Sales Amount] ) and the result is much smaller than expected. Why, and how do you fix it?
Show answer
The iterator visits each sales line, so the average is per line, with context transition making each cell the sales of that single row. The grain is wrong. Iterate over customers instead: AVERAGEX ( VALUES ( Customer[CustomerKey] ), [Sales Amount] ).