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
Anonymous
Not applicable

Calculating time between two rows in the same column

Hi, 

 

I am trying to create a new column that displays the how many minutes have passed between each row in the [Time] Column. Basically, I just need to subtract the bottom row from the above row for all rows. Here's what I have tried, but there is not enough memory to complete the operation. I am wondering if anyone knows a better way around this. 

 

Josh97Ellis_1-1596643733299.png

 

 

1 ACCEPTED SOLUTION

 🙂@Anonymous 

 

Diff =
VAR __Previous =
    MINX (
        FILTER (
            'Table',
            'Table'[Date] = EARLIER ( 'Table'[Date] )
                && 'Table'[Time] > EARLIER ( 'Table'[Time] )
        ),
        'Table'[Time]
    )
VAR __diff = __Previous - 'Table'[Time]
RETURN
    __diff



Did I answer your question? Mark my post as a solution!
Appreciate with a kudos
🙂

 

 


Regards,
Nandu Krishna

View solution in original post

13 REPLIES 13
lit2018pbi
Resolver II
Resolver II

Hi Josh,

 

Step 1: You can add a new column tp concatenate date and time using the following Dax

            Date and Time = CONCATENATE('time diff'[Date].[Date]&" ",'time diff'[Time])
concat.PNG

 Step 2: Now find the Lag Date and Time using the following Dax

     Lag Date and Time = CALCULATE(MAX('time diff'[Date and Time]),FILTER('time diff','time diff'[Material] = EARLIER('time diff'[Material])&&'time diff'[Date and Time]<EARLIER('time diff'[Date and Time])))
Lag.PNG

 Step 3: Find out the time difference using the following Column

            Difference in Minutes = DATEDIFF('time diff'[Lag Date and Time],'time diff'[Date and Time],MINUTE)

Difference.PNG

 

There are many steps involved here but you will get the result by implementing the above!
Thanks!
 

This was so helpful for me! I needed to calculate a time difference based on an ID, and this did so perfectly!

v-lionel-msft
Community Support
Community Support

Hi @Anonymous ,

 

Like this?

v-lionel-msft_0-1596681911097.png

Or like this?

v-lionel-msft_1-1596682031117.png

Or @nandukrishnavs 's formula?

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

@v-lionel-msft Hi, I need it to be the number of minutes (duration) between each time stamp. So in excel, it would look like this:

 

Josh97Ellis_1-1596726659092.png

 

Greg_Deckler
Super User
Super User

@Anonymous - 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/339586



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

@Anonymous 

 

Try this 

Diff =
VAR __Previous =
    MAXX (
        FILTER (
            'Table',
            'Table'[Date] = EARLIER ( 'Table'[Date] )
                && 'Table'[Time] < EARLIER ( 'Table'[Time] )
        ),
        'Table'[Time]
    )
VAR __diff = 'Table'[Time] - __Previous
RETURN
    __diff



Did I answer your question? Mark my post as a solution!
Appreciate with a kudos
🙂


Regards,
Nandu Krishna

Anonymous
Not applicable

@nandukrishnavs  This is close, it looks like it needs to be "shifted up" one row. So basically, the value I get in row 2 should be the value for row 1, etc.. You'll see that in the picture. 

 

Josh97Ellis_0-1596727205695.png

 

pls, I am also stuck here. The value in row 2, should be in row 1

 

 

@Anonymous you have to change colum formatting.

Set the data type as Time. Format as HH:mm



Did I answer your question? Mark my post as a solution!
Appreciate with a kudos
🙂


Regards,
Nandu Krishna

Anonymous
Not applicable

@nandukrishnavs Yeah, i got the format correct after I sent that, I was just in the wrong place. 

 

How can I adjust the formula so that each row in that calculated column is essentially shifted up one row. per my last reply. 

 

Thanks

 🙂@Anonymous 

 

Diff =
VAR __Previous =
    MINX (
        FILTER (
            'Table',
            'Table'[Date] = EARLIER ( 'Table'[Date] )
                && 'Table'[Time] > EARLIER ( 'Table'[Time] )
        ),
        'Table'[Time]
    )
VAR __diff = __Previous - 'Table'[Time]
RETURN
    __diff



Did I answer your question? Mark my post as a solution!
Appreciate with a kudos
🙂

 

 


Regards,
Nandu Krishna

Good afternoon @nandukrishnavs.

I have a similar situation. I tried the two recommended solutions, resulting in what is shown in the following image:

RichardFig1_0-1632865434549.png

Sometimes it works, so the solution I need should not be far away. To explain my case, each row represents a movement (or modification of the ticket) this group for a help desk ticket (14 movements). I have a column with the date and another with the modification time. I also concatenated both columns in the "Date & Time" column.

I need to calculate how much time passed between each movement. For example, from the first move to the second passed 00h:40m:59s.

The result can be in duration format or could indicate the number of seconds in total and then convert it to minutes or hours.

I hope you can help me.

Best regards

Richard

amitchandak
Super User
Super User

@Anonymous , Try like

diff =
var _max = maxx(filter(Table, [date] = earlier([date]) && [time] < earlier([time]) && [material] = earlier([material])),[time])
return
[time] - maxx(filter(Table, [date] = earlier([date]) && [time] = _max && [material] = earlier([material])),[time])

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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.

Top Kudoed Authors