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

Counting Number of Occurrences of a String Over a Dynamic Date Range

Hi all,

 

I have a column 'CountDistinctSearchedName' which counts the number of times column values [Searched_Name] appears in my date set, like this :

 

CountDistinctSearchedName = COUNTX ( FILTER ( ScrutinySearchHeader, EARLIER ( ScrutinySearchHeader[Searched_Name] ) = ScrutinySearchHeader[Searched_Name]), ScrutinySearchHeader[Searched_Name])
 
I wish to compare the counts of those values to counting the same column [Searched_Name] over a given period, each [Searched_Name] having a [Search_Date], so given the following:
 
Searched_NameSearch_Date
white01-Dec-18
white01-Dec-18
white01-May-19
red01-Dec-18
red01-May-19
red01-May-19
red01-May-19
blue01-Dec-18
blue01-Dec-18
blue01-May-19
black01-Dec-18
black01-Dec-18
 
I'd like to be able to produce the following:
 
Searched_NameSearch_DateTotal CountCount In Last 3 months Based on Search_Date
white01-Dec-1831
red01-Dec-1843
blue01-Dec-1831
black01-Dec-1820

 

Help is much appreciated.

 

Thanks

1 ACCEPTED SOLUTION
v-xicai
Community Support
Community Support

Hi @Regal ,

 

For the error message, EARLIER and EARLIEST are usually used in calculated column. You can go through the link to learn more: https://docs.microsoft.com/zh-cn/dax/earlier-function-dax.

 

Based on my test creating a calculated column when use this DAX, it work fine. While, the place of EARLIER is usually like this:

CountDistinctSearchedName = COUNTX ( FILTER ( ScrutinySearchHeader, ScrutinySearchHeader[Searched_Name] = EARLIER(ScrutinySearchHeader[Searched_Name])), ScrutinySearchHeader[Searched_Name])

 

5.png

If your need to create CountDistinctSearchedName in measure, you can try this DAX.

 

CountDistinctSearchedName_measure = CALCULATE(COUNT(ScrutinySearchHeader[Searched_Name]),FILTER(ScrutinySearchHeader, ScrutinySearchHeader[Searched_Name]=MAX(ScrutinySearchHeader[Searched_Name])))

 

Best Regards,

Amy

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

3 REPLIES 3
v-xicai
Community Support
Community Support

Hi @Regal ,

 

For your demand, I have two questions showing picture below, especially, which date range is for "the Last 3 month ", because the count values seems not to be calculated by the range date.

 

4.png

Best Regards,

Amy

 

Regal
Frequent Visitor

Thanks for your response @v-xicai 

 

For Q1 - I don't need the search date in the final table, that's an error in my screenshot, apologies.

 

For Q2 - I meant the previous 3 months from today. But actually I relaised last night there was a very simple solution to this problem I had overlooked (too long staring at a screen yesterday I think). In the end I just used a table visualisation, added Searched_Name and Count of Searched_Name with a top N filter, and then used the same visual and added a relative date filter on the Search_Date for the last 3 calendar months.

 

But just out of curiosity anyway, do you know if there is a way to add additional filters into this code below, specifically around date ranges? Wherever I try to put additional filters in I get EARLIER/EARLIEST refers to an earlier row context which doesn’t exist error. 

 

CountDistinctSearchedName = COUNTX ( FILTER ( ScrutinySearchHeader, EARLIER ( ScrutinySearchHeader[Searched_Name] ) = ScrutinySearchHeader[Searched_Name]), ScrutinySearchHeader[Searched_Name])

 

v-xicai
Community Support
Community Support

Hi @Regal ,

 

For the error message, EARLIER and EARLIEST are usually used in calculated column. You can go through the link to learn more: https://docs.microsoft.com/zh-cn/dax/earlier-function-dax.

 

Based on my test creating a calculated column when use this DAX, it work fine. While, the place of EARLIER is usually like this:

CountDistinctSearchedName = COUNTX ( FILTER ( ScrutinySearchHeader, ScrutinySearchHeader[Searched_Name] = EARLIER(ScrutinySearchHeader[Searched_Name])), ScrutinySearchHeader[Searched_Name])

 

5.png

If your need to create CountDistinctSearchedName in measure, you can try this DAX.

 

CountDistinctSearchedName_measure = CALCULATE(COUNT(ScrutinySearchHeader[Searched_Name]),FILTER(ScrutinySearchHeader, ScrutinySearchHeader[Searched_Name]=MAX(ScrutinySearchHeader[Searched_Name])))

 

Best Regards,

Amy

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

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.