Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
LipiSingal
Frequent Visitor

Looking for DAX formula to calculate percentage per category for a year for a multiple choice ques

I have done almost everything but not able to achieve the desired solution.

I have two surveys for 2020 and 2021 which have multiple-choice questions.

 

So there are two tables:

Table 1:

Survey ID respondentWeightageSurvey yearAttractive region
50512021Western Europe
50512021Central & Eastern Europe
50512021Northern America
50612021Western Europe
50612021Other Asia
50612021Oceania
50712021Western Europe
50712021Central & Eastern Europe
50712021Northern America
50812021Central & Eastern Europe
50812021Japan
50812021Other Asia
10.72522922020Central & Eastern Europe
10.72522922020Western Europe
10.72522922020Can't say
20.74704682020Central & Eastern Europe
20.74704682020Middle East
20.74704682020Northern Africa
30.56614242020Western Europe
30.56614242020Northern America
30.56614242020Middle East
41.41035182020Other Asia
41.41035182020China
41.41035182020Latin America
51.14075952020Northern America
51.14075952020China
51.14075952020Western Europe

 

Table 2:

Survey ID respondentWeightageSurvey year
50512021
50612021
50712021
50812021
10.72522922020
20.74704682020
30.56614242020
41.41035182020
51.14075952020

 

Since it's a multiple-choice question, I want to calculate the percentage of respondents who chose e.g. western Europe as an attractive region based on total number of respondents for a survey. For this, I have linked the above two charts using Survey ID.

 

I want a measure to calculate the last column in below table 3:

 Weightages Percentage I am getting from my measurePercentage I want (required measure)
Attractive region  2020202120202021
China2.5511113 28%0%51%0%
Western Europe2.4321311327%33%49%75%
Northern America1.7069019219%22%34%50%
Central & Eastern Europe1.472276316%33%29%75%
Other Asia1.4103518216%22%28%50%
Latin America1.4103518 16%0%28%0%
Middle East1.3131892 15%0%26%0%
Northern Africa0.7470468 8%0%15%0%
Can't say0.7252292 8%0%15%0%
Japan 10%11%0%25%
Oceania 10%11%0%25%
Total13.768589112    

 

For eg. for Western Europe, 3 respondents in 2021 survey have marked it as a attractive region. Now the percentage I want is 3 divided by 4 (respondents in 2021 survey). The percentage I am getting is 3/9 (respondents in 2020+2021 survey)

 

This is the DAX I am using:

 

Denominator = calculate(sum('Table 2'[Weightage]),allexcept('Table 2',Table 2[Survey year]))
%category = divide(sum('Table 1'[Weightage]),Denominator,0)
 
Please suggest the change in DAX function to require desired percentage. Since its a multiple choice question I am facing issues. I really need this to be resolved, please suggest what can be done. Thankyou.
1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi  @LipiSingal ,

Here are the steps you can follow:

Regarding your first question:

Required percentage

1. Create measure.

Measure =
var _countgroup=
CALCULATE(COUNTROWS('Table'),FILTER(ALL('Table'),'Table'[Survey year]=MAX('Table'[Survey year])&&'Table'[Attractive region]=MAX('Table'[Attractive region])))
var _countall=
CALCULATE(COUNTROWS('Table2'),FILTER(ALL('Table2'),'Table2'[Survey year]=MAX('Table'[Survey year])))
return
DIVIDE(_countgroup,_countall)

2. Result:

v-yangliu-msft_0-1622103835217.png

Regarding your second question:

Measure %= If( year= 2020, denominator to be sum of weightages from 2020, else denominator to be sum of weighatges from 2021)

1. Create measure.

Measure 2 =
var _select=SELECTEDVALUE('Table'[Survey year])
var _countgroup=CALCULATE(COUNTROWS('Table'),FILTER(ALL('Table'),'Table'[Survey year]=MAX('Table'[Survey year])&&'Table'[Attractive region]=MAX('Table'[Attractive region])))
var _selectweight=CALCULATE(SUM('Table'[Weightage]),FILTER(ALL('Table'),'Table'[Survey year]=_select))
return
DIVIDE(_countgroup,_selectweight)

2. Use [Survey year] as the slicer, when the slicer is 2020, the denominator is the sum of [Weight] in 2020, and when the slicer is 2021, the denominator is the sum of [Weight] in 2021

3. Result:

v-yangliu-msft_1-1622103835222.png

 

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-yangliu-msft
Community Support
Community Support

Hi  @LipiSingal ,

Here are the steps you can follow:

Regarding your first question:

Required percentage

1. Create measure.

Measure =
var _countgroup=
CALCULATE(COUNTROWS('Table'),FILTER(ALL('Table'),'Table'[Survey year]=MAX('Table'[Survey year])&&'Table'[Attractive region]=MAX('Table'[Attractive region])))
var _countall=
CALCULATE(COUNTROWS('Table2'),FILTER(ALL('Table2'),'Table2'[Survey year]=MAX('Table'[Survey year])))
return
DIVIDE(_countgroup,_countall)

2. Result:

v-yangliu-msft_0-1622103835217.png

Regarding your second question:

Measure %= If( year= 2020, denominator to be sum of weightages from 2020, else denominator to be sum of weighatges from 2021)

1. Create measure.

Measure 2 =
var _select=SELECTEDVALUE('Table'[Survey year])
var _countgroup=CALCULATE(COUNTROWS('Table'),FILTER(ALL('Table'),'Table'[Survey year]=MAX('Table'[Survey year])&&'Table'[Attractive region]=MAX('Table'[Attractive region])))
var _selectweight=CALCULATE(SUM('Table'[Weightage]),FILTER(ALL('Table'),'Table'[Survey year]=_select))
return
DIVIDE(_countgroup,_selectweight)

2. Use [Survey year] as the slicer, when the slicer is 2020, the denominator is the sum of [Weight] in 2020, and when the slicer is 2021, the denominator is the sum of [Weight] in 2021

3. Result:

v-yangliu-msft_1-1622103835222.png

 

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

LipiSingal
Frequent Visitor

@Greg_Deckler Thanks for your response. I tried your suggestion and it works fine for the 2021 survey. For a moment I thought its working fine but there was a slight difference in the numbers.

 

To explain:

In my original survey 2020 survey has 504 respondents, 2021 has 550 respondents. 

 This variable : ___Denominator seems to be calculating 550 as the denominator for both years. 

So in my graph, the percentage for 2020 is also coming as xxx/550, whereas it should be xxx/504. The percentage for 2021 is absolutely correct and thank you very much for this. 

 

Another issue: I have filters on my main dashboard where people can filter the survey results based on revenue, nationality etc. After using this measure, the filters don't seem to be working on this graph. Other graphs are changing based on the filters chosen, but not this one.

 

Looking forward to your reply. Thankyou.

I am not aware with DAX a lot, I mostly google the various codes and try for my purpose. I had something like this in mind, but I only know how to do this in Excel, can anyone help me convert it to DAX for PBI and can something like this work?

 

Measure %= If( year= 2020, denominator to be sum of weightages from 2020, else denominator to be sum of weighatges from 2021)

 

So basically, if we can add a condition to calculate the denominator variably, this might help us solve this. 

In other words- this is just one of those scenarios where we have data for a multiple choice question from two years, and we want to divide the total of these by unique respondents from the respective year.

Greg_Deckler
Super User
Super User

@LipiSingal Perhaps try:

Measure 2 = 
    VAR __Year = MAX('Table 1'[Survey year])
    VAR __Denominator = COUNTROWS(SUMMARIZE(FILTER(ALL('Table 1'),[Survey year]=__Year),[Survey ID respondent]))
RETURN
    DIVIDE(SUM('Table 1'[Weightage]),__Denominator)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.