Forum Discussion
Calculating Percentage using Groupby
- 8 years ago
Anonymous
If you directly want a calculated table as an OUTPUT,,, then goto Modelling Tab>>New Table and use this formula
Table = SUMMARIZE ( TableName, TableName[Person], "%age of compliance", VAR totalCountforaperson = CALCULATE ( COUNT ( TableName[Person] ), ALLEXCEPT ( TableName, TableName[Person] ) ) VAR CountwithYes = CALCULATE ( COUNT ( TableName[Person] ), FILTER ( ALLEXCEPT ( TableName, TableName[Person] ), TableName[Compliance] = "Yes" ) ) RETURN DIVIDE ( CountwithYes, totalCountforaperson ) ) - 8 years ago
Hi Anonymous
The below calculated table will gget you pretty close
New Table = ADDCOLUMNS( SUMMARIZECOLUMNS( 'Table1'[Person], "Rows Total",COUNTROWS('Table1') , "Compliant",COUNTROWS(Filter('Table1','Table1'[Compliance]="Yes"))+0 ), "Ratio" , DIVIDE([Compliant],[Rows Total]) )
HI Anonymous
Try this
=
VAR totalCountforaperson =
CALCULATE (
COUNT ( TableName[Person] ),
ALLEXCEPT ( TableName, TableName[Person] )
)
VAR CountwithYes =
CALCULATE (
COUNT ( TableName[Person] ),
FILTER (
ALLEXCEPT ( TableName, TableName[Person] ),
TableName[Compliance] = "Yes"
)
)
RETURN
DIVIDE ( CountwithYes, totalCountforaperson )Hi Zubair, thanks for replying! Is the formula above supposed to return a table?
- Zubair_Muhammad8 years agoCommunity Champion
Hi,
No. Its a MEASURE.
If you put it in a TABLE VISUAL alongwith Names of Persons you will get the desired percentages
Do you need a calcuated table?
- Zubair_Muhammad8 years agoCommunity Champion
Anonymous
If you directly want a calculated table as an OUTPUT,,, then goto Modelling Tab>>New Table and use this formula
Table = SUMMARIZE ( TableName, TableName[Person], "%age of compliance", VAR totalCountforaperson = CALCULATE ( COUNT ( TableName[Person] ), ALLEXCEPT ( TableName, TableName[Person] ) ) VAR CountwithYes = CALCULATE ( COUNT ( TableName[Person] ), FILTER ( ALLEXCEPT ( TableName, TableName[Person] ), TableName[Compliance] = "Yes" ) ) RETURN DIVIDE ( CountwithYes, totalCountforaperson ) )- Anonymous8 years agoNot applicable
Thank you so much Zubair_Muhammad! I got the desired table :smileyhappy:
- Anonymous8 years agoNot applicable
Yes, I need a calculated table.
- Phil_Seamark8 years agoMicrosoft Employee
Hi Anonymous
The below calculated table will gget you pretty close
New Table = ADDCOLUMNS( SUMMARIZECOLUMNS( 'Table1'[Person], "Rows Total",COUNTROWS('Table1') , "Compliant",COUNTROWS(Filter('Table1','Table1'[Compliance]="Yes"))+0 ), "Ratio" , DIVIDE([Compliant],[Rows Total]) )- Anonymous8 years agoNot applicable
Hi Phil_Seamark, thanks for replying. I was wondering what's the +0 for in the formula? This seems to be neat solution :smileyhappy:
- Phil_Seamark8 years agoMicrosoft Employee
HI Anonymous,
The +0 just converts a blank cell to a 0 in this case. Take it out and see. :)
- Anonymous8 years agoNot applicable
Phil_Seamark I do have one question, though. Do you know if there's a way to accomplish this using groupby and filtering? I'm used to solving this kind of stuff using the groupby method of Pandas in Python, I'm just wondering if there's a same way in DAX.