cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

Customer Count - Bought A & B but not C

I need to create a report that gives a count of customers who have purchased a certain product (or products) but NOT purchased another other product (or products). I have 3 products, A, B, C amongst a sales table of many more products. 

 

Bought A only - 200 customers

Bought B only - 50 customers

Bought C only - 65 customers

Bought A and B (but not C) - 156 customers

Bought A and C (but not B) - 90 customers

bought B and C (but not A) - 33 customers

Bought A, B and C - 23 customers

 

I do not need to show all counts at once, I have created a filter for the products and a card to show the customer count:

 

card.PNG

 

For simplicities sake of the example, this is the table structure:

table.PNG

 

What makes it more complicated is that I am using a live connection to an Azure Analysis Services tabular model so cannot use Calculated Tables or Calculated Columns.

 

I started by creating a static table (within my model) for the selectable products A, B and C and wrote a measure but realised it was only taking into account products people had bought based on the filter selection and wasn't factoring in products they hadn't bought.

 

I am new to DAX and finding it tricky dealing with having to create tables on the fly within my measure and not really be able to see what is happening!

 

If anyone can help it would be great. Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Champion
Community Champion

Re: Customer Count - Bought A & B but not C

@catfood49

 

Hi, please review the PBIX attached. I think that this can help you.

 

Regards

 

Victor

 




Lima - Peru

View solution in original post

7 REPLIES 7
Highlighted
Solution Supplier
Solution Supplier

Re: Customer Count - Bought A & B but not C

Hi, 

 

This is a data modeling issue. 

 

You'll need to have 3 tables:

- All customers

- All Products

- The link table, who bought what and when

 

Otherwise you will always have the problem that records that you need are filtered out. 

 

Once you have these three tables you can do the calculations you want.

 

Hope this gets you on the right track. 

Cheers, Edgar Walther
Power BI User Group Netherlands
Highlighted
Frequent Visitor

Re: Customer Count - Bought A & B but not C

Hi @waltheed

Thanks for the reply. 

 

The model isn't as simple as in the image from the question, I do have Customer, Sale, Product tables with required relationships. I have also created a static table containing the Product Keys for the 3 products I am interested in.

 

Even with these tables however I am still not sure how I can set up the measure count the customers who have sales for products for those selected, and do not have a sale for any products not selected.

 

Any further help you can offer me?

Highlighted
Community Champion
Community Champion

Re: Customer Count - Bought A & B but not C

@catfood49

 

Maybe this link can help you:

 

https://www.daxpatterns.com/basket-analysis/

 

Regards

 




Lima - Peru
Highlighted
Frequent Visitor

Re: Customer Count - Bought A & B but not C

Thanks for the link @Vvelarde. I took a look but I couldn't manage to change it to what I needed. I have come up with a working solution which doesn't feel particularly elegant but it is returning the results I need:

 

Measure = COUNTROWS(
	FILTER(
		ADDCOLUMNS (
		   'Customer' , "Cust Key" , Customer[Customer Key]           
			, "B Product Sale" , CALCULATE( COUNTROWS( 'Sale' ) , Sale[Product Key] = "B" )
			, "E Product Sale" , CALCULATE( COUNTROWS( 'Sale' ) , Sale[Product Key] = "E" )                
			, "G Product Sale" , CALCULATE( COUNTROWS( 'Sale' ) , Sale[Product Key] = "G" )
		)
		, (
			(				
                (
                    ( [B Product Sale] > 0 && [E Product Sale] = 0 && [G Product Sale] = 0 && COUNTROWS(Filter_Product)=1 && COUNTROWS( filter( Filter_Product , Filter_Product[Product Key]="B" ) ) = 1 )
                    || ( [E Product Sale] > 0 && [B Product Sale] = 0 && [G Product Sale] = 0 && COUNTROWS(Filter_Product)=1 && COUNTROWS( filter( Filter_Product , Filter_Product[Product Key]="E" ) ) = 1 )
                    || ( [G Product Sale] > 0 && [B Product Sale] = 0 && [E Product Sale] = 0 && COUNTROWS(Filter_Product)=1 && COUNTROWS( filter( Filter_Product , Filter_Product[Product Key]="G" ) ) = 1 )
                    || ( [B Product Sale] > 0 && [E Product Sale] > 0 && [G Product Sale] = 0 && COUNTROWS(Filter_Product)=2 && MIN(Filter_Product[Product Key]) = "B" && max(Filter_Product[Product Key]) = "E" )
					|| ( [B Product Sale] > 0 && [G Product Sale] > 0 && [E Product Sale] = 0 && COUNTROWS(Filter_Product)=2 && MIN(Filter_Product[Product Key]) = "B" && max(Filter_Product[Product Key]) = "G" )
					|| ( [E Product Sale] > 0 && [G Product Sale] > 0 && [B Product Sale] = 0 && COUNTROWS(Filter_Product)=2 && MIN(Filter_Product[Product Key]) = "E" && max(Filter_Product[Product Key]) = "G" )
					|| ( [B Product Sale] > 0 && [E Product Sale] > 0 && [G Product Sale] > 0 && COUNTROWS(Filter_Product)=3 )
                )
                && ISFILTERED(Filter_Product[Product Key])                
			)
		)
	)
)

I create a table for each customer and then whether they have a sale for each of my products I care about. I then filter on that table checking for sales and which filters are selected, with a filter for each of the 7 combinations of results I want to return giving I only care about 3 products. It's clearly very hard-coded and wouldn't really be feasible for any more than 3 products due to the number of combinations but it does work.

 

If anyone happens can offer a more elegant solution then I would be very happy to learn a better way of doing this in future!

Highlighted
Community Champion
Community Champion

Re: Customer Count - Bought A & B but not C

@catfood49

 

Hi, please review the PBIX attached. I think that this can help you.

 

Regards

 

Victor

 




Lima - Peru

View solution in original post

Highlighted
Frequent Visitor

Re: Customer Count - Bought A & B but not C

Wow thats great. Haven't looked into the code as yet but definitely seems to do what I need it to with much better code!

thanks @Vvelarde

Highlighted
Frequent Visitor

Re: Customer Count - Bought A & B but not C

Hi,  I loved your solution on the problem. I am trying to solve the same problem for my org.  It is just I have "Sub Product" as well.
I have one more add into your solution, if you can tell me, how can I add a measure or caculated column such that it can show me what items are missing.

 

fred.JPG

 

 

for instance in your example pbix file, Client Fred has bought only Product "A" but Fred is missing Product B,C,D,E.

I want to show that in a seperate table.
Please help me in how I can modify it.

 

Thank you. 

Stay Safe 🙂

Helpful resources

Announcements
Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Using the Community

Using the Community

Need help with the Power BI Community? Our 'Using the Community' support articles are a great place to start.

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Power Platform 2020 release wave 2 plan

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors