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
MFester
Frequent Visitor

Distinct counting with conditions

Hello, 

 

Hopefully someone can help me.

 

I need to distinct count with a condition.

 

I have a list of sellerID, a list of products and a date.

 

Basically i want to know how many different sellers have sold X, Y and Z products. A sellerID can be the same if a seller sells different products. So I need a distinct count

 

I need a formula something like this CountDistinct(SellerID) if (Product) is 'banana' or 'apple'.

 

Is this possible?

4 REPLIES 4
MFester
Frequent Visitor

SellerIDProductDate
1Apple1.1.2019
1Banana1.1.2020
2Chair1.1.2021
3Apple1.1.2022
4Apple1.1.2023
5Apple1.1.2024
5Banana1.1.2025
5Chair1.1.2026

 

Here is an example of the data.

 

I can't use a filter, because I need different groups in the same table/figure, therefor I think I need measures for each "group" i wanna create.


For instance I wanna know how many sellers have sold fruit, have many sellers furniture and so forth.

Because a seller can sell apple more than once i need distinct count.

The "or" suggestion could only have two groups. "Banana" and "apple", but if we also add "Orange" it doesn't work I think.

 

Thank you all for your helpful tips.

Anonymous
Not applicable

can you please try this and check if it is working

Measure = CALCULATE(DISTINCTCOUNT('Table'[SellerId]),OR('Table'[Product] = "apple",'Table'[Product]="banana"))

 

That should work. It's equivalent to (although less versatile than) my first solution. You can check it yourself 

AlB
Super User
Super User

Hi @MFester 

Your question is a bit ambiguous. First you say you want an AND of the products sold and then an OR. Also, it would help if you provide some sample data and and example.

If it's an OR:

1. Place product in a slicer and select the ones you want

2. Create a simple measure and place it in a card visual:

Measure = DISTINCTCOUNT(Table1[sellerID])

If it's an AND:

1. Place product in a slicer and select the ones you want

2. Create this measure and place it in a card visual:

 

Measure2 =
VAR ProdsInFilter_ =
    DISTINCT ( Table1[Product] )
VAR AuxTable_ =
    FILTER (
        DISTINCT ( Table1[SellerID] ),
        VAR ProdsCurrentSeller_ = CALCULATETABLE ( DISTINCT ( Table1[Product] ) )
        RETURN
            COUNTROWS ( INTERSECT ( ProdsInFilter_, ProdsCurrentSeller_ ) ) = COUNTROWS ( ProdsInFilter_ )
    )
RETURN
    COUNTROWS ( AuxTable_ )

Please mark the question solved when we get to the solution and consider kudoing if posts are helpful.

Cheers  Datanaut

 

 

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