Forum Discussion

katbro20's avatar
katbro20
Frequent Visitor
1 year ago
Solved

Weighted Average where weights are dynamic based on slicer

Hello,

 

I'm quite new to power BI and so hopefully I've posted this in the correct place. I'm trying to get the weighted average of a quarterly development % list weighted by sales premium, and I want the weights to be dynamic based based on what is selected in the slicer.

 

My sales data looks like this (this is not complete), and so there is a region, country/state, and product type associated with every sale premium.

 

There are then two seperate tables which contain quarterly development list (again not complete) for every product type,

and for every country/state.

 

What I would like is to get a weighted average of the development pattern based on the sales premium (at the moment then development patterns are just showing simple average). The bit that I'm really struggling with is that I would like the ability to select multiple values from the slicers (eg select france and germany, and then have the weighted average of the  country/state development patterns just based on the premiums from those two countries).

 

I would then do this seperately for the other product type development pattern table based on the product type slicer (again with the ability to base the weights % on multiple selected values)

 

The last part would be to show the weighted average of both types of development patterns (the country/state and the product type) on the same graph, but I think if I have my relationships correct, that should be easy.

 

This is a simplified version of what I'm trying to do, and so if explanations are needed then I will definately put some more color around the problem, just send post asking what I am missing!

 

Thank you very much!

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi katbro20 ,

     

    Best Regards,
    Wenbin Zhou

5 Replies

  • katbro20's avatar
    katbro20
    Frequent Visitor

    Posting data as tables for ease instead of pictures

    SalesIDRegionCountry/StateProduct TypeSalesPremium
    1USNew YorkA200
    2USLAA300
    3USNew YorkA400
    4USLAB500
    5USLAB600
    6USLAB700
    7USLAB800
    8USBostonB900
    9USBostonB1000
    10USNew YorkD1100
    11USNew YorkD1200
    12USBostonD1300
    13EuropeUKB1400
    14EuropeUKB1500
    15EuropeUKB1600
    16EuropeFranceB1700
    17EuropeFranceC1800
    18EuropeFranceC1900
    19EuropeFranceC2000
    20EuropeGermanyC2100
    21EuropeGermanyD2200
    22EuropeGermanyD2300
    23EuropeUKD2400
    24EuropeUKD2500
    25EuropeSpainA2600
    26EuropeSpainA2700
    27EuropeSpainA2800
    28EuropeSpainB2900
    29EuropeSpainB3000
    30EuropeFranceB3100
    31EuropeFranceB3200
    32EuropeGermanyC3300
    33EuropeGermanyC3400
    34EuropeGermanyC3500
    35EuropeGermanyC3600
    36EuropeGermanyD3700
    37EuropeUKD3800
    38EuropeUKD3900
    39EuropeUkD4000

     

     

    Country/State Development Table

    Country/StateQuarterDevelopment
    New York10.05
    New York20.1
    New York30.15
    New York40.2
    New York50.25
    New York60.3
    New York70.35
    New York80.4
    New York90.45
    New York100.5
    Boston10.02
    Boston20.045
    Boston30.07
    Boston40.095
    Boston50.12
    Boston60.145
    Boston70.17
    Boston80.195
    Boston90.22
    Boston100.245
    LA10.1
    LA20.2
    LA30.3
    LA40.4
    LA50.5
    LA60.6
    LA70.7
    LA80.8
    LA90.9
    LA101
    France10.07
    France20.14
    France30.21
    France40.28
    France50.35
    France60.42
    France70.49
    France80.56
    France90.63
    France100.7
    UK10.05
    UK20.1
    UK30.15
    UK40.2
    UK50.25
    UK60.3
    UK70.35
    UK80.4
    UK90.45
    UK100.5
    Germany10.07
    Germany20.14
    Germany30.21
    Germany40.28
    Germany50.35
    Germany60.42
    Germany70.49
    Germany80.56
    Germany90.63
    Germany100.7
    Spain10.02
    Spain20.045
    Spain30.07
    Spain40.095
    Spain50.12
    Spain60.145
    Spain70.17
    Spain80.195
    Spain90.22
    Spain100.245

     

    Product Type Development Table

    Product TypeQuarterDevelopment
    A10.05
    A20.1
    A30.15
    A40.2
    A50.25
    A60.3
    A70.35
    A80.4
    A90.45
    A100.5
    B10.02
    B20.045
    B30.07
    B40.095
    B50.12
    B60.145
    B70.17
    B80.195
    B90.22
    B100.245
    C10.1
    C20.2
    C30.3
    C40.4
    C50.5
    C60.6
    C70.7
    C80.8
    C90.9
    C101
    D10.07
    D20.14
    D30.21
    D40.28
    D50.35
    D60.42
    D70.49
    D80.56
    D90.63
    D100.7
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi katbro20 ,

      How is the weighted average calculated?' Is 'Development' your weight column?
      Like this?

      If that is the case, this is the result I obtained

      Product = 
      VAR _quarter = SELECTEDVALUE('Quarter'[Value])
      VAR _table = ADDCOLUMNS(SUMMARIZE('SalesData',[Product Type],"Sum",SUM(SalesData[SalesPremium])),"result",VAR _type = [Product Type] RETURN MAXX(FILTER('Product Type Development Table',[Product Type] = _type && [Quarter] = _quarter),[Development]) * [Sum])
      VAR _a = SUMX(_table,[result])
      VAR _b = CALCULATE(SUM('Product Type Development Table'[Development]),USERELATIONSHIP('Quarter'[Value],'Product Type Development Table'[Quarter]))
      RETURN DIVIDE(_a,_b)

      For quarter1 :
      1928.57 = (900 * 0.05) + (4500 * 0.02) / (0.05 + 0.02)

       

      If I have misunderstood, please let me know the steps of the calculation and the expected results

       

      Best Regards,
      Wenbin Zhou

      • katbro20's avatar
        katbro20
        Frequent Visitor

        Hi Anonymous ,

         

        No the developments are the things that I want weighted. Ie I would like a total development column by quarter, weighted by the premium (in the sales data) for each product type. (and for each country/state seperately if selected)

         

        So very simply, if, for example, the slicer were to select product type A and B, I would like the weight for A in the Product Type Development Table to be the total premium for A (in the selected region) divided by the total premium for both product type A and B (in the selected region). So for example, if the US was selected:

         

         

        These weights would then be applied to every value in the development column for product type A and B respectively.

        The final result would then be the sum of the weighted development column for each quarter.

        Hopefully this is clearer, but let me know if not, thanks