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

Compare Dates in IF, and run DATEDIFF in formula

Hi, im a new user in Power BI. Currently my task is to calculate the time difference between two dates, A and B, where supposed Date B is later than Date A. Since there are some technical error where Date A is later than Date B, it produces a negative result. Thus, now I need to do an IF to check if the Date A is later than Date B, return null, else run the calculation. But power BI is giving me an error. 

 

Unless, I didn't run the calculation, and I return string type, then it would work fine. And please let me know if I declare null correctly.

 

rocky_puff_0-1616547982709.png

 

Can anybody give some tips on the solution?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

IF you are creating the measure then use following DAX :

Measure =
VAR ABC = SELECTEDVALUE('Table'[Start])
VAR BC = SELECTEDVALUE('Table'[End])
RETURNIF(ABC<BC,DATEDIFF(ABC,BC,DAY),BLANK())
 
else in a calculated column :
Column = IF('Table'[Start]<'Table'[End],DATEDIFF('Table'[Start],'Table'[End],DAY),BLANK())
 

View solution in original post

5 REPLIES 5
HotChilli
Super User
Super User

You have to return the same datatype from all paths of the 'if' statement.  At the moment a string or a number are being returned.  Try blank()

But how to show the result of DATEDIFF calculation if I put blank() ? 

HotChilli
Super User
Super User

I haven't looked at the logic of the calculation. I'm just trying to get you past the error.  Replace "null" with blank()

Anonymous
Not applicable

IF you are creating the measure then use following DAX :

Measure =
VAR ABC = SELECTEDVALUE('Table'[Start])
VAR BC = SELECTEDVALUE('Table'[End])
RETURNIF(ABC<BC,DATEDIFF(ABC,BC,DAY),BLANK())
 
else in a calculated column :
Column = IF('Table'[Start]<'Table'[End],DATEDIFF('Table'[Start],'Table'[End],DAY),BLANK())
 

Yes, I've tried like this before. But I had wrongly set the sequence and I didn't set the variable. Maybe that's why it run into error. But my solution is the same as this one, so I will mark this as a solution. Thanks!

Helpful resources

Announcements
November Fabric Update Carousel

Fabric Monthly Update - November 2025

Check out the November 2025 Fabric update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Users online (27)