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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
prpaz
Regular Visitor

Calculate distinct items by classification

Hello friends, I have the following question about creating a measure.

 

I have two related tables, items and classification. I need a measure that calculates the number of distinct items that have only one classification, example of a table.

 

table item

cod item |  item 

a1              lop1

a2              lop33

a3              lop77
a4              prp20
a5              kge25

table Classification
cod item |  classification  | item 
a1                orange           prp20

a2                red                 prp20

a3                orange           kge25

a4                blue               lop1
a5                blue               lop33


What measure to use to know the distinct count of only the orange items? just the blue items? just the red items?

1 ACCEPTED SOLUTION

Hi @prpaz ,

 

We can create measures. 

Flag = 
var _table=SUMMARIZE('classification','classification'[item],'classification'[classification])
var _count=COUNTROWS(FILTER(_table,[item] in VALUES('classification'[item])))
RETURN IF(_count=1,1,0)
count = SUMX(VALUES('classification'[item]),[Flag])

Then the result is as follows, the number of items that have only one type of classification is 7.

vtangjiemsft_0-1704693044066.png

Best Regards,

Neeko Tang

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

8 REPLIES 8
v-tangjie-msft
Community Support
Community Support

Hi @prpaz ,

 

According to your description, here are my steps you can follow as a solution.

(1) My test data is the same as yours.

(2) We can create a measure.

Measure = CALCULATE(DISTINCTCOUNT('table Classification'[item]),FILTER(ALLSELECTED('table Classification'),'table Classification'[classification]=MAX('table Classification'[classification])))

(3) Then the result is as follows.

vtangjiemsft_0-1704438701646.png

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

 

Best Regards,

Neeko Tang

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

Hello friends, I still haven't found the solution I need.

Below is an image for better understanding.

I need to know the number of items that have only one type of classification, see that I have 9 different items, but which ones have only one type of classification (graph 3)?



img.jpg



link to power bi.
distinct test.pbix 

Hi @prpaz ,

 

We can create measures. 

Flag = 
var _table=SUMMARIZE('classification','classification'[item],'classification'[classification])
var _count=COUNTROWS(FILTER(_table,[item] in VALUES('classification'[item])))
RETURN IF(_count=1,1,0)
count = SUMX(VALUES('classification'[item]),[Flag])

Then the result is as follows, the number of items that have only one type of classification is 7.

vtangjiemsft_0-1704693044066.png

Best Regards,

Neeko Tang

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

prpaz
Regular Visitor

Explaining in an easier way, I have several items that have several classifications. But I need the number of items that only have one distinct classification. Example: The number of distinct items that are allocated to the orange classification.

Hi, @prpaz in this case what is count for orange?

For red?

For Blue? From data you provide 

Dangar332
Super User
Super User

hi, @prpaz 

try below code 

Measure 3 = 
CALCULATE(DISTINCTCOUNT('item'[item]),TREATAS(VALUES(clas[code item]),'item'[code item]))+DISTINCTCOUNT(clas[code item])

 

Dangar332_0-1704396107848.png

 

PijushRoy
Super User
Super User

HI @prpaz 

Can you please try the below code in Measure

Measure = 
CALCULATE(DISTINCTCOUNT('table Classification'[classification]),'table Classification'[classification]="orange")

 
Please change the DISTINCTCOUNT column name which you want to count

If solve your requirement, please mark as SOLUTION

Thanks
Pijush

Hello, an item can have several classifications, but I only need the distinct count of the items in each classification for comparison in the graph, see example:

Orange.... 4

Red................5
Blue.....1

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.