Forum Discussion

poweruser124's avatar
poweruser124
New Member
3 years ago
Solved

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.

 

 

 

 

 

  • 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]))

2 Replies

  • 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]))