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
dusdau
Helper II
Helper II

Counting rows where date column falls within the last year

Hi All,

 

DAX newbie here, trying to do what seems like it should be a simple measure.  I have a table ('Market Share') that has a column of dates (LastModified), which has a relationship to my date table ('Dates'). Date is my date key column. I simply want to count the rows in the Market Share table where the LastModified column is within the last year.

 

It seems like this should do the trick, but its not returning anything:

 

UpdatedLastYear = CALCULATE(COUNTROWS('Market Share'), DATESBETWEEN('Dates'[Date],NEXTDAY(SAMEPERIODLASTYEAR(LASTDATE('Dates'[Date]))),LASTDATE('Dates'[Date])))

 

Can anyone tell what I'm missing?

 

Thanks,

Dustin

1 ACCEPTED SOLUTION

Ok, it wasn't the null values.  I think it was an issue with Date/Time format of the column.  I changed both the Market Share table and the Date table to 'Date' and it works.  Thanks for your help Ross!

View solution in original post

8 REPLIES 8
Anonymous
Not applicable

UpdatedLastYear = CALCULATE(
	COUNTROWS('Market Share'),	
	DATESINPERIOD(
		'Dates'[Date], 
		TODAY(), 
		-12, 
		MONTH
	)
)

Give this a try

Thanks Ross....same thing though...not returning anything

 

pbi.PNG

Anonymous
Not applicable

Can you try this for me:  Lets separate the countrows out into its own measure. So you have:

CountMeasure = COUNTROWS('Market Share')



UpdatedLastYear = CALCULATE(
	[CountMeasure],	
	DATESINPERIOD(
		'Dates'[Date], 
		TODAY(), 
		-12, 
		MONTH
	)
)

Thanks again Ross.  The count measure is working fine, but there must be something wrong with the way the measure is evaluating dates as that part is not working.  It doesn't return anything.  I realized my Dates table was not marked as a date table, but marking that didn't fix it.

 

Could the fact that some of the LastModified dates in the Market Share table are NULL cause this issue?

 

Thanks so much for your help on this.

 

Dustin

 

 

Anonymous
Not applicable

The nulls shouldn't matter that much.  Your column is set to type "Date" isnt it?

Interestingly the column or nulls seems to have an issue.   It is definitely a Date/Time column.

 

I created another column with formula (TodayDate = TODAY()-325) and then changed the relationship to the date table, and the formula works.

 

Not quite sure how to deal with the NULLs yet...

Anonymous
Not applicable

Good info to know!  Glad you are getting closer.  You are on the right track.  A find/replace of nulls to a specific date could be your solution. Whether that date is done via a lookup, or as a set "filter me out" type date will depend on your needs.

Ok, it wasn't the null values.  I think it was an issue with Date/Time format of the column.  I changed both the Market Share table and the Date table to 'Date' and it works.  Thanks for your help Ross!

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.