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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

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

@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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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