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
catfood49
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

@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
catfood49
Frequent Visitor

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!

@catfood49

 

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

 

Regards

 

Victor

 




Lima - Peru

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 🙂

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

waltheed
Solution Supplier
Solution Supplier

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
ITsmart BI and Analytics consultant

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?

@catfood49

 

Maybe this link can help you:

 

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

 

Regards

 




Lima - Peru

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.