Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Enigma
Helper III
Helper III

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.

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft 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

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Microsoft Employee
Microsoft 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

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thanks @mahoneypat 

 

This worked perfectly!

And to answer your questions, I was converting the date as I was not aware how to use the DATE function. Now that I do, I am using that and not converting.

About a separate Date table - well, again, I don't have much idea about. Will do a bit of research. Any resources you can suggest that explains the pros and cons versus using date hierarchies? 

Glad it worked for you.  Here are some useful links with more info.

 

https://www.youtube.com/watch?v=d4yAzhIQqNk

 

https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

 

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.