Sub topic 4
SUMX: summing a per-row calculation
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 one-to-many relationship are in a star schema. The chapters on row context and filter context come before this one. Here we apply both to SUMX.
Why SUM is not enough
SUM adds up one column. Revenue is not stored in a column of the Sales table. It is the product of two columns, Quantity and Net Price, taken row by row.
The following measure looks plausible but does not work:
Wrong Sales Amount =
SUM ( Sales[Quantity] ) * SUM ( Sales[Net Price] )It runs, but it multiplies two totals. Take three sales rows:
| Row | Quantity | Net Price | Quantity × Net Price |
|---|---|---|---|
| 1 | 2 | 10 | 20 |
| 2 | 3 | 5 | 15 |
| 3 | 1 | 20 | 20 |
The correct revenue is 20 + 15 + 20 = 55. The wrong measure gives 6 × 35 = 210. The multiplication must happen before the addition, once for each row. That is what SUMX does.
The shape of SUMX
SUMX takes two arguments:
SUMX ( <table>, <expression> )- DAX builds the table in the first argument.
- For each row, DAX evaluates the expression in a row context for that row.
- DAX adds the results.
Here is the measure that fixes the example above:
Sales Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)On the three rows it returns 55.
SUMX uses both kinds of context, and each plays a different role:
- Filter context acts before the iteration. It decides which rows of Sales exist when SUMX reads the table. If a report row is filtered to Year 2025, SUMX only visits 2025 sales rows. You write no extra code for this.
- Row context is created by SUMX itself, one row at a time. It is the reason you can write
Sales[Quantity]as a bare column reference inside the expression. Without a row context, a bare column reference in a measure is an error.
A useful rule: the table argument is evaluated in the filter context, and the expression argument is evaluated in a row context on top of it.
The expression can be any scalar
The second argument must return a single value for each row. It can use arithmetic, IF, SWITCH, text functions, or other scalar logic. For example, a discount stored as a percentage column:
Discounted Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price] * ( 1 - Sales[Discount Pct] )
)A conditional expression is also fine:
Bulk Sales Amount =
SUMX (
Sales,
IF (
Sales[Quantity] >= 10,
Sales[Quantity] * Sales[Net Price],
0
)
)This is valid, but you can usually write the same logic as a filter on the table argument. That is the next section.
Choosing the table to iterate over
Any expression that returns a table can be the first argument. The three forms you will use most:
| First argument | Rows visited | Typical use |
|---|---|---|
| `Sales` | Sales rows left by the current filter context | Standard per-row calculation |
| `FILTER ( Sales, <condition> )` | Only the rows that also meet the condition | Per-row calculation on a subset |
| `VALUES ( Product[Category] )` | One row per distinct visible category | Calculation per member, then sum |
The bulk-sales measure rewritten with FILTER:
Bulk Sales Amount =
SUMX (
FILTER ( Sales, Sales[Quantity] >= 10 ),
Sales[Quantity] * Sales[Net Price]
)FILTER receives the Sales table already restricted by the filter context. It then removes more rows. The result cannot be wider than the report's current selection. If you need to ignore the report's filters, wrap the table in ALL, or use CALCULATE with a filter modifier. Choosing between these is the topic of the table-argument chapter, so this chapter keeps to the simple cases.
Values from related tables
The Net Price column is on the Sales table. The unit cost is on the Product table. Inside the iteration you are on a Sales row, and each Sales row points to exactly one Product. The RELATED function follows that many-to-one relationship:
Total Cost =
SUMX (
Sales,
Sales[Quantity] * RELATED ( Product[Unit Cost] )
)Two points matter here:
- RELATED works from the many side to the one side. Iterating over Sales and reading Product columns works. Iterating over Product and calling RELATED to read Sales columns does not, because each product has many sales rows.
- Margin follows directly from the two measures you have:
Margin = [Sales Amount] - [Total Cost]A measure can reference another measure. It does not have to repeat the SUMX code.
Edge cases and blanks
SUMX handles empty and missing values in specific ways. These rules are easy to forget and cause surprises in reports.
- No rows: if the table argument is empty, SUMX returns BLANK, not 0. In a matrix, an item with no sales shows an empty cell, which is usually what you want.
- Blank results per row: if the expression returns BLANK for a row, that row is skipped. It contributes nothing.
- Blank inputs in arithmetic: in
Sales[Quantity] * Sales[Net Price], a BLANK in either column is treated as 0 in the multiplication, so the row result is 0, not BLANK. - Text results: SUMX only sums numbers. If the expression returns text for some rows, those rows are ignored. An expression that is text for every row gives an error or a blank, depending on the content.
- Adding a constant:
SUMX ( Sales, 1 )counts the rows, like COUNTROWS. It rarely beats COUNTROWS but shows how the pieces fit.
If you want the measure to display 0 instead of blank, add it explicitly: Sales Amount + 0. Do this deliberately. In a large matrix, it makes every combination of rows and columns visible, which can slow the report and clutter it.
Measures and context transition inside SUMX
Take a measure reference as the expression instead of column arithmetic:
Sum of Customer Sales =
SUMX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)The iteration here runs over customer keys. For each key, a row context exists on the Customer table. A measure reference such as [Sales Amount] is silently wrapped in CALCULATE. That wrapping converts the current row into a filter. It is called context transition. As a result, [Sales Amount] is evaluated for that one customer and returns that customer's sales. SUMX then adds one value per customer.
The result matches a plain [Sales Amount] at the report row's grain, because every sale belongs to one customer. The measure becomes useful when the per-member value is not simply additive. For example, replace the expression with MIN ( [Sales Amount], 1000 ) to cap each customer's contribution at 1,000 before adding. A single SUM cannot do that.
Context transition has a cost. Each row triggers a separate evaluation of the measure. This matters for the next section.
Performance
SUMX over a fact table with a simple column expression is fast. The storage engine, which scans compressed columns, can usually process Quantity * Net Price without handing rows back to the slower formula engine. In practice, this pattern scales to hundreds of millions of rows.
The following make an iterator slower:
- Measure references in the expression over a large table. Each row triggers a context transition. Iterating over a fact table with millions of rows and calling a measure in the expression is the most common cause of slow reports. Iterate over a smaller table, such as VALUES of a dimension key, when you need a measure inside.
- Complex row logic that the storage engine cannot run. Some functions force a callback to the formula engine for every row.
- Nested iterators. The costs multiply: 1,000 outer rows with 1,000 inner rows means a million evaluations.
For a single column, prefer the aggregator. SUMX ( Sales, Sales[Quantity] ) returns the same result as SUM ( Sales[Quantity] ). SUM states the intent more clearly, and it is never slower.
If the same per-row product is summed in many measures, you can store it in a calculated column on Sales. That trades model size and refresh time for query speed. For most models, the plain SUMX measure is the better default, because it adds no storage.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `SUM ( Sales[Quantity] * Sales[Net Price] )` fails | SUM accepts only a single column | Use SUMX |
| Totals look too large | Multiplying two aggregated totals | Move the multiplication inside SUMX |
| RELATED returns an error | The iterated table is on the one side of the relationship, or no relationship exists | Iterate over the many-side table |
| Row shows blank, you expected 0 | Table argument is empty | Add `+ 0` on purpose, or accept the blank |
| Measure is slow | Context transition on a very large table | Iterate over a smaller table or push the logic into a column expression |
Exercise
A Sales table has these rows (Quantity, Net Price): (4, 2.50), (1, 40.00), (10, 1.00). Write a measure that returns total revenue and give its result. Then write a measure that returns the revenue of only the rows with Quantity of at least 4.
Show answer
Total revenue is Sales Amount = SUMX ( Sales, Sales[Quantity] Sales[Net Price] ), which gives 4 × 2.50 + 1 × 40.00 + 10 × 1.00 = 10 + 40 + 10 = 60. For rows with Quantity of at least 4, use SUMX ( FILTER ( Sales, Sales[Quantity] >= 4 ), Sales[Quantity] Sales[Net Price] ). That keeps rows 1 and 3 and gives 10 + 10 = 20.
Exercise
Explain why SUM ( Sales[Quantity] ) SUM ( Sales[Net Price] ) gives a different answer from SUMX ( Sales, Sales[Quantity] Sales[Net Price] ) on the rows in the previous exercise.
Show answer
SUM first totals each column, giving 15 and 43.50, and then multiplies them: 652.50. SUMX multiplies within each row first and then adds, giving 60. Multiplication does not distribute over the sum of different columns, so the order of operations changes the result.