What is the use case of SUM vs SUMX? - powerbi

I'm learning DAX.
For a measure, I can write: Profit = SUM(Sales[SalesAmt])-SUM(Sales[ProdcutionCost])
This gives me the Profit.
I get the same result if I do: Profit = SUMX(Sales,Sales[SalesAmt]-Sales[ProdcutionCost])
So what is the use case or difference between SUM and SUMX?

Actually, SUM is just syntactic sugar for simple sums of a column.
SUM ( 'Table'[Column] )
is shorthand for (and will be translated to by the engine at query time)
SUMX ( 'Table' , 'Table'[Column] )
and the use case is, again, for when you only want a simple sum. If you want to do more things in a row context across your table, you need to invoke SUMX.
Consider a case where you have a table like this, with unit price and quantity and want to calculate the total revenue:
Transaction ID
Product ID
Unit Price
Quantity
1
1
5.99
5
1
2
10.49
3
2
1
5.99
3
In this case you cannot do:
Total Revenue = SUM ( 'Table'[Unit Price] ) * SUM ( 'Table'[Quantity] )
Instead you would have to use the row context in the explicit SUMX iterator to sum row by row:
Total Revenue =
SUMX (
'Table' ,
'Table'[Unit Price] * 'Table'[Quantity]
)

Related

What is the difference between using SUM or SUMX when calculating the average by sum of customer sales?

What is the difference between using SUM or SUMX when calculating the average by sum of customer sales (example 1 and example 2)? And what happens when we wrap them with calculate (example 3 and example 4)?
Example 1:
AverageSalesPerCustomer :=
AVERAGEX (
Customer,
SUM (Sales[SalesAmount])
)
Example 2:
AverageSalesPerCustomer :=
AVERAGEX (
Customer,
SUMX (
Sales,
Sales[SalesAmount]
)
)
Example 3:
AverageSalesPerCustomer :=
AVERAGEX (
Customer,
CALCULATE(SUM (Sales[SalesAmount]))
)
Example 4:
AverageSalesPerCustomer :=
AVERAGEX (
Customer,
CALCULATE( SUMX (
Sales,
Sales[SalesAmount]
) )
)
SUM function takes single column of data to add all the data in that column. Syntax:
SUM(<Column Name>)
SUMX function first iterates row by row to perform the given expression on each row and finally sum up all the values to give the final sum.
Syntax:
SUMX (<Table>, <Expression>)
Example:
SUM(Sales[Units])
return the sum of total units in Sales table
SUMX(Sales, Sales[Units] * Sales[Price])
will first multiply number of units with price for each row and then will find the sum of values found for all the rows to return total sales amount

Cumulative total percentage in Power BI

I have a summary table in Power BI which shows how many days it takes for leads to convert to a sale. It has 2 columns, sum_convert (the amount of days in between lead creation date and converted date) and count_lead (the count of leads that have taken that amount of days to convert), both are numeric values. Here is an example of the data:
What I want, is a column next to count_lead that shows the running percentage total in the specific ascending order of sum_convert. Currently I've created a measure called lead_count which is the sum of count_lead. Then I've attempted to create the cumulative total with the following measure:
Cum_Lead = calculate([lead_count], FILTER(ALL(Convert_Count_Summary[Sum_Convert]), SUM(Convert_Count_Summary[count_lead]) <= [lead_count]))
This creates a cumulative total, but not in the specific sum_convert order, it's in the order of largest volume for count_lead. Any idea what I need to change so that it's in the order of sum_convert?
You could do this in Power Query using M:
= Table.AddColumn(#"Previous Step", "Cumulative_Count_PQ", each List.Sum(List.FirstN(#"Previous Step"[count_lead],_[sum_convert]+1)), type number)
Or as a calculated column using DAX:
Cumulative Count DAX =
CALCULATE (
SUM ( Convert_Count_Summary[count_lead] ),
ALL ( Convert_Count_Summary ),
Convert_Count_Summary[sum_convert] <= EARLIER ( Convert_Count_Summary[sum_convert] )
)
Edit:
Cumulative percentages in Power Query:
= Table.AddColumn(#"Previous Step", "Cumulative_Count_Percent_PQ", each List.Sum(List.FirstN(#"Previous Step"[count_lead],_[sum_convert]+1)) / List.Sum(#"Previous Step"[count_lead]), Percentage.Type)
Cumulative percentages calculated column in DAX:
Cumulative Count % DAX =
VAR _Numerator =
CALCULATE (
SUM ( Convert_Count_Summary[count_lead] ),
ALL ( Convert_Count_Summary ),
Convert_Count_Summary[sum_convert] <= EARLIER ( Convert_Count_Summary[sum_convert] )
)
VAR _Divisor =
SUM ( Convert_Count_Summary[count_lead] )
RETURN
DIVIDE (
_Numerator,
_Divisor
)

PowerBI rolling sum of sums

I have 4 columns
Sales
Spend1
Spend2
Spend3
I want to calculate the rolling Net Sales
I know I can do this :
Running_Total =
CALCULATE (
SUM ( Revenue[Sales] ),
FILTER (
Targets[Date] <= EARLIER ( Targets[Date] )
)
)
What strategy can be used to calculate the rolling sum of the following formula :
Sales - Spend1 - Spend2 - Spend

sum product of two columns in powerbi table with dax

I would like to get the sum product of this table:
example: (0*.0256) + (1*.0468) + (2*.0344) ...
days to departure is a dimension under one table. And Conversions is a measure that I'm showing as % of Grand Total.
Thanks for any advice.
So we have table T with the column [Days To Departure] and the measure [%GT Conversions]
We want a measure to sum all the values of [%GT Conversions] for all the rows of T.
Let's call this measure [Total Formula], since I don't know what it'd be expected to represent
Total Formula =
VAR Total = SUMX( ALL( T ), T[Conversion] )
RETURN
SUMX( ALL( T ), T[Days To Departure] * T[Conversion] / Total )

Percentage value of a segment against segment total Power BI(DAX)

Hi guys, I am new to Power BI(DAX formulas) and I am attempting to calculate the percentage contribution of the sum of "count" where "category" = X and "item_no"=1 to the total of "count" across all categories where 'item_no' = 1.
The ideal mathematical statement here will be the (30/50)*100%
I intend to represent the percentage values in a chart showing percentage contribution of each distinct item_no to its total in the format as represented in the example above.
The standard way to approach this is
calculation over partial set / same calculation over larger set
Since you haven't made it clear what context you are trying to calculate this, I will assume it's a visual along these lines:
The measure I use here is
%ItemTotal =
DIVIDE (
SUM ( Table1[count] ),
CALCULATE ( SUM ( Table1[count] ), ALLEXCEPT( Table1, Table1[item_no] ) )
)
In the numerator, you have the sum in the local filter context. For example, in that top-left cell, this means all rows that match item_no = 1 and category = "X".
In the denominator, we do the same thing except we remove all filter context except the context we say to keep (item_no) so it includes all category values.
If you're trying to calculate that 60% outside of the context of a visual, then you can explicitly define what filters you want. For example, this should work in any filter context:
X%_Item1 =
DIVIDE (
CALCULATE (
SUM ( Table1[count] ),
ALL ( Table1 ),
Table1[category] = "X",
Table1[item_no] = 1
),
CALCULATE (
SUM ( Table1[count] ),
ALL ( Table1 ),
Table1[item_no] = 1
)
)
See here and here for other ways to modify the filter context instead of ALLEXCEPT.