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
Mike22
Helper III
Helper III

Calculate percentage at hierarchy level

 

 

Hello Friends, 

 

I am trying to do a very simple measure but I am really struggling, I searched everywhere but cannot find a solution. What I am trying to achieve is a measure to use as a column on a hierarchy to calculate the percentage of each value in the hierarchy. 

 

I tried the following but it does not seem to work. 

 

Percentage =


var totalgender = CALCULATE(DISTINCTCOUNT(Stafflist[personnelnumb.]),ALLSELECTED(Stafflist[Gender]))

 

var totalstaff= CALCULATE(DISTINCTCOUNT(Stafflist[personnelnumb]),ALLEXCEPT(Stafflist,Stafflist[personnelnumb]))

return

totalgender/totalstaff


what I am struggling with is the totalstaff counting the staff only in the hierarchy 

 

Percentage has to show 48% for Female and 52% for male in level 4 and 38% for male and 62%for Female in level 3. The measure has to be dynamic in order to update as slicers are filtering and reducing the number of staff on the page. Below is the results I am getting instead. 

Level 4Level 4
image.png

Would appreaciate if anyone can point me in the right direction. 

Thanks,

Mike

 
 
 
1 ACCEPTED SOLUTION
dax
Community Support
Community Support

Hi Mike22,

I am sorry, I didn't take your filter into consideration previously. Now you could try below to see whether it work or not.

 

Measure 3 = if(ISFILTERED(Entity[Entity]),COUNT(T2[Person Number])/CALCULATE(COUNT(T2[Person Number]),filter( ALLSELECTED(T2), T2[Level]=MIN(T2[Level]) && T2[Entity]=MIN(Entity[Entity]))),COUNT(T2[Person Number])/CALCULATE(COUNT(T2[Person Number]),filter( ALLSELECTED(T2), T2[Level]=MIN(T2[Level]) )))

 

 

Best Regards,
Zoe Zhi

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

18 REPLIES 18
Anonymous
Not applicable

Hi @Mike22 ,

Kindly use the below measue to achive your request.

Measure:
Percent =
VAR Total = CALCULATE(SUM('Sample'[Staff]),ALL('Sample'),VALUES('Sample'[Level]))
RETURN
CALCULATE(DIVIDE(SUM('Sample'[Staff]),Total,0))

Output:
Capture.PNG


Best Regards,
Mail2inba4

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

Anonymous
Not applicable

Hi,

we call your table staff, with Gender, Level and Persons columns.

You could create these measures for your first target:

 

# Persons = SUM(Staff[Persons])

# Level Persons = CALCULATE([# Persons],ALLEXCEPT(Staff,Staff[Level]))
# Total Persons = CALCULATE([# Persons],ALL(Staff))
% of Level = IF( ISINSCOPE(Staff[Gender]), DIVIDE([# Persons], [# Level Persons]))
% of Total = DIVIDE([# Persons], [# Total Persons])
 
This could be a starting point because if you want manage other filters and slicers you have to work on the ALLEXCEPT.
 
Regards
 
Lorenzo

Hi Lorenzo @Anonymous , 

Thank you for the suggestion but it does not work. Just sharing below the dataset to facilitate.

Table = Stafflist

Person NumberGenderLevel
1M3
2F3
3F3
4F4
5M4
6M3
7M4
8F3
9F4
10M3

 

The matrix rows are level and Gender. The columns are the count of person number and the measure I am trying to create to give me the percentage. 

Hope this can clarify. 


@parry2k  Thanks for replying, above is the dataset. 

 

Thanks. 

Mike

Anonymous
Not applicable

Hi @Mike22 ,

Person Number is the number of the persons or a PersonID.

This doesn't make great difference for my solution but I would know to better fit my sample.

I verify my solution and it's correct.

Using your data assuming Person Number as number of persons:

 

powerbi_perc_of_parent.png

But this one works also assuming Person Number as PersonID.

Look at my sample: Percentage of Parent 

 

Regards

Lorenzo

dax
Community Support
Community Support

Hi Mike22,

You could refer to my sample to see whether it work or not.

Best Regards,
Zoe Zhi

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

Anonymous
Not applicable

Hi @dax ,

our solutions are the same!

 

👍

 

Lorenzo

 

@dax @Anonymous 

Thank you both, especially for the very quick answer.  I think my issue could be because of the realtionship between various tables and how I am using them. I have attached how the actual dataset is with the various related tables. You will see that now the percentage does not work, especially if you filter with the slicer. 

Thanks,

Mike

 

https://file.io/qtomU8  (not sure how to attach a file)

 

Anonymous
Not applicable

Hi @Mike22 ,

the link doesn't work.

Try copy pasting in the browser. 

 

https://file.io/qtomU8

 

Thanks!

@Anonymous  it stopped working for me as well. 

Try below

https://file.io/L5835e

 

@Anonymous 

 

this should work

 

https://wsi.li/l58QzwNBxpI9m8

dax
Community Support
Community Support

Hi Mike22,

You could try below measure to see whether it work ot not.

Measure 3 = COUNT(T2[Person Number])/CALCULATE(COUNT(T2[Person Number]),filter( ALL(T2), T2[Level]=MIN(T2[Level])))

 

Best Regards,
Zoe Zhi

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

Thanks @dax 

 

Unfortunately it does not work. Results below. I am really confused. 

 

image.png

Anonymous
Not applicable

Hi,

Try with this one:

 

Measure 2 = COUNT(T2[Person Number])/CALCULATE(COUNT(T2[Person Number]), ALLEXCEPT(T2,'Level Ordering'[Level]))
 
Lorenzo

Thank you @Anonymous 

 

I am getting the same results as the previous screen shot. Anyway you can include it in the pbix file I had previosuly shared? As soon as I try to filter the results become incorrect. 

Thanks!

dax
Community Support
Community Support

Hi Mike22,

I am sorry, I didn't take your filter into consideration previously. Now you could try below to see whether it work or not.

 

Measure 3 = if(ISFILTERED(Entity[Entity]),COUNT(T2[Person Number])/CALCULATE(COUNT(T2[Person Number]),filter( ALLSELECTED(T2), T2[Level]=MIN(T2[Level]) && T2[Entity]=MIN(Entity[Entity]))),COUNT(T2[Person Number])/CALCULATE(COUNT(T2[Person Number]),filter( ALLSELECTED(T2), T2[Level]=MIN(T2[Level]) )))

 

 

Best Regards,
Zoe Zhi

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

Thank you very much! This worked and also thought me a few more things about dax!

parry2k
Super User
Super User

@Mike22 how you sample data looks like can you share?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.