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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
flyinggnugget
Frequent Visitor

Using measures to find overlapping between 2 or more categories

Hi All,

 

Suppose I have the following data:

NameGroup
JohnGroup 1
JohnGroup 2
MatthewGroup 1
MaryGroup 1
MaryGroup 2
SusanGroup 1
LisaGroup 2
SamuelGroup 1

 

Doing a simple distinct count for each group would give me the following results:

GroupDistinct Count
Group 15
Group 23

 

However, I am trying to find the count of names that are in both group 1 and group 2, in this case I should get "2". How can create a measure to get this value?

 

To add on, suppose I want to get the following results:

GroupDistinct Count
Group 13
Group 21
Both2

 

Is there a way I can achieve this result using purely measures? I am using a live connection to a semantic model so I won't be able to create calculated columns and tables.

 

Thanks!

2 ACCEPTED SOLUTIONS
mh2587
Super User
Super User

Distinct Count Group = //Try this and adjust your Table Names and Column Names
VAR Group1Names = CALCULATETABLE(VALUES('Table'[Name]), 'Table'[Group] = "Group 1")
VAR Group2Names = CALCULATETABLE(VALUES('Table'[Name]), 'Table'[Group] = "Group 2")
VAR CountBothGroups = COUNTROWS(INTERSECT(Group1Names, Group2Names))
VAR CountGroup1 = DISTINCTCOUNTX(FILTER('Table', 'Table'[Group] = "Group 1"), 'Table'[Name])
VAR CountGroup2 = DISTINCTCOUNTX(FILTER('Table', 'Table'[Group] = "Group 2"), 'Table'[Name])
RETURN
IF(
    HASONEVALUE('Table'[Group]),
    SWITCH(
        VALUES('Table'[Group]),
        "Group 1", CountGroup1,
        "Group 2", CountGroup2,
        "Both", CountBothGroups
    ),
    BLANK()
)

Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



View solution in original post

Troekoe
Frequent Visitor

Yo!

To get the exact data which names show up is quite difficult in DAX.
However to get the count of names from group 1 and 2 we can create a DAX measure to count the rows in a table where the names are similar.
Considering the data you suggested we write the following 3 measures:

DISTINCT_GROUPS = DISTINCTCOUNT(Data[Group])
COUNT_NAMES = COUNT(Data[Name])
COUNT_SAME_NAME =
VAR table_1  = DISTINCT(CALCULATETABLE(SELECTCOLUMNS(Data, Data[Name]), Data[Group] = "Group 1"))
VAR table_2  = DISTINCT(CALCULATETABLE(SELECTCOLUMNS(Data, Data[Name]), Data[Group] = "Group 2"))
VAR innerjoin = NATURALINNERJOIN(table_1, table_2)

RETURN
    COUNTROWS(DISTINCT(innerjoin))


After the result we can put the measures in a Matrix and get the following:

Troekoe_0-1713951473622.png

 



hope this helps.

--Troekoe

 

View solution in original post

3 REPLIES 3
Troekoe
Frequent Visitor

Yo!

To get the exact data which names show up is quite difficult in DAX.
However to get the count of names from group 1 and 2 we can create a DAX measure to count the rows in a table where the names are similar.
Considering the data you suggested we write the following 3 measures:

DISTINCT_GROUPS = DISTINCTCOUNT(Data[Group])
COUNT_NAMES = COUNT(Data[Name])
COUNT_SAME_NAME =
VAR table_1  = DISTINCT(CALCULATETABLE(SELECTCOLUMNS(Data, Data[Name]), Data[Group] = "Group 1"))
VAR table_2  = DISTINCT(CALCULATETABLE(SELECTCOLUMNS(Data, Data[Name]), Data[Group] = "Group 2"))
VAR innerjoin = NATURALINNERJOIN(table_1, table_2)

RETURN
    COUNTROWS(DISTINCT(innerjoin))


After the result we can put the measures in a Matrix and get the following:

Troekoe_0-1713951473622.png

 



hope this helps.

--Troekoe

 

mh2587
Super User
Super User

Distinct Count Group = //Try this and adjust your Table Names and Column Names
VAR Group1Names = CALCULATETABLE(VALUES('Table'[Name]), 'Table'[Group] = "Group 1")
VAR Group2Names = CALCULATETABLE(VALUES('Table'[Name]), 'Table'[Group] = "Group 2")
VAR CountBothGroups = COUNTROWS(INTERSECT(Group1Names, Group2Names))
VAR CountGroup1 = DISTINCTCOUNTX(FILTER('Table', 'Table'[Group] = "Group 1"), 'Table'[Name])
VAR CountGroup2 = DISTINCTCOUNTX(FILTER('Table', 'Table'[Group] = "Group 2"), 'Table'[Name])
RETURN
IF(
    HASONEVALUE('Table'[Group]),
    SWITCH(
        VALUES('Table'[Group]),
        "Group 1", CountGroup1,
        "Group 2", CountGroup2,
        "Both", CountBothGroups
    ),
    BLANK()
)

Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



Thanks for the reply, the first half of the DAX query will be useful for me, unfortunately I won't be able to make use of the part in the return section as I don't have a group named "Both". I provided the example just in case there was some way to get that category out there, but no worries, I can definitely make use of the first half and use a workaround for my visual. Thanks!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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