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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Count if both products is bought

Hi,

 

Need some Dax help.

 

Trying to calculate how many products is bought of all customers only if they have bought all chosen products.

 

If I filter in a slicer on Product "XX" AND "YY" a simple count measure would give me the result "6"

 

But as said I only want it to count the products if the same customer bought both products chosen in the slicer. So the result should be "4", As only customer A and B bought both products.

 

Real data is larger with more products and customers and the amount of products chosen can be more than two. 

 

Datatable simplyfied:

CustomerProduct
AXX
AYY
BXX
BYY
CXX

C

ZZ
DYY
1 ACCEPTED SOLUTION

@Anonymous Maybe. Try:

 

 

Measure Count = 
  VAR __Values = DISTINCT('Slicer'[Product])
  VAR __Table = 
    DISTINCT(
      FILTER(
        SELECTCOLUMNS('Datatable',"__Customer",[Customer],"__Product",[Product]),
        [__Product] IN __Values
      )
    )
  VAR __Table1 = GROUPBY(__Table,[__Customer],"__Count",COUNTX(CURRENTGROUP(),[__Product]))
  VAR __Table2 = SELECTCOLUMNS(FILTER(__Table1,[__Count] = COUNTROWS(__Values)),"__Customer",[__Customer])
RETURN
  COUNTROWS(FILTER(Datatable,[Customer] IN __Table2 && [Product] IN __Values))

 

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

6 REPLIES 6
CNENFRNL
Community Champion
Community Champion

Correct modeling results in simple solution,

Screenshot 2021-08-21 202404.pngScreenshot 2021-08-21 202619.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Hi Again,

 

Thanks for your quick help - really appreciated 🙂

I think you are almost there.

 

Only thing is that it shouldn't be a distinct count. 

From your shown dataset the answer should "4", as customer C&D (who bought both products) in total bought 4 products.

 

How to change? 

Greg_Deckler
Community Champion
Community Champion

@Anonymous I wrote something similar to this Better Together - Microsoft Power BI Community.

 

But, I think in your case:

 

Measure Count = 
  VAR __Values = DISTINCT('Slicer'[Product])
  VAR __Table = 
    DISTINCT(
      FILTER(
        SELECTCOLUMNS('Datatable',"__Customer",[Customer],"__Product",[Product]),
        [__Product] IN __Values
      )
    )
  VAR __Table1 = GROUPBY(__Table,[__Customer],"__Count",COUNTX(CURRENTGROUP(),[__Product]))
RETURN
  COUNTROWS(FILTER(__Table1,[__Count] = COUNTROWS(__Values))) * COUNTROWS(__Values)

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi Greg,

 

Thanks a lot for your help - really great..

 

Maybe you are not far away from the result i'm searching for.

 

Only thing is that your measure dosen't seem to count more of the same products per Customer.

If the same customer bought more than one of the same product it needs to be counting all of them and not only once.

 

Does it makes sence? 🙂

@Anonymous Maybe. Try:

 

 

Measure Count = 
  VAR __Values = DISTINCT('Slicer'[Product])
  VAR __Table = 
    DISTINCT(
      FILTER(
        SELECTCOLUMNS('Datatable',"__Customer",[Customer],"__Product",[Product]),
        [__Product] IN __Values
      )
    )
  VAR __Table1 = GROUPBY(__Table,[__Customer],"__Count",COUNTX(CURRENTGROUP(),[__Product]))
  VAR __Table2 = SELECTCOLUMNS(FILTER(__Table1,[__Count] = COUNTROWS(__Values)),"__Customer",[__Customer])
RETURN
  COUNTROWS(FILTER(Datatable,[Customer] IN __Table2 && [Product] IN __Values))

 

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler  - You are the man and came first to the finish line.

Thanks for your help - much appreciated 😁😁

 

Now I'll take time to read your Better Together Topic 🙂

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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