Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Weighted Average by Category

Hello, 

 

I need assistance calculating 4 different weighted averages by category for Spread to Libor - weighted average by Quarter Year, weighted average by Tenor Year, weighted average by Quarter, and weighted average by Year. Is the best way to calucate in the Power Query Editor or in a measure in the visualization itself? In excel, I would just normal SUMPRODUCT(Amount*Spread to Libor)/SUM(Amount) for each of the different category groups i.e. 1995 or 4|1995 but I can't seem to figure out the conditional weighted average in DAX. I have included a sample of the data below.

 

Thanks for your help!

 

Issue YearIssue QuarterTenorSpread to LiborAmountQuarter YearTenor Year
1995Q14208500000001Q19954|1995
1995Q1210500000001Q19952|1995
1995Q227.5500000002Q19952|1995
1995Q11-3500000001Q19951|1995
1996Q320500000003Q19962|1996
1996Q31-2500000003Q19961|1996
1996Q310500000003Q19961|1996
1996Q110500000001Q19961|1996
1996Q11-283500000001Q19961|1996
1996Q11-5575000001Q19961|1996
1997Q12-2600000001Q19972|1997
1997Q11-5677000001Q19971|1997
1997Q120700000001Q19972|1997
1997Q21-5500000002Q19971|1997
1997Q21-5500000002Q19971|1997
1997Q21-5500000002Q19971|1997
1997Q228.6600000002Q19972|1997
1997Q221500000002Q19972|1997
1997Q235.5500000002Q19973|1997
1997Q21-3500000002Q19971|1997
1997Q347.55500000003Q19974|1997
1997Q332.2500000003Q19973|1997
1997Q336.2500000003Q19973|1997
1997Q335500000003Q19973|1997
  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi Anonymous,

    You can try to use the following measure formulas:

    AVG by QY =
    CALCULATE (
        AVERAGE ( Table[Spread to Libor] ),
        ALLSELECTED ( Table ),
        VALUES ( Table[Quarter Year] )
    )
    
    AVG by TY =
    CALCULATE (
        AVERAGE ( Table[Spread to Libor] ),
        ALLSELECTED ( Table ),
        VALUES ( Table[Tenor Year] )
    )
    
    AVG by IQ =
    CALCULATE (
        AVERAGE ( Table[Spread to Libor] ),
        ALLSELECTED ( Table ),
        VALUES ( Table[Issue Quarter] )
    )
    
    AVG by IY =
    CALCULATE (
        AVERAGE ( Table[Spread to Libor] ),
        ALLSELECTED ( Table ),
        VALUES ( Table[Issue Year] )
    )

    Regards,
    Xiaoxin Sheng

2 Replies

  • Try like

    divide(SUMX(table,table[Amount]*table[Spread to Libor]),SUM(table[Amount]) )

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous,

    You can try to use the following measure formulas:

    AVG by QY =
    CALCULATE (
        AVERAGE ( Table[Spread to Libor] ),
        ALLSELECTED ( Table ),
        VALUES ( Table[Quarter Year] )
    )
    
    AVG by TY =
    CALCULATE (
        AVERAGE ( Table[Spread to Libor] ),
        ALLSELECTED ( Table ),
        VALUES ( Table[Tenor Year] )
    )
    
    AVG by IQ =
    CALCULATE (
        AVERAGE ( Table[Spread to Libor] ),
        ALLSELECTED ( Table ),
        VALUES ( Table[Issue Quarter] )
    )
    
    AVG by IY =
    CALCULATE (
        AVERAGE ( Table[Spread to Libor] ),
        ALLSELECTED ( Table ),
        VALUES ( Table[Issue Year] )
    )

    Regards,
    Xiaoxin Sheng