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 September 15. Request your voucher.

Reply
cuseman03
Helper I
Helper I

DAX - If statement with datediff based on most recent date

Hi all,

 

so I am pretty close to what I need to do but just stuck on this last part.  Ok, so I have two columns from SQL, one is a 'start date' and one is an 'end date'.  Format is datetime.  I used query editor and created a new calculated column that did a date diff in days.  Worked fine.  Every day between dates is accurate except for one, and this is a repeating pattern.  It is always wrong on the latest date for a given line item.  That is because in our warehouse, we denote record history with the latest having an end date '12/31/9999.

 

As you can see from the picture, DAX is counting the duration between the last record (start date is correct) but it's calculating to the above date.  What I want to do is instead say for the most recent record, in only this instance calculate days diff by subtracting 'today' from 'start date'

 

my logic is essentially this:  for every record except the latest, do X and for the latest do Y.   Also, how do I make it so 'day' count is only business days.

 

DAX date diff.png

 

 

1 ACCEPTED SOLUTION
andhiii079845
Solution Sage
Solution Sage

Hi,
my solution:

Bildschirmfoto 2023-02-25 um 12.12.04.png

DATEDIFF =
VAR
_varstart = maxx(Tabelle11,Tabelle11[Start])
VAR _varend = maxx(Tabelle11,Tabelle11[End])
VAR _varmaxstart = maxx(ALLSELECTED(Tabelle11),Tabelle11[Start])
RETURN
IF(_varmaxstart=_varstart,DATEDIFF(_varstart,TODAY(),DAY),DATEDIFF(_varstart,_varend,DAY))




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

Proud to be a Super User!




View solution in original post

2 REPLIES 2
cuseman03
Helper I
Helper I

this worked great!

andhiii079845
Solution Sage
Solution Sage

Hi,
my solution:

Bildschirmfoto 2023-02-25 um 12.12.04.png

DATEDIFF =
VAR
_varstart = maxx(Tabelle11,Tabelle11[Start])
VAR _varend = maxx(Tabelle11,Tabelle11[End])
VAR _varmaxstart = maxx(ALLSELECTED(Tabelle11),Tabelle11[Start])
RETURN
IF(_varmaxstart=_varstart,DATEDIFF(_varstart,TODAY(),DAY),DATEDIFF(_varstart,_varend,DAY))




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

Proud to be a Super User!




Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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