Prompt
Answer
To achieve this, we'll need to create a few measures. We'll start with one to calculate the summed product for 'HGSP' rows, another for 'MC' rows, and finally a measure to subtract the 'MC' result from the 'HGSP' result. We'll also need measures to calculate the total of 'Tonnes' for both 'HGSP' and 'MC'.
Firstly, before we start, it's important to bear in mind that DAX implicitly handles row context. Therefore, in our measures, we'll use the CALCULATE
and SUMX
functions to ensure that our row-level calculation is done correctly.
Let's create the measures:
// Measure to calculate product sum for 'HGSP'
HGSP Sum = CALCULATE(
SUMX('WA TEST', 'WA TEST'[AU] * 'WA TEST'[TONNES]),
'WA TEST'[DEST]="HGSP"
)
// Measure to calculate product sum for 'MC'
MC Sum = CALCULATE(
SUMX('WA TEST', 'WA TEST'[AU] * 'WA TEST'[TONNES]),
'WA TEST'[DEST]="MC"
)
// Measure to calculate total of 'Tonnes' for 'HGSP'
HGSP Tonnes = CALCULATE(
SUM('WA TEST'[TONNES]),
'WA TEST'[DEST]="HGSP"
)
// Measure to calculate total of 'Tonnes' for 'MC'
MC Tonnes = CALCULATE(
SUM('WA TEST'[TONNES]),
'WA TEST'[DEST]="MC"
)
// Weighted Average calculation
Weighted Average = DIVIDE(
[HGSP Sum] - [MC Sum],
[HGSP Tonnes] - [MC Tonnes]
)
This code would result in the desired weighted average calculation, with 'HGSP' contributing positively and 'MC' contributing negatively.
As an analyst at Enterprise DNA, the best way to learn about DAX formulas and such dynamic calculations would be to enroll in an appropriate course available on the Enterprise DNA's Platform.
Description
This input explains how to create DAX measures to calculate a weighted average in Power BI or other DAX-supported tools. The code snippet demonstrates the creation of measures to calculate the summed product for rows with 'HGSP' and 'MC' destinations, as well as measures for calculating the total 'Tonnes' for both destinations. The weighted average is then calculated by subtracting the 'MC' result from the 'HGSP' result and dividing it by the difference in total 'Tonnes'. This input also mentions the importance of understanding DAX's handling of row context and suggests enrolling in suitable courses on the Enterprise DNA platform for further learning.