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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Lauraeire_81
Helper I
Helper I

Count values in categories

Hi

I have the following table:

Food Category Food 
AmericanHot Dogs
Irish Hot Dogs
ItalianPasta
AmericanFries
Irish Ham and Cabbage
Irish Shepherds pie
AmericanCorn Dogs

 

I would like to create three measures for the following:

Count: 2 are only American (Fries, Corn Dogs) - non-repeated values in Food Column

Count: 1 are American and Irish (Hot Dogs) - repeated values in Food Column

Count: 1 are Italian (Pasta) etc..

 

I am creating a Venn Diagram so will need to get these counts for each area of it. 

 

Thanks in advance. 

 

Regards,

Laura

 

 

1 ACCEPTED SOLUTION
v-kathullac
Community Support
Community Support


Hi @Lauraeire_81 ,

As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?

If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

Regards,

Chaithanya

View solution in original post

10 REPLIES 10
v-kathullac
Community Support
Community Support


Hi @Lauraeire_81 ,

As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?

If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

Regards,

Chaithanya

Khushidesai0109
Super User
Super User

Hiii @Lauraeire_81 

 

 

Measure 1: Count of Foods that are only American
Only_Ameican_Count =

VAR AmericanFoods =
FILTER(
VALUES('Table'[Food]),
CALCULATE(DISTINCTCOUNT('Table'[Food Category]), ALL('Table')) = 1
&& MAX('Table'[Food Category]) = "American"
)
RETURN
COUNTROWS(AmericanFoods)

 

 

Measure 2: Count of Foods that are American & Irish
American_And_Irish_Count =

VAR AmericanIrishFoods =
FILTER(
VALUES('Table'[Food]),
CALCULATE(DISTINCTCOUNT('Table'[Food Category]), ALL('Table')) = 2
&& "American" IN VALUES('Table'[Food Category])
&& "Irish" IN VALUES('Table'[Food Category])
)
RETURN
COUNTROWS(AmericanIrishFoods)

 



Measure 3: Count of Foods that belong to only one category (e.g., Italian, Irish, etc.)
Single_Category_Count =
VAR SingleCategoryFoods =
FILTER(
VALUES('Table'[Food]),
CALCULATE(DISTINCTCOUNT('Table'[Food Category]), ALL('Table')) = 1
)
RETURN
COUNTROWS(SingleCategoryFoods)







Proud to be a Super User!!
sroy_16
Resolver II
Resolver II

Hello @Lauraeire_81 

Please try using these measures and see if it fulfils your requirement.

Count_American_Only =
CALCULATE (
DISTINCTCOUNT (FoodCategory[Food]),
FoodCategory[Food Category] = "American",
NOT (
FoodCategory[Food] IN
CALCULATETABLE (
VALUES (FoodCategory[Food]),
FoodCategory[Food Category] <> "American"
)
)
)

Count_American_and_Irish =
CALCULATE (
DISTINCTCOUNT (FoodCategory[Food]),
FoodCategory[Food] IN
CALCULATETABLE (
VALUES (FoodCategory[Food]),
FoodCategory[Food Category] = "American"
),
FoodCategory[Food] IN
CALCULATETABLE (
VALUES (FoodCategory[Food]),
FoodCategory[Food Category] = "Irish"
)
)


 

 

Count_Italian_Only =
CALCULATE (
DISTINCTCOUNT (FoodCategory[Food]),
FoodCategory[Food Category] = "Italian",
NOT (
FoodCategory[Food] IN
CALCULATETABLE (
VALUES (FoodCategory[Food]),
FoodCategory[Food Category] <> "Italian"
)
)
)


I hope the solution I provided helps! If it works for you and solves your issue, please feel free to mark it as a solution so it can assist others who may have the same problem.

Lauraeire_81
Helper I
Helper I

HI @bhanu_gautam Thank you this helps.

What does EARLIER mean? Also if I had a slicer with 3 values (American, Irish, Italian), can you show me how this works with those measures above?

I'd like to leave all values as '0' if there are no slicer values selected.

Thanks for your help.

Regards,

Laura

@Lauraeire_81 The EARLIER function in DAX is used to refer to an earlier row context in a nested row context

 

To incorporate a slicer with values (American, Irish, Italian) and ensure that the measures return '0' if no slicer values are selected, you can modify the measures to include a check for the slicer selection. Here are the updated measures:

dax
CountOnlyAmerican =
IF(
ISFILTERED('Table'[Category]),
CALCULATE(
COUNTROWS(
FILTER(
VALUES('Table'[Food]),
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] = "American")
) = 1 &&
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] <> "American")
) = 0
)
)
),
0
)

 

dax
CountAmericanAndIrish =
IF(
ISFILTERED('Table'[Category]),
CALCULATE(
COUNTROWS(
FILTER(
VALUES('Table'[Food]),
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] = "American")
) > 0 &&
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] = "Irish")
) > 0
)
)
),
0
)

 

dax
CountItalian =
IF(
ISFILTERED('Table'[Category]),
CALCULATE(
COUNTROWS(
FILTER(
VALUES('Table'[Food]),
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] = "Italian")
) > 0
)
)
),
0
)

 

These measures use the ISFILTERED function to check if the slicer is applied to the 'Category' column. If the slicer is not applied, the measures return '0'. If the slicer is applied, the measures perform the calculations as described.




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Thanks @bhanu_gautam it gives me back a count of '3' when American is selected. Can you please send me the powerbi pbx file ? Thanks for your help.

danextian
Super User
Super User

Hi @Lauraeire_81 

 

Please try the following:

Measure01 = 
CALCULATE (
    DISTINCTCOUNT ( _data[Food Category] ),
    KEEPFILTERS ( _data[Food] IN {"Hot Dogs", "Fries"} )
)


Measure02 = 
CALCULATE (
    DISTINCTCOUNT ( _data[Food Category] ),
    KEEPFILTERS ( _data[Food] =  "Hot Dogs" )
)

Measure03 = 
CALCULATE (
    DISTINCTCOUNT ( _data[Food Category] ),
    KEEPFILTERS ( _data[Food] = "Pasta" )
)

 

danextian_0-1742390612454.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
bhanu_gautam
Super User
Super User

@Lauraeire_81 Create measure

Count of Foods that are only American (non-repeated values in Food Column):

DAX
CountOnlyAmerican =
CALCULATE(
COUNTROWS(
FILTER(
VALUES('Table'[Food]),
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] = "American")
) = 1 &&
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] <> "American")
) = 0
)
)
)

 

Count of Foods that are American and Irish (repeated values in Food Column):

DAX
CountAmericanAndIrish =
CALCULATE(
COUNTROWS(
FILTER(
VALUES('Table'[Food]),
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] = "American")
) > 0 &&
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] = "Irish")
) > 0
)
)
)

 

Count of Foods that are Italian:

DAX
CountItalian =
CALCULATE(
COUNTROWS(
FILTER(
VALUES('Table'[Food]),
COUNTROWS(
FILTER('Table', 'Table'[Food] = EARLIER('Table'[Food]) && 'Table'[Category] = "Italian")
) > 0
)
)
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






hI @bhanu_gautam @sroy_16 @danextian - Thanks for all of your help.

Can you please help with the following DAX:

I want to Count the values of food in Irish and American if 2 values are selected in the slicer (America and Irish), can you please help with this?

 

IrishandAmerican =
var selected = SELECTEDVALUE('Sheet1'[Column1])
return
if (selected = "Irish""American",
CALCULATE (
DISTINCTCOUNT (Sheet1[Column2]),
Sheet1[Column1] = "American",
NOT (
(Sheet1[Column2] IN
CALCULATETABLE (
VALUES (Sheet1[Column2]),
(Sheet1[Column1] <> "Irish")
)
))),
0
)

HI @Lauraeire_81 

Please give this a try and let me know if it meets your requirements.

IrishandAmerican =
VAR selectedValues = VALUES('Sheet1'[Column1])
RETURN
IF (
COUNTROWS(selectedValues) = 2
&& "Irish" IN selectedValues
&& "American" IN selectedValues,
CALCULATE (
DISTINCTCOUNT('Sheet1'[Column2]),
'Sheet1'[Column1] IN {"Irish", "American"}
),
0
)

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors