Forum Discussion
Calculate Percentages
How can I calculate the average of percentage grouped by category based on a criteria?
In this case, the grouping is by "Machine" and the percentage is number of failed products to total products by machine. Then the average of percentage of failed products over all machines.
My current formula:
Data aka "Table":
| Machine | Quality | Volume |
| 1 | Fail | 5 |
| 1 | OK | 7 |
| 1 | Good | 2 |
| 2 | Fail | 4 |
| 2 | OK | 5 |
| 2 | Good | 3 |
| 3 | OK | 1 |
| 3 | Good | 8 |
What I want:
| Machine | % Failed |
| 1 | 0.36 |
| 2 | 0.33 |
| 3 | 0.00 |
| Average | 0.23 |
The issue is that since Machine 3 did not produce any failed items, it gets "filtered out". How can I make sure machine 3 gets included? That is, the final calculation should be (0.36+0.33+0)/3 = 0.23, rather than (0.36+0.33)/2 = 0.35.
Try
%Failed = AVERAGEX( KEEPFILTERS(VALUES('Table'[Machine])), DIVIDE( CALCULATE(SUMx('Table',if('Table'[Quality] = "Fail",'Table'[Volume],0))) , CALCULATE(SUM('Table'[Volume])) ) )pbix : https://www.dropbox.com/s/an0ry8twt8m9hrw/machineData.pbix?dl=0
Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks.
My Recent Blog - https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601
4 Replies
- amitchandakSuper User
Try
%Failed = AVERAGEX( KEEPFILTERS(VALUES('Table'[Machine])), DIVIDE( CALCULATE(SUMx('Table',if('Table'[Quality] = "Fail",'Table'[Volume],0))) , CALCULATE(SUM('Table'[Volume])) ) )pbix : https://www.dropbox.com/s/an0ry8twt8m9hrw/machineData.pbix?dl=0
Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks.
My Recent Blog - https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601- AnonymousNot applicable
Thanks amitchandak, adding the IF made it work like a charm!
- v-eachen-msftCommunity Support
Hi Anonymous ,
You could refer to the following DAX:
Measure = VAR a = CALCULATE ( ( CALCULATE ( SUM ( 'Table'[Volume] ), FILTER ( 'Table', 'Table'[Quality] = "Fail" ) ) + 0 ) / SUM ( 'Table'[Volume] ), ALLEXCEPT ( 'Table', 'Table'[Machine] ) ) RETURN aHere is the result.
- AnonymousNot applicable
This works in this regard, but I think amitchandak 's solution is more flexible, since I can pull in other filters to further slice-and-dice the data as needed.