Forum Discussion

Enigma's avatar
Enigma
Icon for Helper III rankHelper III
6 years ago
Solved

Counting unique rows from a column after filtering another column

Hi,

 

I have a measure that first filters out all rows from a date column that are older than a given date; and then, counts the rows in the 'Document Key' column.

 

Under1Year = COUNTX(FILTER('Inventory',[Content Current Date].[Date] >= CONVERT("03/01/2019",DATETIME)),'Inventory'[Document Key])

 

This was working fine.

 

Now, there's an additional requirement to count only unique values from the column. I modified the formula as below but none of them worked.

This one gave the same result as the above original formula(which I know is incorrect as I manualy checked there are duplicate values present in the 'Document Key' column after the date filter is applied)

 

 

Under1Year = COUNTX(FILTER('Inventory',[Content Current Date].[Date] >= CONVERT("03/01/2019",DATETIME)),DISTINCTCOUNT('Inventory'[Document Key]))

 

 

 

Then I tried this one and, as expected, it threw a runtime error as DISTINCTCOUNT only accepts a column as an argument.

 

 

Under1Year = DISTINCTCOUNT(COUNTX(FILTER('Inventory',[Content Current Date].[Date] >= CONVERT("03/01/2019",DATETIME)),'Inventory'[Document Key]))

 

 

 

Any ideas how to modify the formula so it only counts unique rows in the 'Document Key' column?

- Regards.

  • How about this measure

     

    NewMeasure = calculate(distinctcount(Inventory[Customer Key]), [Content Current Date].[Date] >= CONVERT("03/01/2019", DATETIME))

     

    A couple questions though.  Why are you converting to DateTime?  Is your Date column in that format?  Would >= Date(2019,3,1) work in its place?

     

    And I would also suggest having a separate Date table and turn off the Auto date hierarchies (.[Date]).

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

     

3 Replies

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft Employee

    How about this measure

     

    NewMeasure = calculate(distinctcount(Inventory[Customer Key]), [Content Current Date].[Date] >= CONVERT("03/01/2019", DATETIME))

     

    A couple questions though.  Why are you converting to DateTime?  Is your Date column in that format?  Would >= Date(2019,3,1) work in its place?

     

    And I would also suggest having a separate Date table and turn off the Auto date hierarchies (.[Date]).

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat