Code Generator | DAX

DAX Weighted Average Calculation

This input provides a DAX code snippet to calculate a weighted average using measures for different rows.


Empty image or helper icon

Prompt

In DAX write the code for Is it possible to have values in a weighted average calculation that are subtracted from the average rather than addded? The Table is Called WA TEST, For example: DEST AU TONNES HGSP 0.21 9 HGSP 0.12 4 MC 0.18 3 HGSP 0.23 8 MC 0.15 2 If the dest says HGSP the AU and Tonnes are multiplied and added to the average, but if the dest says MC the AU and tonnes are summed then subtracted from the average. The values would then be divided by the sum of the tonnes where any records with HGSP are added and any records with MC are subtracted.

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.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

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.