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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Weighted average of group after unpivot.

I am trying to make a weighted average based on the unpivoted data below, grouped by year and by region / year for other graphs.

 

first.GIF

 

These are the results I am trying to achieve for grouping by just year

 

expected.GIF

 

In order to achieve the end result. I believe PowerBI, behind the scenes, would need to see the table collapsed / groupedby like this:

 

behind.GIF

 

What is the best practice in dax for grouping unpivoted data for calculations?

 

The steps I attempted were to create all of this using a Measure with variables for the "occurences * pct" and "weight" columns as they are not necessary outside of the calculation. I was unable to successfully get the measure to work.

8 REPLIES 8
v-eachen-msft
Community Support
Community Support

Hi @Anonymous ,

 

Here is my sample data which is similar to yours.
2-1.PNG

Then you can use one measure to get your result.

Measure =
VAR a =
    SELECTEDVALUE ( 'Table'[pct] )
VAR b =
    SELECTEDVALUE ( 'Table'[occurence] )
VAR c =
    CALCULATE (
        SUMX ( 'Table', 'Table'[pct] * 'Table'[occurence] ) / 2,
        ALLEXCEPT ( 'Table', 'Table'[year] )
    )
RETURN
    ( a * b ) / c * a

Here is the result.
2-2.PNG

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.
Anonymous
Not applicable


@v-eachen-msft 

 

Why does this behavior happen?
 
/* Aggregates sum by year */
CALCULATE (
    SUMX( TABLE , pct ) / COUNTROWS()
    , ALLEXCEPT( TABLE / TABLE['YEAR'] )
)
 

/* Sums invidual rows */
CALCULATE (
    SUMX( TABLE , pct * occurence ) / COUNTROWS()
    , ALLEXCEPT( TABLE / TABLE['YEAR'] )
)
 
I cannot get the pct * occurence to aggregate by year so I can calculate an individual countries weight by year. Your solution works when the data is not repeated (due to unpivoting)

Hi @Anonymous ,

 

The data for each line of yours cannot be exactly the same, otherwise it will be aggregated in the visual. If you want to get the result like your second image, you need to add an index column to ensure that each row of data is unique.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.
Anonymous
Not applicable

@v-eachen-msft Adding an index column still causes the formula to aggregate on a row level and not based on year. What am I missing here? I am so puzzled.

Hi @Anonymous ,

 

I uploaded my test file, you can download and refer to it.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.
Anonymous
Not applicable

@v-eachen-msft Thank you for your help on this. I have included a sample of the dataset including all columns along with the measure you created to show an example of how it's not working.

weightedAVG.pbix on Google Drive - There's no attachment button for me on the forum.

Hi @Anonymous ,

 

Could you tell me what percentage of each country should appear in this sample data? Maybe the logic needs to be modified.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.
Anonymous
Not applicable

@v-eachen-msft 

 

The only columns of concern are country, year, pct, and occurence. The remaining columns are only there to show why there are so many repeats when unpivoted.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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