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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.