Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
djdonnel
Regular Visitor

Hierarchy Level Gives Incorrect Result for Weighted Average Calculation

I have a weighted average calculation that works properly at one level of granularity but not at the next level up. The result is correct on the chemical level e.g. 0.81 for Bisphenol A, but incorrect at the chemical class level e.g. 58.92 for Organic Parameters. The value of 58.92 is the sum of weighted averages for the chemicals in the class i.e. 58.92 = 0.10 + 20.00 + 0.05 + ... + 6.70. So it appears to be adding the weighted averages of each chemical rather than taking the average overall.

 

Capture.PNG

 

I have chemical results in a Results table and the weighting factor in a Samples table. The relationship columns are both named [Pool Number Key]. This is my DAX formula for the weighted average. 

 

=
CALCULATE (
    SUMX ( 'Results', [Results numeric] * RELATED ( 'Samples'[Weight] ) )
        / SUM ( 'Samples'[Weight] ),
    CROSSFILTER ( 'Results'[Pool Number Key], 'Samples'[Pool Number Key], BOTH )
)

If I pull out the lower level to just show Chemical Class it is wrong on the rows and wrong on the total. 

 

Capture2.PNG

 

EDIT: I have narrowed down the issue to this part of the formula.

SUM ( 'Samples'[Weight] )

At the chemical level, there are 24 samples for the region and the SUM ( 'Samples'[Weight] ) equals the sum of weights for 24 samples. At the class level the calculation is using SUM ( 'Samples'[Weight] ) for 24 samples again but it should be multiplied by 12 chemicals in the class (24 samples * weight for each of the 12 chemicals). The denominator in the weighted average calculation is off by a factor of how many chemicals are in the class. 

 

A manual check gives the right answer. 58.92 / 12 = 4.91 which is the correct weighted average for the Organic Parameters class.

2 REPLIES 2
v-jiascu-msft
Employee
Employee

Hi @djdonnel,

 

Can you share a dummy sample? The pbix file would be great. I think the DAX functions, like Summarize, Averagex, Hasonevalue, could help.

 

 

Best Regards,

Dal

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
djdonnel
Regular Visitor

 I have found a solution by replacing 

 

 

SUM ( 'Samples'[Weight] )

 

with

 

 

SUMX ( 'Samples', 'Samples'[Weight]*DISTINCTCOUNT( 'Results'[Chemical Name] )

 

 

The complete formula is

 

 

=CALCULATE (
    SUMX ( 'Results', [Results numeric] * RELATED ( 'Samples'[Weight] ) )
        / SUMX (
            'Samples',
            'Samples'[Weight] * DISTINCTCOUNT ( 'Results'[Chemical Name] )
        ),
    CROSSFILTER ( 'Results'[Pool Number Key], 'Samples'[Pool Number Key], BOTH )
)

 

Here's a comparison of the formula giving incorrect result vs. formula giving the correct result. I have also included the respective denominator calculation for the weighted average. As you can see, the SUM('Samples'[Weight]) result is the same at both levels of the hierarchy because the 'Samples' table is not being expanded by the number of chemicals in the class. So each sample's weight is being included only once when it should be included 12 times (in the case of the Organic Parameters class). Changing the formula to SUMX('Samples','Samples'[Weight]*DISTINCTCOUNT('Results'[Chemical Name])) effectively arrives at the correct value.


Is there a better way to achieve the desired result?

 

Capture3.PNG

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.