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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Janica123
Helper I
Helper I

Calculate time difference in one column

Hi everyone, 

 

My data looks similar to the first three columns. 

 

Janica123_1-1650975466270.png

 

Now, what I need to do is to calculate the timedifference of the values in column "Time" for each order and date and put the outcome into a new column named "Timediff". 

 

Is there an easy way to do this? Thank you for your help!

 

 

 

1 ACCEPTED SOLUTION

Hi, @Janica123 

 

Yes, you need to create the datetime measure and use it to create datediff measure to display your results.

Like this:

Datetime = SELECTEDVALUE('Table'[Date])+SELECTEDVALUE('Table'[Time])
Measure = 
VAR a =
    MAXX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [Index] < SELECTEDVALUE ( 'Table'[Index] )
                && [Order] = SELECTEDVALUE ( 'Table'[Order] )
        ),
        [Datetime]
    )
RETURN
    DATEDIFF ( a, [Datetime], MINUTE )

vjaneygmsft_0-1651219760286.png

 

 

Did I answer your question? Please mark my reply as solution. Thank you very much.
If not, please feel free to ask me.

Best Regards,
Community Support Team _ Janey

 

View solution in original post

6 REPLIES 6
Whitewater100
Solution Sage
Solution Sage

Hi:

You can try this calculated column. I named your table "Data". Where a column had a time like 8:20, I changed each Column with time to DATE/TIME data type. 

I hope this solves the question. Thanks..

Time Difference =
var PreviousRow = LOOKUPVALUE(Data[Time],
Data[Index],
Data[Index] -1
)
var Mvalue = CALCULATE(MIN(Data[Time]), ALLEXCEPT(Data,Data[Order]))
var Last = IF(Data[Time] = Mvalue, BLANK(), PreviousRow - Data[Time])
var result = IF(Last = BLANK(), BLANK(),
DATEDIFF(Data[Time], PreviousRow,MINUTE))
return
result
 
Here is an image with the steps and then all folded into one calculation:
Whitewater100_0-1650980945954.png

 

Thank you very much for your help!

But now there is one issue left. My data looks now similar to this. 

Janica123_0-1651039148026.png

 

The problem is, that the time difference should be based on one order only and should also include the datediff. Meaning that if I filter now for order A the data looks like this. The green colored columns show how it should look like. 

 

Janica123_3-1651040016421.png

If you have any suggestions I would be very happy!! 

 

 

Is it possible that we need to solve this with a measure not a calculated column? 

Hi, @Janica123 

 

Yes, you need to create the datetime measure and use it to create datediff measure to display your results.

Like this:

Datetime = SELECTEDVALUE('Table'[Date])+SELECTEDVALUE('Table'[Time])
Measure = 
VAR a =
    MAXX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [Index] < SELECTEDVALUE ( 'Table'[Index] )
                && [Order] = SELECTEDVALUE ( 'Table'[Order] )
        ),
        [Datetime]
    )
RETURN
    DATEDIFF ( a, [Datetime], MINUTE )

vjaneygmsft_0-1651219760286.png

 

 

Did I answer your question? Please mark my reply as solution. Thank you very much.
If not, please feel free to ask me.

Best Regards,
Community Support Team _ Janey

 

Hi:

If you want result to be positive the code would be the same until the last part. 

here is the last line if you want to see minutes in positive values:

result*-1

I forgot to mention,  I added an index column in Power Query. 

Transform Data > Add New Column > Index Column > Index From 1. That helps getting previous row.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.