Sub topic 4
SUMX: summing a per-row calculation
This chapter assumes you can write simple measures with SUM and CALCULATE, and that you know what a fact table, a dimension table, and a relationship are. It builds on the iterator pattern from the previous chapter: a table, an expression, and an aggregation.
Why SUM is not enough
SUM adds up one column. If the value you need is not stored in a column, SUM cannot produce it. A typical case is a Sales table with Quantity and Net Price but no line-amount column. The amount of each line is quantity times price, and the total is the sum of those line amounts.
SUMX solves this. It takes a table and an expression. For each row of the table, it evaluates the expression in row context, then adds the results.
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)SUMX visits each row of Sales, multiplies quantity by price, and adds up the products. Summing the two columns separately and then multiplying the totals would give a wrong answer.
How SUMX evaluates
The measure runs inside the filter context of the report. If a visual shows one year, the Sales table that SUMX receives already contains only that year's rows. SUMX then creates a row context on each of those rows.
The sequence has three steps:
- The filter context decides which rows of the table argument are visible.
- SUMX steps through those rows. In each step, the columns of the current row are available to the expression.
- The per-row results are added into a single number.
Using RELATED inside SUMX
The expression can only read columns from the current row of the iterated table directly. Sometimes the value you need is on a dimension table. Suppose Unit Cost is stored in Product, and Sales has a many-to-one relationship to Product. RELATED follows that relationship from the current Sales row to its Product row.
Total Cost =
SUMX (
Sales,
Sales[Quantity] * RELATED ( Product[Unit Cost] )
)RELATED works in this direction, from the many side to the one side, because each Sales row has exactly one matching product. It needs a row context, which SUMX provides.
Choosing the table argument
The first argument defines which rows are visited, so it also defines what is summed. You can pass the table in several forms:
| Table argument | Rows visited |
|---|---|
| `Sales` | Rows of Sales visible in the current filter context |
| `FILTER ( Sales, Sales[Quantity] > 1 )` | Only visible rows that also meet the condition |
| `VALUES ( Product[Color] )` | One row per distinct color in the current filter context |
| `ALL ( Product )` | Every product, ignoring the current filters |
Iterating a fact table gives you line-level calculations. Iterating a dimension column, such as VALUES ( Product[Color] ), gives you one step per member. This is useful when the expression contains a measure, which the later chapters cover.
Common errors
- Using RELATED on the wrong side. RELATED does not work from the one side to the many side. To read many-side values from a dimension row, use an aggregation or RELATEDTABLE instead.
- Forgetting that SUMX respects filters. If you pass
ALL ( Sales ), the report filters on Sales are removed, and the total may not match what the visual shows. - Using SUMX when SUM would do.
SUMX ( Sales, Sales[Quantity] )returns the same result asSUM ( Sales[Quantity] ). The engine often optimizes this case, but the plain aggregation is clearer.
Exercise
Sales has Quantity and Discount Amount columns, and Product has Unit Cost. Write a measure named Margin that sums, for each Sales row, the net amount (Sales[Quantity] * Sales[Net Price] minus Sales[Discount Amount]) minus the cost (Sales[Quantity] times the product's Unit Cost).
Show answer
Margin = SUMX ( Sales, Sales[Quantity] Sales[Net Price] - Sales[Discount Amount] - Sales[Quantity] RELATED ( Product[Unit Cost] ) )