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
Anonymous
Not applicable

get counts based on nested conditions

Hi 

I have following table

 

ProductCustomer number
A1
B1
C1
F2
A2

 

Definition of customer: If the customer has purchased product A or B, only then we consider the customer. 

I want to create a count of customers based on these criteria. Also, it needs to add slicer products. However, whenever I select product F, technically it should show 1 customer. But it is showing blank.

 

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi, @Anonymous 

Thank you very much for your reply. Using the following DAX expression will get a count of different IDs:

Count Customer =
VAR _seleted_product =
    SELECTCOLUMNS ( 'Table', 'Table'[Product] )
VAR _table =
    FILTER ( 'Table', 'Table'[Product] IN _seleted_product )
VAR _ID_AB =
    SUMMARIZE (
        FILTER ( ALL ( 'Table' ), 'Table'[Product] IN { "A", "B" } ),
        'Table'[Customer number]
    )
RETURN
    IF (
        ISFILTERED ( 'Table'[Product] ),
        COUNTAX (
            SUMMARIZE (
                FILTER ( _table, 'Table'[Customer number] IN _ID_AB ),
                'Table'[Customer number]
            ),
            'Table'[Customer number]
        )
    )

 Here are the results:

vjianpengmsft_0-1719209429336.png

vjianpengmsft_1-1719209444857.png

I've uploaded the PBIX file below for your reference.

 

 

Best Regards

Jianpeng Li

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

4 REPLIES 4
Anonymous
Not applicable

Thank you @ryan_mayu 

Hi, @Anonymous 

Based on the data you provided and the expected results described, I created a measure using the following DAX expression:

Count Customer =
VAR _seleted_product =
    SELECTCOLUMNS ( 'Table', 'Table'[Product] )
VAR _table =
    FILTER ( 'Table', 'Table'[Product] IN _seleted_product )
VAR _ID_AB =
    SUMMARIZE (
        FILTER ( ALL ( 'Table' ), 'Table'[Product] IN { "A", "B" } ),
        'Table'[Customer number]
    )
RETURN
    IF (
        ISFILTERED ( 'Table'[Product] ),
        COUNTROWS ( FILTER ( _table, 'Table'[Customer number] IN _ID_AB ) )
    )

Here are the results:

vjianpengmsft_0-1718679064392.png

vjianpengmsft_1-1718679073539.png

vjianpengmsft_2-1718679095841.png

vjianpengmsft_3-1718679108806.png

I've provided the PBIX file used this time below.

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

Anonymous
Not applicable

This is good. However, if I select A, B and F, it should return 2 as there are only two unique customer

Anonymous
Not applicable

Hi, @Anonymous 

Thank you very much for your reply. Using the following DAX expression will get a count of different IDs:

Count Customer =
VAR _seleted_product =
    SELECTCOLUMNS ( 'Table', 'Table'[Product] )
VAR _table =
    FILTER ( 'Table', 'Table'[Product] IN _seleted_product )
VAR _ID_AB =
    SUMMARIZE (
        FILTER ( ALL ( 'Table' ), 'Table'[Product] IN { "A", "B" } ),
        'Table'[Customer number]
    )
RETURN
    IF (
        ISFILTERED ( 'Table'[Product] ),
        COUNTAX (
            SUMMARIZE (
                FILTER ( _table, 'Table'[Customer number] IN _ID_AB ),
                'Table'[Customer number]
            ),
            'Table'[Customer number]
        )
    )

 Here are the results:

vjianpengmsft_0-1719209429336.png

vjianpengmsft_1-1719209444857.png

I've uploaded the PBIX file below for your reference.

 

 

Best Regards

Jianpeng Li

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

 

 

 

ryan_mayu
Super User
Super User

@Anonymous 

maybe you can try to create a new column

 

count =
VAR _A=maxx(FILTER('Table','Table'[Customer number]=EARLIER('Table'[Customer number])&&'Table'[Product]="A"),'Table'[Product])
VAR _B=maxx(FILTER('Table','Table'[Customer number]=EARLIER('Table'[Customer number])&&'Table'[Product]="B"),'Table'[Product])
RETURN IF(_A<>""&&_B<>"",1,0)
 
11.PNG
 
then you just need to sum the count column
 
12.PNG




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

Proud to be a Super User!




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.