SUM vs SUMX in DAX: when you need the X

The difference between SUM and SUMX in DAX, aggregating a column against evaluating an expression per row, why SUM is SUMX in disguise, line amounts, weighted values, measures inside SUMX, totals that match the rows, and performance.

Short answer: SUM ( Sales[Amount] ) adds up one column. SUMX ( table, expression ) goes through a table row by row, evaluates the expression for each row, and adds the results. Use SUM when the number is already in a column. Use SUMX when it must be calculated per row first (Quantity * Price), or when a total must add up the rows of a visual (the sum of per-customer results). Internally, SUM ( col ) is SUMX ( table, col ), so there is no speed penalty for SUMX with simple column math.

A Sales table has Quantity and Unit Price but no Amount. SUM ( Sales[Quantity] ) * SUM ( Sales[Unit Price] ) multiplies two totals and is badly wrong. SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ) multiplies per line, then adds.

Why does the order of operations matter?

LineQuantityUnit PriceQuantity x Price
121020
25100500
SUM(Qty) x SUM(Price)7110770
SUMX(Qty x Price)520
Two order lines
Revenue Wrong = SUM ( Sales[Quantity] ) * SUM ( Sales[Unit Price] )

Revenue = SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )

When else do you need SUMX?

-- 1 a related value per row
Revenue at List = SUMX ( Sales, Sales[Quantity] * RELATED ( 'Product'[List Price] ) )

-- 2 a weighted average: price weighted by quantity
Avg Price Weighted =
DIVIDE ( SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ), SUM ( Sales[Quantity] ) )

-- 3 a total that adds up the rows: sum of each customer's capped bonus
Bonus =
SUMX (
    VALUES ( Customer[Customer] ),
    MIN ( [Revenue] * 0.05, 500 )
)

-- 4 currency conversion per row
Revenue EUR = SUMX ( Sales, Sales[Amount] * RELATED ( Rates[Rate] ) )

Example 3 is the important one for totals. Without SUMX, the total row computes MIN ( total revenue * 0.05, 500 ), which is 500, not the sum of the customers' bonuses.

Why do totals not add up without it?

CustomerRevenueBonus with MIN onlyBonus with SUMX over customers
Ana4,000200200
Ben20,000500500
Chen8,000400400
Total32,000500
Total32,0001,100
Capped bonus per customer

A measure is evaluated in the total's own filter context, not as a sum of the rows. SUMX over the grouping column forces the total to be the sum of the per-row results.

How fast is SUMX?

Simple arithmetic on columns of one table (Quantity * Price) is handled by the storage engine in bulk and is as fast as SUM. It gets slower when:

  • the expression calls a measure (one context transition per row): iterate a small table such as VALUES ( Customer[Customer] ), not the fact table,
  • the expression uses RELATED across large tables,
  • the expression has IF or other logic the storage engine cannot push down.

A calculated column Line Amount makes SUM possible but costs memory; with SUMX the model stays smaller.

What goes wrong?

  • Multiplying sums. SUM ( A ) * SUM ( B ) is not SUMX ( A * B ).
  • SUMX over the fact table with a measure inside. Millions of context transitions; iterate the dimension.
  • Expecting SUMX to change filters. It iterates the rows in the current filter context; it removes nothing.
  • Blank handling. Rows where the expression is BLANK are skipped in the sum.

Where can you check this?

Where do you start?

Find measures that multiply two sums or totals that do not match their rows, and rewrite them with SUMX over the right table. AVERAGEX, MINX and MAXX work the same way for other aggregates.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.