SUM vs SUMX in Power BI: What's the difference?

SUM vs SUMX in Power BI: What's the difference?

Summation vs Summation on Steroids ๐Ÿš€

ยท

2 min read

The SUM ( ) function is one of the first functions any DAX beginner would learn that would perform a really simple summation calculation. In Power BI you meet SUMX ( ) function which looks almost the same and performs the same calculation, but with a little twist.

In this post let's find out why we need 2 functions if both do the same thing. What's the difference and what are the different situations we can use SUM ( ) and SUMX ( ) functions?

SUM vs SUMX. The Syntax

SUM ( )

SUM(column)

SUM ( ) - Microsoft Docs

This is what happens when the function is applied to a column and it always should be a column.

SUMX ( )

SUMX(table, expression)

SUMX ( ) - Microsoft Docs

SUMX ( ) apply the "expression" to the given "table" and "SUM ()" up the return values. Let me explain.

Here we have a new table with 2 numerical columns. Unit Price and Qty.

Let's calculate Total Price. To do that we can use the rookie method. Create a new column with Unit Price * Qty calculation and then Use SUM ( ) to get the total.

Here is the SUMX ( ) function for that,

Total Price = SUMX('Invoice', 'Invoice'[Unit Price]*'Invoice'[Qty])

When the SUMX ( ) is used like this it executes the calculation (expression) against each row in the table and does the summation to the result values.

Also, you can use SUMX ( ) to choose a subset of the "table" to apply summation using a condition as the "expression".

We have a table with 2 products. Apple and Orage. We only need the Total Price of the Apples.

Let's use SUMX ( ) for that.

Total Apple Price =
SUMX (
    'Invoice',
    IF ( Invoice[Product] = "Apple", 'Invoice'[Unit Price] * 'Invoice'[Qty], 0 )
)

SUMX ( ) executes the calculation against each row where the condition is true and returns the total.

for more Power BI content.

Visit my blog - SUM vs SUMX in Power BI. What's the difference? - overbeeps

ย