SUMX vs SUM in DAX: Key Differences and When to Use Each
SUM adds all values in a single column directly, while SUMX iterates over a table, evaluates an expression for each row, then sums the results. Use SUM for simple column totals and SUMX when you need row-by-row calculations before summing.Quick Comparison
Here is a quick side-by-side comparison of SUM and SUMX functions in DAX.
| Factor | SUM | SUMX |
|---|---|---|
| Type | Aggregation function | Iterator function |
| Input | Single column | Table and expression |
| Operation | Adds column values directly | Evaluates expression row-by-row then sums |
| Performance | Faster for simple sums | Slower due to row iteration |
| Use case | Simple total of a column | Complex calculations per row before summing |
| Example | Total sales amount | Total of (Quantity * Price) per row |
Key Differences
SUM is a straightforward aggregation function that adds all the numbers in a single column. It is simple and efficient when you just need the total of one column without any additional calculations.
SUMX, on the other hand, is an iterator function. It goes through each row of a table, calculates an expression for that row, and then sums all those calculated values. This makes SUMX more flexible for scenarios where you need to multiply, filter, or perform other calculations on each row before adding.
Because SUMX evaluates an expression row-by-row, it can be slower than SUM on large datasets. However, it is essential when your total depends on a calculation involving multiple columns or conditions.
Code Comparison
Example: Calculate total sales amount from a column named Sales[Amount].
Total Sales = SUM(Sales[Amount])
SUMX Equivalent
Example: Calculate total sales by multiplying Quantity and Price columns row-by-row, then summing the results.
Total Sales = SUMX(Sales, Sales[Quantity] * Sales[Price])
When to Use Which
Choose SUM when you need a simple total of a single column without any extra calculations. It is faster and easier to use for straightforward sums.
Choose SUMX when your total depends on calculations involving multiple columns or conditions per row, such as multiplying quantity by price or applying filters before summing. It offers flexibility at the cost of performance.
Key Takeaways
SUM adds values directly from one column and is best for simple totals.SUMX iterates row-by-row to calculate expressions before summing, useful for complex calculations.SUM for performance when no row-level calculation is needed.SUMX when totals depend on multiple columns or expressions per row.