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
leorrn
Regular Visitor

Calculate the difference between two dates in different rows

Hi,

 

I'd like to calculate the difference between dates, splited in different rows, returing the difference in the format "Days Hours".

 

Do you guys have any idea?

 

The table is already ordered by "Planta"and Datetime (ASC order). If there is no previous record, it can return null.

 

Thanks in advance community 🙂 

 

Table example

 

PlantaDatetimeDateIndexIndex.1datediff
Aglaonema15/1/25 9:0115/01/20251011 
Aglaonema18/1/25 16:3118/01/2025910 
Aglaonema22/1/25 8:5022/01/202589 
Aglaonema27/1/25 10:2427/01/202578 
Aglaonema1/2/25 15:1401/02/202567 
Aglaonema3/2/25 13:0503/02/202556 
Aglaonema5/2/25 10:2205/02/202545 
Alecrim19/9/24 8:0419/09/20244950 
Alecrim21/9/24 9:5321/09/20244849 
Alecrim23/9/24 9:3923/09/20244748 
Alecrim25/9/24 10:3025/09/20244647 
Alecrim26/9/24 7:5926/09/20244546 
Alecrim (2025-01)21/1/25 17:0321/01/20255354 
Alecrim (2025-01)22/1/25 10:3122/01/20255253 
Alecrim (2025-01)23/1/25 10:5723/01/20255152 
Alecrim (2025-01)3/2/25 13:1303/02/20255051 
Aloe Aristata19/9/24 8:0419/09/20246566 
Aloe Aristata23/9/24 9:3923/09/20246465 
Aloe Aristata26/9/24 8:1026/09/20246364 
Aloe Aristata29/9/24 10:0929/09/20246263 
Aloe Aristata1/10/24 15:1501/10/20246162 

 

1 ACCEPTED SOLUTION
rajendraongole1
Super User
Super User

Hi @leorrn  - Please find the attached pbix you can use DAX to calculate the time difference between consecutive rows within each "Planta" group

 

 

rajendraongole1_0-1738851598039.png

 

 

Datediff_Days_Hours =
VAR PrevDatetime =
    CALCULATE(
        MAX('timdiff'[Datetime]),
        FILTER(
            'timdiff',
            'timdiff'[Planta] = EARLIER('timdiff'[Planta]) &&
            'timdiff'[Datetime] < EARLIER('timdiff'[Datetime])
        )
    )

VAR DiffMinutes = DATEDIFF(PrevDatetime, 'timdiff'[Datetime], MINUTE)

VAR Days = QUOTIENT(DiffMinutes, 1440)  -- 1440 minutes in a day
VAR Hours = QUOTIENT(MOD(DiffMinutes, 1440), 60)  -- Remaining minutes converted to hours

RETURN
    IF(
        ISBLANK(PrevDatetime),
        BLANK(),
        FORMAT(Days, "0") & " Days " & FORMAT(Hours, "0") & " Hours"
    )




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
rajendraongole1
Super User
Super User

Hi @leorrn  - Please find the attached pbix you can use DAX to calculate the time difference between consecutive rows within each "Planta" group

 

 

rajendraongole1_0-1738851598039.png

 

 

Datediff_Days_Hours =
VAR PrevDatetime =
    CALCULATE(
        MAX('timdiff'[Datetime]),
        FILTER(
            'timdiff',
            'timdiff'[Planta] = EARLIER('timdiff'[Planta]) &&
            'timdiff'[Datetime] < EARLIER('timdiff'[Datetime])
        )
    )

VAR DiffMinutes = DATEDIFF(PrevDatetime, 'timdiff'[Datetime], MINUTE)

VAR Days = QUOTIENT(DiffMinutes, 1440)  -- 1440 minutes in a day
VAR Hours = QUOTIENT(MOD(DiffMinutes, 1440), 60)  -- Remaining minutes converted to hours

RETURN
    IF(
        ISBLANK(PrevDatetime),
        BLANK(),
        FORMAT(Days, "0") & " Days " & FORMAT(Hours, "0") & " Hours"
    )




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

Proud to be a Super User!





Greg_Deckler
Community Champion
Community Champion

@leorrn 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 __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
 ( __Current - __Previous ) * 1.



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...

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.