cancel
Showing results for
Did you mean: Helper II

## Need help calculating a cumulative sum of measures?

Hi Community,

I'm working on a project that calculates a total of mix impact when comparing data between 2 selected periods. I have measures that calculate all of component, but I can't sum them up as a total. Is there a way to do it?

Below is a mock data.

Fact Table

 Period ID State Type Type 1 CA Volume 10000 1 CA Cost 50000 1 NY Volume 1500 1 NY Cost 15000 2 CA Volume 8000 2 CA Cost 45000 2 NY Volume 3000 2 NY Cost 20000 2 TX Volume 1000 2 TX Cost 6000 3 NY Volume 2500 3 NY Cost 10000 3 TX Volume 1000 3 TX Cost 5000

User will use 2 slicers to select data for a comparison. These slicers are Period ID. Let's say a user select peiord 1 in slicer #1 and period 2 in slicer #2

The calculation logics:

 Period 1 State Vol Cost Avg CA 10000 50000 5 NY 1500 15000 10 TX 0 0 0 Total 11500 65000 5.652174

 Period 2 State Vol Cost Avg CA 8000 45000 5.625 NY 3000 20000 6.666667 TX 1000 6000 6 Total 12000 71000 5.916667

First we find a total of Volume, Cost, and Average for each period. Here are the measures

[Volume Sel 1] >> a total volume of each state
[Total Volume Sel 1] >> a total volume of all state in a period
[Avg Cost Sel 1] >> an average cost of each state
[Total Avg Cost Sel 1] >> an average cost of all state in a period

selection #2 has the same measures

Now, we have to find a percentage of a volume from each state comparing to a total of all state in a period. Then, we find what a percentage change of each state between 2 period

 Vol % State Period 1 Period 2 Chg CA 0.8695652 0.666666667 -0.2029 NY 0.1304348 0.25 0.119565 TX 0 0.083333333 0.083333 Total 1 1 0

Once we have a percentage different of each state, we will each state volume impact. To do this, we use a percentage different multiply by a total volume of all state from period #1

To find Avg cost Impact of each state, we use state cost average from period #1 subtract by a total average cost from period #1. Since TX didn't have an average cost in period #1, we use TX average cost from period #2 and subtract by  a total average cost from period #1.

 Dol Impact State Vol Impact Avg Impact CA -2333.3333 -0.65217391 1521.739 NY 1375 4.347826087 5978.261 TX 958.33333 0.347826087 333.3333 Total 7833.333

Once we have both Vol Impact and Avg Impact, we will calculate Dol Impact by multiply Vol Impact by Avg Impact then sum all of state Dol Impact to get our total Dol Impact.

Here is my Dol Impact measure code

Dol Impact =

VAR Vol1_Pct = [Volume Sel 1] / [Total Volume Sel 1]
VAR Vol2_Pct = [Volume Sel 2] / [Total Volume Sel 2]
VAR Pct_Diff = Vol1_Pct - Vol2_Pct
VAR Vol_Impact = Pct_Diff * [Total Volume Sel 1]
VAR AvgCost_Impact =
IF([Volume Sel 1] <> 0, [Avg Cost Sel 1], [Avg Cost Sel 2) - IF([Total Volume Sel 1] <> 0, [Total Avg Cost Sel 1], [Total Avg Cost Sel 2])
VAR Dollar_Impact = Vol_Impact * AvgCost_Impact
RETURN
Dollar_Impact

So, when I put this Dol Impact measure in a Matrix table with State as rows, it displays each state dollar impact correctly, but the total line shows 0. My goal is to display a total dollar impact as a card. As of right now, it shows 0 as a total dollar impact as a card.

Is there a way to sum each state dollar impact up and display as one value? Please help! Thank you for your time.
1 ACCEPTED SOLUTION

Accepted Solutions Super User I

When you want a measure calculated at a certain granularity then summed, I would normally use SUMX.

The first argument of SUMX should be a table containing rows representing the granularity of the calculation (in your case State), and the second argument should be the calculation itself.

In your case, you have a couple of options:

1. Write another measure than sums the original measure by State, and use this new measure in your visuals:

``````Dol Impact Summed by State =
SUMX (
VALUES ( FactTable[State] ),
[Dol Impact]
)``````

2. Rewrite the original measure so that it sums by State:

``````Dol Impact =
SUMX (
VALUES ( FactTable[State] ),
VAR Vol1_Pct = [Volume Sel 1] / [Total Volume Sel 1]
VAR Vol2_Pct = [Volume Sel 2] / [Total Volume Sel 2]
VAR Pct_Diff = Vol1_Pct - Vol2_Pct
VAR Vol_Impact = Pct_Diff * [Total Volume Sel 1]
VAR AvgCost_Impact =
IF ( [Volume Sel 1] <> 0, [Avg Cost Sel 1], [Avg Cost Sel 2] )
- IF ( [Total Volume Sel 1] <> 0, [Total Avg Cost Sel 1], [Total Avg Cost Sel 2] )
VAR Dollar_Impact = Vol_Impact * AvgCost_Impact
RETURN
Dollar_Impact
)``````

Notes:

1. In this situation, the 2nd argument of SUMX should either consist of measures or be wrapped in CALCULATE (in order to trigger context transition so that State is applied as a filter). As far as I can see, all values calculated that would depend on the State filter are already measures so I think there is no need to add an explicit CALCULATE.
2. Also, I can see that you might want to consider declaring a few extra variables to avoid repeated calculation of some of the sub-measures.

Regards,

Owen

Owen Auger

9 REPLIES 9 Super User I

When you want a measure calculated at a certain granularity then summed, I would normally use SUMX.

The first argument of SUMX should be a table containing rows representing the granularity of the calculation (in your case State), and the second argument should be the calculation itself.

In your case, you have a couple of options:

1. Write another measure than sums the original measure by State, and use this new measure in your visuals:

``````Dol Impact Summed by State =
SUMX (
VALUES ( FactTable[State] ),
[Dol Impact]
)``````

2. Rewrite the original measure so that it sums by State:

``````Dol Impact =
SUMX (
VALUES ( FactTable[State] ),
VAR Vol1_Pct = [Volume Sel 1] / [Total Volume Sel 1]
VAR Vol2_Pct = [Volume Sel 2] / [Total Volume Sel 2]
VAR Pct_Diff = Vol1_Pct - Vol2_Pct
VAR Vol_Impact = Pct_Diff * [Total Volume Sel 1]
VAR AvgCost_Impact =
IF ( [Volume Sel 1] <> 0, [Avg Cost Sel 1], [Avg Cost Sel 2] )
- IF ( [Total Volume Sel 1] <> 0, [Total Avg Cost Sel 1], [Total Avg Cost Sel 2] )
VAR Dollar_Impact = Vol_Impact * AvgCost_Impact
RETURN
Dollar_Impact
)``````

Notes:

1. In this situation, the 2nd argument of SUMX should either consist of measures or be wrapped in CALCULATE (in order to trigger context transition so that State is applied as a filter). As far as I can see, all values calculated that would depend on the State filter are already measures so I think there is no need to add an explicit CALCULATE.
2. Also, I can see that you might want to consider declaring a few extra variables to avoid repeated calculation of some of the sub-measures.

Regards,

Owen

Owen Auger Helper II

@OwenAuger,
Thank you for your help! I used option #1and it works great. I was previously trying to do SUMX after the return in Dol Impact measure calculation and it didn't work. Maybe I used the state component from a different table (rookie mistake).

I have one more question, if you don't mind answering.  Instead of State details, what if I want to do State and Store details? What do I need to add/change when I calculate the total sum using SUMX? Super User I

That's good news 🙂

If you have State & Store defining your granularity, I would usually use SUMMARIZE to create a table of existing combinations and SUMX over that:

``````Dol Impact Summed by State =
SUMX (
SUMMARIZE( FactTable, FactTable[State], FactTable[Store] ),
[Dol Impact]
)``````

Notes:

• State & Store can be either in FactTable itself or in tables on the 1-side of a relationship with FactTable.
• Another option is to use nested SUMXs over each dimension in turn: SUMX ( VALUES(...), SUMX ( VALUES(...), [Dol Impact] ) )

Regards,

Owen 🙂

Owen Auger Helper II

@OwenAuger
Thanks again for helping. I used nested SUMX and it gave me "NaN" as a result. I guess there might be some calculation error somewhere.
On another hand, I have tried the SUMMARIZE solution you have provided, it gave me 0 as a total. Does this related to the "calculation error"?

Also, can you educate me on why do I need to create another measure to calculate Total Dollar? I tried adding the same SUMX statement after RETURN in the Dol Impact and it gave me 0 instead of the total. Helper II

Got the NaN error taking care of.

Some of the data were blank and PBI doesn't like it when I do "A / B". So, I used DIVIDE(A, B, 0) instead. Super User I

That's good 🙂
Is the SUMMARIZE method still giving a zero result?

To debug, I would create a table or matrix visual with both State & Store, plus the final measure using SUMMARIZE.

The individual measure values should sum to the total, and you will be able to see why they are offsetting to zero.

Owen Auger Helper II

Yes, the summerize is still giving me 0. I put this in a matrix table and it shows all 0.

Test =
SUMX(SUMMARIZE('Volume', 'Volume'[State], 'Volume'[Store]), 'Impact'[Dol Impact]) Super User I

Thanks, not sure what's causing that, something in the logic of the [Dol Impact] calculation.

Can you share a sample pbix file (with sanitised data if needed) that shows this problem?

Regards,

Owen

Owen Auger Helper II

Unfortunately, due to time crunch and data privacy, I can't share my original .pbix file and don't have enough time to replicate it with a mock data. The example is pretty much using the exact calculation I provided. As for now, the nested SUMX got the job done. I will look around for a the SUMMRIZE solution when I have time. Thank you for all of your help 🙂 Announcements #### Power Platform Community Conference

Check out the on demand sessions that are available now! #### Create an end-to-end data and analytics solution

Learn how Power BI works with the latest Azure data and analytics innovations at the digital event with Microsoft CEO Satya Nadella. Top Solution Authors
Top Kudoed Authors
Users online (2,241)