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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Date time comparison with NOW() function

Hello Everyone,

I have an issue with comparing two datetime values.
The goal is to mark all the rows of data that are within 6:00:00 AM 3 days ago and 6:00:00 AM today:

datetime comparison.JPG

 

As you may see upper expression returns NO for datetime that is within last 3 days and i do not understand why is that.
Two clues:

1. When i am doing IF statement only for IF CurrentRowDate>=Is3Day it returns only datetime rows for past year. Condistion is not meet for current year.

2. Previously report was build for periods midnight to midnight, i wasn't including time part - it worked.

Can someone please explain what is goind on? DateTime column is Date/Time format but maybe it is still the case of different formatting used in expression?

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

I think the problem is that FORMAT is returning a string, so the IF statement is comparing 2 string values. Try

Within last 3 days =
VAR Is3Day =
    TODAY () - 3
        + TIME ( 6, 0, 0 )
VAR IsToday =
    TODAY () + TIME ( 6, 0, 0 )
RETURN
    IF ( 'Table'[DateTime] >= Is3Day && 'Table'[DateTime] <= IsToday, "Yes", "No" )

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Thanks to all of you who participated in the conversation. 

@johnt75  his solution i checked first and it worked so i marked as a solution but @ValtteriN input is also very useful!

Anonymous
Not applicable

Works like a charm!!!

Thank you very much!

ValtteriN
Super User
Super User

Hi,

The way you are using FORMAT the NOW() is considered as text in the calculation. This can be tested easily by removing the FORMAT from the dax:

Column2 = var _now =
NOW()
var _3now = NOW()-3
 var _col = 'Table (16)'[Column1]
 return

IF(_col>=_3now && _col<=_now,"Yes","No")


To get full hours you can use this kind of dax: 


Column 3 = var _now = TODAY() + TIME(6,0,0)
var _3now = TODAY()-3 + TIME(6,0,0)
 var _col = 'Table (16)'[Column1]
 return

IF(_col>=_3now && _col<=_now,"Yes","No")

End results:
ValtteriN_0-1672743580360.png

 

I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




johnt75
Super User
Super User

I think the problem is that FORMAT is returning a string, so the IF statement is comparing 2 string values. Try

Within last 3 days =
VAR Is3Day =
    TODAY () - 3
        + TIME ( 6, 0, 0 )
VAR IsToday =
    TODAY () + TIME ( 6, 0, 0 )
RETURN
    IF ( 'Table'[DateTime] >= Is3Day && 'Table'[DateTime] <= IsToday, "Yes", "No" )

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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 community update carousel

Fabric Community Update - June 2025

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