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

DAX - Measure to count distinct values when countrows with same ID is 2

Hi all,

 

I am trying to calculate:
1) Number of Departments where each customer has bought

2) For those customers who bought in 2 different departments, what is the total count of those departments

3) not sure if possible, create a matrix to represent the count combination

 

Here is the sample of the dataset:

customerIDDepartmentAmountDate
1Salad2010-jan-21
1Burger910-jan-21
1Drinks1310-jan-21
2Salad1810-jan-21
2Drinks2010-jan-21
3Coffee910-jan-21
4Salad510-jan-21
5Salad910-jan-21
5Burger610-jan-21
6Salad1510-jan-21
6Burger2010-jan-21
7Drinks1210-jan-21
7Coffee1010-jan-21

 

With this calculation I got the following table

 

Count Distinct Dep =
sumx(SUMMARIZE(Articles,Articles[Departments],"Count dep",count(cust[customerID])),[Count dep])

 

 

customerIDCount Deparments
13
22
31
41
52
62
72

 

Now I would like to select only with Count department = 2

 

customerIDDistinct Deparments = 2DepartmentDepartment
2xSaladDrinks
5xSaladBurger
6xSaladBurger
7xDrinksCoffee

 

And count the distinct values of the Departments for those clients, but these different calculation are not working...

 

Two Dep Count Distinct = 
COUNTROWS (FILTER (VALUES (cust[customerID]), CALCULATE([Count Distinct Dep]=2) ))

 

 

This following one returns the correct number of clients buying in 2 departments, but when visualizing with Department list, it crashes.

 

Dep count 2 grouping = 
COUNTROWS (
    FILTER (
        VALUES ( cust[customerID] ),
        CALCULATE ( [Count Distinct Dep]=2)
    ))

 

 

This should be the desired output:

 Count
Salad3
Burger2
Drinks2
Coffee1

 

It is being quite tricky to find information about how to create this matrix, where you can visualize what are the pairs combination. In other terms, the previous result buy providing more information about how the Departments relate eachother.

 

 SaladBurgerDrinksCoffee
Salad-21 
Burger2-  
Drinks1 -1
Coffee  1-

 

7 REPLIES 7
Ashish_Mathur
Super User
Super User

Hi,

This measure works

Measure = COUNTROWS(FILTER(SUMMARIZE(VALUES(Data[customerID]),Data[customerID],"ABCD",COUNTROWS(Data),"EFGH",CALCULATE(COUNTROWS(Data),ALL(Data[Department]))),[ABCD]>0&&[EFGH]=2))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi,

This solution is not working for me. I get totally random values that does not make sense. Could you please explain a bit the DAX and name ABCD and EFGH in a way that I can understand what they calculate?

 

Thank you.

I do not what the reason could be.  You can very clearly see in my screenshot that it works fine on the data that you had shared.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks for your explanation Ashish.

Hi @tortugamarina ,

 

Has your problem been solved? If it is solved, please mark a reply which is helpful to you.

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

 

Best Regards,
Winniz

amitchandak
Super User
Super User

@tortugamarina , While showing at department level you have to igonre department filter .

 

example

 


Two Dep Count Distinct =
calculate(COUNTROWS (FILTER (VALUES (cust[customerID]), CALCULATE([Count Distinct Dep]=2) )), filter(allselected(cust), Table[cust] = max(Table[cust])))

Hi, 
I don´t get the result desired. In fact, MAX function is not possible to use with a Table, but needs a specific column, and if I replace the cust for cust[customerID] it results in blank:

Two Dep Count Distinct =
calculate(COUNTROWS (FILTER (VALUES (cust[customerID]), CALCULATE([Count Distinct Dep]=2) )), filter(allselected(cust), cust[customerID] = max(cust[customerID])))

 

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.