Forum Discussion

tortugamarina's avatar
tortugamarina
Frequent Visitor
5 years ago

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

  • 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.

    • tortugamarina's avatar
      tortugamarina
      Frequent Visitor

      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.

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Super User

        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.

  • 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])))

    • tortugamarina's avatar
      tortugamarina
      Frequent Visitor

      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])))