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

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

Reply
cpereyra
Helper I
Helper I

Date difference between dates in same column

Hi All,

 

I need to calculate the date difference between dates in the same column. I'm using the dax below but it is calculating incorrectly I get all 1s.

Dates Between Prospects =
DATEDIFF (Merge1[Extra_Fields.Log_MS_Date_Started],
FIRSTDATE ( FILTER ( ALL (Merge1[Extra_Fields.Log_MS_Date_Started]), Merge1[Extra_Fields.Log_MS_Date_Started] > EARLIER ( Merge1[Extra_Fields.Log_MS_Date_Started] ))), DAY)

Capture.PNG

2 ACCEPTED SOLUTIONS

Hi @Greg_Deckler 

i have used a slightly different formula, but your pattern works well:

 

31-08-_2020_18-03-13.png

 

Difference = 
VAR _CurrentDate = 'Table'[Extra_Fields.Log_MS_Date_Started]
VAR _PreviousDate = 
    MAXX(
        FILTER(
            'Table',
            'Table'[Extra_Fields.Log_MS_Date_Started] < EARLIER('Table'[Extra_Fields.Log_MS_Date_Started])
        ),
        'Table'[Extra_Fields.Log_MS_Date_Started]
    )
RETURN
   IF(_PreviousDate = BLANK(), 0, _CurrentDate - _PreviousDate)

 

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

View solution in original post

@cpereyra - If I had to guess, you are probably not including all of the filtering criteria that you need to. Like whatever that field is just to the left of your date field. Your filter criteria needs to include all of the row columns that you want to "group" together.

 

So, like:

FILTER(
  'Table',
  [Column] = EARLIER([Column]) &&
  [Column1] = EARLIER([Column1])
)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

12 REPLIES 12
amitchandak
Super User
Super User

@cpereyra , Try a column like

 

datediff(maxx(filter(table,[date] <earlier([date])),[Date]),[date], day)

 

or

 

datediff([date],minx(filter(table,[date] >earlier([date])),[Date]), day)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

This formula just gives me a result of 1 for all rows.

 

Days between prospect =
DATEDIFF(MAXX(FILTER(Merge1,Merge1[Extra_Fields.Log_MS_Date_Started]<EARLIER(Merge1[Extra_Fields.Log_MS_Date_Started])),Merge1[Extra_Fields.Log_MS_Date_Started]),Merge1[Extra_Fields.Log_MS_Date_Started],day)
Greg_Deckler
Community Champion
Community Champion

@cpereyra See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __Previous = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Value])
RETURN
  __Current - __Previous



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler 

i have used a slightly different formula, but your pattern works well:

 

31-08-_2020_18-03-13.png

 

Difference = 
VAR _CurrentDate = 'Table'[Extra_Fields.Log_MS_Date_Started]
VAR _PreviousDate = 
    MAXX(
        FILTER(
            'Table',
            'Table'[Extra_Fields.Log_MS_Date_Started] < EARLIER('Table'[Extra_Fields.Log_MS_Date_Started])
        ),
        'Table'[Extra_Fields.Log_MS_Date_Started]
    )
RETURN
   IF(_PreviousDate = BLANK(), 0, _CurrentDate - _PreviousDate)

 

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

It looks like with this formula is picking up the day before rather than the previous date. 

Again, 

@cpereyra - If I had to guess, you are probably not including all of the filtering criteria that you need to. Like whatever that field is just to the left of your date field. Your filter criteria needs to include all of the row columns that you want to "group" together.

 

So, like:

FILTER(
  'Table',
  [Column] = EARLIER([Column]) &&
  [Column1] = EARLIER([Column1])
)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi, Thank you for getting back. I tried it but still incorrect. Any sugestion ?

 

 

Capture 1.PNG

@cpereyra - If I had to guess, you are probably not including all of the filtering criteria that you need to. Like whatever that field is just to the left of your date field. Your filter criteria needs to include all of the row columns that you want to "group" together.

 

So, like:

FILTER(
  'Table',
  [Column] = EARLIER([Column]) &&
  [Column1] = EARLIER([Column1])
)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

That was it. Thank you so much everyone for all your help. @Greg_Deckler @FrankAT @amitchandak 

@Greg_Deckler 

What field would you use as [value]? Do I need a date table besides the date field? 

I'm new to dax. TIA.

@cpereyra Depends on what you are trying to lookup, but in your case probably just use [Date] for [Value] in the formula.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
cpereyra
Helper I
Helper I

Result expected between 2/24/2020 and 2/19/2020 is 5 days.

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.