问题
I have rather complex problem to expres. In DAX powerpivot I am trying to create measure which will be using two different Weighted averages in one measure based on aggregation level.
The problem is complicated even more, because weight measures have different level of duplication (need to apply distinct SUM on them).I have been able to create Distinct SUM Measure1 and 2 to solve that.
[Weight_1] = SUMX(DISTINCT(Table1[Level2],[SupportWeight1])
[SupportWeight1] = MAX(Table1[RevenueLevel2])
[Weight_2] = SUMX(DISTINCT(Table1[Level3],[SupportWeight2])
[SupportWeight1] = MAX(Table1[RevenueLevel3])
So far so good, It was necessary because as you will see in below example, both measures need to be "deduplicated" during aggregation.
Weight_1
is Unique per Level2 dimension and Weight 2 is unique on higher level, per Level3 dimension.
After that I wanted to create Weighted average utilizing Weight_1, creating new supporting column:
[Measure x Weight_1] = [Measure] * [Weight_1]
I have forgot to mention, Weights are unique on Higher granularity (Level2 and Level3), however [Measure]
is unique on lowest granularity: Level1
Now I had everything to create First weighted average:
[Measure Weight_1] = SUMX(Table1,[Measure x Weight_1])/SUMX(Table1,[Weight_1])
I t was done and works as expected.
Tricky part started now. I was thinking that simply creating one next support column and final measure I will accomplish "Final weighted" measure. And it will behave as expected different way on Level 1,2,3 and different on Level4,5,6,...
So I have use [Measure Weight_1] and create:
[Measure Weight_1 x Weight_2] = [Measure Weight_1] * [Weight_2]
Consequently Final measure:
[Measure Weight_2 over Weight_1] =SUMX(Table1, [Measure Weight_1 x Weight_2] )/SUMX(Table1,[Weight_2])
However it does not work obviously those measure are on different granularities and they do not comes together during aggregation. In final measure issue is that Level3 aggregation is arithmetical average not weighted average, but I am expecting to get there same result as in [Measure Weight_1]
. Simply Because weight #2
has same value Lvel 3,2,1.
Consequently something like this would be probably treated in MDX with on Focus function.
Maybe the issue is column [Measure Weight_1 x Weight_2]
maybe i need to aggregate first also this.
Maybe it could be accomplished with ROLLUP functions but I am not certain how to write it.
I am stuck here.
Try to rewrite programmatically my desired solution:
Weighted Average of Measure X =
IF Dim = Level1 Then Measure
IF Dim = Level2 Then AVG(Measure)
IF Dim = Level3 Then SUM(AVG(Measure)*Weight_1) / SUM(Weight_1)
IF Dim = Level4 Then SUM(SUM(AVG(Measure)*Weight_1) / SUM(Weight_1) * Weight_2) / SUM(Weight_2)
来源:https://stackoverflow.com/questions/42542505/dax-2-phased-weighted-average-with-2-different-weight-measures