Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Distinct count in calculated column

Hi all,

 

Absolute fresher here so please bear with me.

 

I am struggling to understand how to create a calculated column using distinct count - I've managed to find the solution but don't understand why it works.

 

Basically, I wanted to create a calculated column that shows 'unique vendors by product' using the below 'export orders' table

 

 

My initial attempt is as follows:

Unique Vendors by Product = CALCULATE(

DISTINCTCOUNT(export_orders[Vendor]),
export_orders[Product])

This gave me the error message 'cannot convert value 'product1' of type Text to True/False.

 

After some googling, I updated my function to: 

 
Unique Vendors by Product = CALCULATE(
DISTINCTCOUNT(export_orders[Vendor]),
allexcept(export_orders,export_orders[Product]))
 
This gives me the desired calculated column, however, I am confused as to why my initial function did not work - why is 'allexcept' necessary to pull the desired result?
 
 

 

  • Hi Anonymous ,

    When you use calculate ,you need add filter condiction,and like the below(In the dax you started with, less expersion for filtering):

     

    CALCULATE(<expression>[, <filter1> [, <filter2> [, …]]])

     

     

    And if you want to make your first dax work work ,you need to adjust like the below:

     

    Unique Vendors by Product3 = CALCULATE(
    DISTINCTCOUNT(export_orders[Vendor]),FILTER(ALL(export_orders),
    export_orders[Product]=EARLIER(export_orders[Product])))

     

    Refer the below output result:

     

    And to learn more different between filter and allexcept ,refer the below article ,It is very specific in its presentation:

     
     
     

    Did I answer your question? Mark my post as a solution!


    Best Regards

    Lucien

2 Replies

  • v-luwang-msft's avatar
    v-luwang-msft
    Community Support

    Hi Anonymous ,

    When you use calculate ,you need add filter condiction,and like the below(In the dax you started with, less expersion for filtering):

     

    CALCULATE(<expression>[, <filter1> [, <filter2> [, …]]])

     

     

    And if you want to make your first dax work work ,you need to adjust like the below:

     

    Unique Vendors by Product3 = CALCULATE(
    DISTINCTCOUNT(export_orders[Vendor]),FILTER(ALL(export_orders),
    export_orders[Product]=EARLIER(export_orders[Product])))

     

    Refer the below output result:

     

    And to learn more different between filter and allexcept ,refer the below article ,It is very specific in its presentation:

     
     
     

    Did I answer your question? Mark my post as a solution!


    Best Regards

    Lucien