DAX SUMX Function
How To iterate over a table and return the sum of an expression evaluated for each row?
SUMX
is an iterator function in DAX that:
- Iterates over a table
- Evaluates an expression for each row
- Returns the sum of those evaluated values
Syntax
SUMX(<table>, <expression>)
<table>
: The table (real or virtual) to iterate over<expression>
: An expression to evaluate for each row in the table
Concept
Think of SUMX
as a loop:
-
For each row in the table:
- Evaluate the expression (e.g.,
Price * Quantity
) - Sum all the results
- Evaluate the expression (e.g.,
This is different from SUM
, which just adds up values in a single column no row-by-row calculation.
Example
Let's say you have a Sales
table:
Product | Quantity | UnitPrice |
---|---|---|
A | 2 | 10 |
B | 3 | 15 |
C | 1 | 20 |
You want to calculate total revenue (Quantity * UnitPrice
for each row).
- This won't work:
SUM(Sales[Quantity] * Sales[UnitPrice])
-- INVALID! SUM can't do row-by-row multiplication
SUMX
Use SUMX(Sales, Sales[Quantity] * Sales[UnitPrice])
Result
Row 1: 2 × 10 = 20 Row 2: 3 × 15 = 45 Row 3: 1 × 20 = 20 Total: 20 + 45 + 20 = 85
SUMX
(vs SUM
)
When to Use Use Case | Use SUM | Use SUMX |
---|---|---|
Summing a single column | ✅ | |
Calculating row-level expressions | ✅ | |
Working with virtual tables (e.g., FILTER , VALUES ) | ✅ |
FILTER
)
Real-World Example (with Calculate total sales for products with quantity > 2:
SUMX(
FILTER(Sales, Sales[Quantity] > 2),
Sales[Quantity] * Sales[UnitPrice]
)
Here, SUMX
is summing revenue only for rows where Quantity > 2
, thanks to FILTER
.
Summary
Feature | SUMX |
---|---|
Function type | Iterator (row by row) |
Takes a table | Yes |
Takes an expression | Yes (evaluated per row) |
Use case | Row-based calculations, virtual tables |
No questions available.