Forum Discussion
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:
| IDs | Type | Step Status | Values |
| Chair | Light | Step 1 | -5 |
| Chair | Light | Step 2 | 5 |
| Chair | Light | Step 3 | 4 |
| Cloth | Light | Step 1 | -1 |
| Cloth | Light | Step 2 | 2 |
| Cloth | Light | Step 3 | 0 |
| Pencil | Light | Step 1 | -3 |
| Pencil | Light | Step 2 | 14 |
| Pencil | Light | Step 3 | 0 |
| Paper | Light | Step 1 | 6 |
| Paper | Light | Step 2 | 4 |
| Paper | Light | Step 3 | 4 |
| Tree | Heavy | Step 1 | 5 |
| Tree | Heavy | Step 2 | 6 |
| Tree | Heavy | Step 3 | 8 |
| Tree | Heavy | Step 3 | 8 |
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
- crln-bluePost Patron
Hello, I think I got this right but somehow I'm getting it also wrong?? Haha
- crln-bluePost Patron
Just closing this thread now and checking my data. Apologies!
- DimaMDSolution 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?