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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
Anonymous
Not applicable

Power Query - How to Calculate Time Difference with Previous Row & Adjacent Column?

 
2 ACCEPTED SOLUTIONS
speedramps
Community Champion
Community Champion

Thanks for your clear question and example data. I wish all members did that!

 

There are a few methods you can use, but try this one ....


Create a measure tp get the previous stop time

 

Previous stop time =
VAR mystart = SELECTEDVALUE(Microchips[Start time])
RETURN
CALCULATE(
    MAX(Microchips[Stop time]),
    ALL(),
    Microchips[Stop time] <= mystart
    )
 
Then create a measure to get the idle tile between the batches.
You can do this in any unit of measure, but I have used minutes in this example
 
Minutes idle =
DATEDIFF([Previous stop time],SELECTEDVALUE(Microchips[Start time]),MINUTE)


speedramps_0-1697810216327.png

Thanks for reaching out for help.

I put in a lot of effort to help you, now please quickly help me by giving kudos.

Remember we are unpaid volunteers and here to coach you with Power BI and DAX skills and techniques, not do the users job for them. So please click the thumbs up and accept as solution button. 

If you give someone a fish then you only give them one meal, but if you teach them how to fish then they can feed themselves and teach others for a lifetime.  I prefer to teach members on this forum techniques rather give full solutions and do their job. You can then adapt the technique for your solution, learn some DAX skills for next time and soon become a Power BI Super User like me. 

One question per ticket please. If you need to extend your request then please raise a new ticket.

You will get a quicker response and each volunteer solver will get the kudos they deserve. Thank you ! 


If you quote @speedramos in yout tickers then I will then received an automatic notification, and will be delighted to help you if you click the humbs up and accept as solution button. Thank you.

View solution in original post

mickey64
Super User
Super User

For your reference.

 

Step 1: I load the data with Power Query Editor twice as 'DATA_01' and 'DATA_02'.

    mickey64_0-1697810717090.png

    mickey64_1-1697810758796.png

 

Step 2: I add 'Index+1' column to 'DATA_02'.

    mickey64_2-1697810858597.png

    mickey64_3-1697810954843.png

 

Step 3: I merge 'DATA_01' query and 'DATA_02' query with 'Index' and 'Index+1'.

    mickey64_5-1697811138776.png

    mickey64_4-1697811025306.png

 

Step 4: I expand 'DATA_02'.

    mickey64_6-1697811217082.png

 

Step 5: I add 'Elapsed Time in Minutes' column to 'DATA_01' in Power BI Desktop.

    Elapsed Time in Minutes = DATEDIFF([DATA_02.STOP TIME],[START TIME],MINUTE)

 

    mickey64_7-1697811347372.png

 

View solution in original post

2 REPLIES 2
mickey64
Super User
Super User

For your reference.

 

Step 1: I load the data with Power Query Editor twice as 'DATA_01' and 'DATA_02'.

    mickey64_0-1697810717090.png

    mickey64_1-1697810758796.png

 

Step 2: I add 'Index+1' column to 'DATA_02'.

    mickey64_2-1697810858597.png

    mickey64_3-1697810954843.png

 

Step 3: I merge 'DATA_01' query and 'DATA_02' query with 'Index' and 'Index+1'.

    mickey64_5-1697811138776.png

    mickey64_4-1697811025306.png

 

Step 4: I expand 'DATA_02'.

    mickey64_6-1697811217082.png

 

Step 5: I add 'Elapsed Time in Minutes' column to 'DATA_01' in Power BI Desktop.

    Elapsed Time in Minutes = DATEDIFF([DATA_02.STOP TIME],[START TIME],MINUTE)

 

    mickey64_7-1697811347372.png

 

speedramps
Community Champion
Community Champion

Thanks for your clear question and example data. I wish all members did that!

 

There are a few methods you can use, but try this one ....


Create a measure tp get the previous stop time

 

Previous stop time =
VAR mystart = SELECTEDVALUE(Microchips[Start time])
RETURN
CALCULATE(
    MAX(Microchips[Stop time]),
    ALL(),
    Microchips[Stop time] <= mystart
    )
 
Then create a measure to get the idle tile between the batches.
You can do this in any unit of measure, but I have used minutes in this example
 
Minutes idle =
DATEDIFF([Previous stop time],SELECTEDVALUE(Microchips[Start time]),MINUTE)


speedramps_0-1697810216327.png

Thanks for reaching out for help.

I put in a lot of effort to help you, now please quickly help me by giving kudos.

Remember we are unpaid volunteers and here to coach you with Power BI and DAX skills and techniques, not do the users job for them. So please click the thumbs up and accept as solution button. 

If you give someone a fish then you only give them one meal, but if you teach them how to fish then they can feed themselves and teach others for a lifetime.  I prefer to teach members on this forum techniques rather give full solutions and do their job. You can then adapt the technique for your solution, learn some DAX skills for next time and soon become a Power BI Super User like me. 

One question per ticket please. If you need to extend your request then please raise a new ticket.

You will get a quicker response and each volunteer solver will get the kudos they deserve. Thank you ! 


If you quote @speedramos in yout tickers then I will then received an automatic notification, and will be delighted to help you if you click the humbs up and accept as solution button. Thank you.

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.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Feb2025 NL Carousel

Fabric Community Update - February 2025

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