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
Pamela1
New Member

Calculate the proportion of people over a distinct count by category

Hello,

 

I’d like to create a measure to calculate the proportion of people by ethnicity group and job group. I have created a measure to calculate the proportion of people by ethnicity but need some help to do it by ethnicity group, i.e. the total number of people in an ethnicity group over the distinct number of people within an ethnicity group. 

 

In the dataset I am using, people can select up to three individual ethnicities and then these are grouped into broader ethnicity groups for reporting. The Ethnicity ID columns join the fact table and the dim table and is a many to one relationship, respectively. (The fact table also includes job group in case it’ll affect the measure as I include it on the chart which will be the proportion of people by ethnicity group and job group)

 

Fact table:

Person IDEthnicity IDJob Group
191
181
171
291
281
351
492
583
573
692
652
632

 

Dim table:

Ethnicity IDEthnicityEthnicity Group
9EnglishEuropean
8ScottishEuropean
7WelshEuropean
5FrenchEuropean
3EgyptianAfrican

 

In Person ID 2’s case, they have selected English, Scottish, and Welsh, and their broader ethnicity group for both of these is European. My measure is currently calculating this as 3 divided by 1. I’d like to update it to be 1 divided by 1 because even though they have selected three ethnicities, they all fall under the same ethnicity group and they’re the same person. The denominator is correct at 1 as it is counting just the one person. In Person ID 6’s case for example, they have selected English, French, and Egyptian. I’d like them to be counted once under the ethnicity group European, and also once under African.

 

Regards

1 ACCEPTED SOLUTION

Hi , try to create a new table with the code below:Table =

VAR Table1 =
ADDCOLUMNS(
     VALUES(Fact[Job Group]),
    "European",
    CALCULATE(
        DISTINCTCOUNT(Fact[Person ID]),
        DIM[Ethnicity Group]="European"
     ),
    "African",
    CALCULATE(
        DISTINCTCOUNT(Fact[Person ID]),
        DIM[Ethnicity Group]="African"
     ),
     "People",
     CALCULATE(
        DISTINCTCOUNT(Fact[Person ID])
     )
)
VAR Table2=
ADDCOLUMNS(
    Table1,
    "Percentage",
    FORMAT(
        DIVIDE([European]+[African], [People]),
        "00%"
     )
)
RETURN
    Table2

 

I tried and it works.

 

 

FreemanZ_5-1668840889258.png

FreemanZ_4-1668840869942.png

FreemanZ_3-1668840847532.png

@Pamela1

View solution in original post

4 REPLIES 4
Pamela1
New Member

@FreemanZ , thanks for your quick response.

 

Using the dataset above, the data would look like this:

 

Job groupEuropeanAfricanDistinct no. peoplePercentage
13 3100%
2212150%
31 1100%

Hi , try to create a new table with the code below:Table =

VAR Table1 =
ADDCOLUMNS(
     VALUES(Fact[Job Group]),
    "European",
    CALCULATE(
        DISTINCTCOUNT(Fact[Person ID]),
        DIM[Ethnicity Group]="European"
     ),
    "African",
    CALCULATE(
        DISTINCTCOUNT(Fact[Person ID]),
        DIM[Ethnicity Group]="African"
     ),
     "People",
     CALCULATE(
        DISTINCTCOUNT(Fact[Person ID])
     )
)
VAR Table2=
ADDCOLUMNS(
    Table1,
    "Percentage",
    FORMAT(
        DIVIDE([European]+[African], [People]),
        "00%"
     )
)
RETURN
    Table2

 

I tried and it works.

 

 

FreemanZ_5-1668840889258.png

FreemanZ_4-1668840869942.png

FreemanZ_3-1668840847532.png

@Pamela1

@Pamela1 ,You may also do it step by step, by creating 4 measures and putting them into a table visual, like this:

FreemanZ_8-1668841611326.png

 

 

Decomposed code for the 4 measures:

European :=
    CALCULATE(
        DISTINCTCOUNT(Fact[Person ID]),
        DIM[Ethnicity Group]="European"
     )
 
African :=
    CALCULATE(
        DISTINCTCOUNT(Fact[Person ID]),
        DIM[Ethnicity Group]="African"
     )
 
People :=
     CALCULATE(
        DISTINCTCOUNT(Fact[Person ID])
     )
 
Percentage :=
    FORMAT(
        DIVIDE([European]+[African], [People]),
        "00%"
     )

 

FreemanZ
Super User
Super User

how will your expected result look like?

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.

Top Solution Authors