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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
poweruser124
New Member

Weighted Average by Distinct Category

Hello PBI Community,

 

I'm fairly new to DAX and am trying to write a DAX formula to calculated weighted average by distinct category as follows:

 

The table structure is this:

Row ID (Key)__Category___Place Name__Total Sample__Average Size within Sample  
123AIndiana140014
232AOhio140014
111BNew Jersey30030

 

Values for Total Sample and Average Size within Sample columns are grouped by category (i.e each rows with the same category will have the same value for those 2 columns). I am trying to calculate the folowing weighted average by distinct category.  For the data above, the result I would want is below:

 

[ (1400 x 14) + (300 x 30) ] / (1400+300) = 16.82 (This is the desired result). In a slicer if Place Name Indiana, Ohio and New Jersey are selected, I would want this value. The result will also be same if only Indiana and New Jersey are selected. If only Indiana and Ohio are selected (places with same category), I would not want to do any aggregation and only display the calucation as 14.

 

I have created a DAX formula below that does not take into account the distinct category which gives the below:

 

DIVIDE(
SUMX('Table','Table'[Total Sample]*'Table'[Average Size within Sample]),
SUM('Table'[Total Sample]))

 

Translates to:

[(1400 x 14) +(1400 x 14) + (300 x 30)] / [ (1400+1400+300)] = 15.54 (This is not the desired result)

 

Any help that can be suggested to modify the formula to get the desired result will be appreciated.

 

Thank you so much.

 

 

 

 

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@poweruser124 , Try a measure like

 

Var _tab= Summarize(Table,[Total Sample],Table[Average Size within Sample])
return
divide(Sumx(_tab, [Total Sample] * Table[Average Size within Sample]), Sumx(_tab, [Total Sample]))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

2 REPLIES 2
poweruser124
New Member

Thank you @amitchandak , this worked. Much appreciated!

amitchandak
Super User
Super User

@poweruser124 , Try a measure like

 

Var _tab= Summarize(Table,[Total Sample],Table[Average Size within Sample])
return
divide(Sumx(_tab, [Total Sample] * Table[Average Size within Sample]), Sumx(_tab, [Total Sample]))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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