Sub topic 6
Choosing the table to iterate over
This chapter assumes you can write measures with SUM and CALCULATE and that you know the difference between row context and filter context. It also assumes a star schema with a Sales fact table and dimension tables such as Product, Customer, and Date.
The first argument sets the rows
Every iterator such as SUMX, AVERAGEX, or RANKX takes a table as its first argument. The iterator visits each row of that table, evaluates the expression in row context, and combines the results. The table you pass decides which rows are visited and how many there are, so it decides the result.
The table is evaluated in the current filter context. A slicer, a row on a matrix, or a CALCULATE filter changes which rows come back before the iteration starts.
The four common choices
| Table argument | Rows visited | Typical use |
|---|---|---|
| A table such as `Sales` | Every row visible in the filter context | Per-row math such as quantity times price |
| `FILTER ( table, condition )` | Only rows that pass the condition | Per-row math on a subset of rows |
| `VALUES ( column )` | Distinct values of the column in the filter context | Averages over customers, days, or products |
| `ALL ( column )` | Every value of the column, ignoring filters on it | Ranking and shares against the full list |
A table or FILTER: iterate rows
Pass the fact table when each row needs its own calculation.
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)To restrict the rows, wrap the table in FILTER. FILTER returns only the rows where the condition is true.
Large Order Sales =
SUMX (
FILTER ( Sales, Sales[Quantity] >= 5 ),
Sales[Quantity] * Sales[Net Price]
)VALUES: iterate members
When the unit of analysis is a customer or a day, iterate over distinct values of that column. Each iteration is one member, not one sales row.
Avg Sales per Customer =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)If a customer bought ten times, VALUES returns that customer once. The measure inside is then evaluated once for that customer, through context transition. If you iterated over Sales instead, you would average per sales row, which is a different number.
ALL: iterate regardless of filters
ALL ( column ) returns every value of the column and removes filters on that column. Use it when the iteration must cover the whole list, even when a slicer or a matrix row selects one item.
Share of All Products =
DIVIDE (
[Sales Amount],
SUMX (
ALL ( Product[Product Name] ),
[Sales Amount]
)
)The numerator respects the current product selection. The denominator visits every product and adds up their sales, so the result is the selected product's share of the total. RANKX uses the same idea, and a later chapter covers how to choose between ALL and ALLSELECTED there.
Common errors
- Passing a whole dimension table such as
Customerwhen you meant one column. The iteration then runs over every column of each row, and the intent is harder to read. - Using FILTER over a large fact table when a simple column condition in CALCULATE would do. FILTER visits every row.
- Using
ALLand then wondering why a slicer has no effect. That is its purpose.
Exercise
You want the average sales per product for the products visible in the current filter context. Which table argument do you pass to AVERAGEX, and what do you pass as the expression?
Show answer
Pass VALUES ( Product[Product Name] ) as the table, so each iteration is one product currently visible. Use [Sales Amount] as the expression. Context transition turns the measure reference into that product's sales. Using ALL would include products excluded by slicers, and using Sales would average per sales row.