Forum Discussion

crln-blue's avatar
crln-blue
Post Patron
2 years ago
Solved

Imitate Excel SumProduct

Hello everyone!

 

I've been looking for posts related to Excel's sumproduct and most of the results yielded to using SUMX and in its argument, multiply the tables.

 

I've tried it but the reults is not what I am expecting...

 

What I'm planning is to imitate this to Power BI:

The Total is a weighted average by using SUM PRODUCT on the Step Status (for example, Step 1) and # of Unique IDs (Light and Heavy) and then dividing the output to the totals. My problem is making the SUMPRODUCT on power bi..

 

Here is my data:

IDsTypeStep StatusValues
ChairLightStep 1-5
ChairLightStep 25
ChairLightStep 34
ClothLightStep 1-1
ClothLightStep 22
ClothLightStep 30
PencilLightStep 1-3
PencilLightStep 214
PencilLightStep 30
PaperLightStep 16
PaperLightStep 24
PaperLightStep 34
TreeHeavyStep 15
TreeHeavyStep 26
TreeHeavyStep 38
TreeHeavyStep 38

 

I'm trying to use the SUMX function for my weighted average but I think it gets the totals for each of the arguments and uses the SUMX..

 

 

 

 

W AVG = 
VAR _avg = AVERAGE('Table (2)'[Values 2])

VAR _w_avg = SUMX('Table (2)', _avg * [unique ids])

return _w_avg

 

 

 

 

Because I cannot drag the measure for avg weight on totals just like in the Excel, I'm planning to place the Average Weight measure on a different matrix table.. maybe like this:


I'm thinking of creating maybe a mini evaluation table as a results on the variables on my Measure and then apply the SUMX function on the variables (just like excel does) but I'm not sure how.. I'm creating a Measure because the Measures adjusts accordingly based on the filters that the user will set on the report.

 

What would be the correct way in getting my Weighted Average? 

 

Thanks a bunch!

 

I attached a sample pbix file with sample data that captures my scenario. Thank you for all the help! ♥

https://drive.google.com/file/d/1qJXucYygYUsLJJI5R67c23NH1BtPszXJ/view?usp=sharing

Edit: I just realized that I'm getting right with my sample data but somehow getting it wrong on other data?? 

Edit 2: I edited this post because my DAX is calculating the correct weighted average with my sample data. I used another sample data and now I was able to replicate the issue.

 

Apologies for the confusion.

 

 

 

  • Hello, I think I got this right but somehow I'm getting it also wrong?? Haha

     

3 Replies

  • Hello, I think I got this right but somehow I'm getting it also wrong?? Haha

     

  • Just closing this thread now and checking my data. Apologies!

     

  • DimaMD's avatar
    DimaMD
    Solution Sage

    Hi crln-blue  The first step is to count the correct number of IDs

    # of unique IDs = 
    CALCULATE( 
        COUNT('Table'[IDs]), 'Table'[Values] > 0
        )

    Second step in VAR _result add ALLEXCEPT also for correct summary you can use ISINSCOPE looks at given measure

    Weighted Average = 
    VAR _weight_avg = 
        SUMX( 'Table', 'Table'[Values] * [# of unique IDs])
            
    VAR _result = DIVIDE(_weight_avg, CALCULATE([# of unique IDs],ALLEXCEPT('Table','Table'[Type])))
    RETURN 
    IF( ISINSCOPE('Table'[Type]),[Test Wt Avg],_result)

    is this your desired outcome?