Forum Discussion

leorrn's avatar
leorrn
Regular Visitor
1 year ago
Solved

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 

 

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

     

     

     

     

    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"
        )

2 Replies

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

     

     

     

     

    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"
        )