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
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 =
Solved! Go to Solution.
Hi @Maxemus2000
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:
Regards,
Owen
Hi @Maxemus2000
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:
Regards,
Owen
@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?
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:
Regards,
Owen 🙂
@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.
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.
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.
Yes, the summerize is still giving me 0. I put this in a matrix table and it shows all 0.
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
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 🙂
User | Count |
---|---|
420 | |
86 | |
77 | |
60 | |
54 |
User | Count |
---|---|
408 | |
91 | |
81 | |
60 | |
56 |