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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Doerka
Regular Visitor

Count values that are in two categories

Hi,

I have a table with 3 columns: an ID, an User ID and a category. I am trying to get the number of users, that have both categories.

For instance:

IDID_usercategory
11A
22A
33B
41B
52C
63D
74A

 

I want the measure to return 1 when it counts the Users that have the category A and B (in this example only user 1 fullfils that condidtion).

 

 

So im looking for somtehing like:

 

Measure = Calculate(
   DistinctCount('table'[ID_User]),
   'table'[Category] = "A" && 'table'[Category] = "B"
)

 

but this obviusly does not work, and I can't find a solution.

 

I hope I could provide enough information, if not please let me know.  Your help would be very appreciated!

 

Best regards 

Doerka

1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @Doerka ,

 

Here's my solution.

Create the first measure to get the count of category which is A or B and it is grouped by [ID_user.]

 

CountCategory = CALCULATE(DISTINCTCOUNT('Table'[category]),FILTER(ALLSELECTED('Table'),[ID_user ]=MAX('Table'[ID_user ])&&([category]="A"||[category]="B")))

 

Create the second measure to get the count of ID_user.

 

CountID_user = CALCULATE(DISTINCTCOUNT('Table'[ID_user ]),FILTER('Table',[CountCategory]=2))

 

 

vstephenmsft_0-1661246027434.png

 

Best Regards,

Stephen Tao

 

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

6 REPLIES 6
v-stephen-msft
Community Support
Community Support

Hi @Doerka ,

 

Here's my solution.

Create the first measure to get the count of category which is A or B and it is grouped by [ID_user.]

 

CountCategory = CALCULATE(DISTINCTCOUNT('Table'[category]),FILTER(ALLSELECTED('Table'),[ID_user ]=MAX('Table'[ID_user ])&&([category]="A"||[category]="B")))

 

Create the second measure to get the count of ID_user.

 

CountID_user = CALCULATE(DISTINCTCOUNT('Table'[ID_user ]),FILTER('Table',[CountCategory]=2))

 

 

vstephenmsft_0-1661246027434.png

 

Best Regards,

Stephen Tao

 

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

It takes a bit of time to calculate, but it provides the correct number.

 

Thank you very much for taking your time and helping out @v-stephen-msft!

ryan_mayu
Super User
Super User

@Doerka 

you can try this

Measure = 
VAR tbl=SUMMARIZE('Table','Table'[ID_user],"count",CALCULATE( countrows('Table'),FILTER('Table','Table'[category]="A" ||'Table'[category]="B")))
return COUNTROWS(FILTER(tbl,[count]=2))

1.PNG

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@ryan_mayu thank you for your quick reply.
Unfortunately does not work for me yet, for I didn't properly represent my Data in the example.

The issue is, that [ID_user] will have the same category assigned to it multiple times, like this:

IDID_user  category
11A
22A
33B
41B
52C
63D
71A
82A
91A

 

The expected result for A and B should still be 1 (user 1).

@Doerka 

pls try this

Measure = 
VAR tbl=SUMMARIZE('Table','Table'[ID_user ],'Table'[ category])
var tbl2=SUMMARIZE(tbl,'Table'[ID_user ],"count",CALCULATE( countrows('Table'),FILTER('Table','Table'[ category]="A" ||'Table'[ category]="B")))
return COUNTROWS(FILTER(tbl2,[count]=2))

1.png





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @ryan_mayu ,
the measure you suggested only counts the ID_user that appear [count]=2 times, wich happens in the provided Data once for [ID_user] = 2

IDID_usercategory
22A
82A


If I understand your code corrrectly, you tried to SUMMARIZE instances, where ID_user and category are the same, but it seems like this does not work.

You can check for yourself by changing the last row of the data to

IDID_usercategory
92A

 

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

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

August Carousel

Fabric Community Update - August 2024

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