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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

How to find second difference between two dates in the same column

I'm trying to replicate in powerbi what I have in the excel screenshot. I want to find the time difference between two adjacent dates and output the time difference in seconds to the next column.

powerbiexcelexample.PNG

1 ACCEPTED SOLUTION
v-xuding-msft
Community Support
Community Support

Hi @Anonymous ,

You could try to add an index column and use the function of DATEDIFF to calculate. I created a calculated column to implement it that you can have a try.

let
    Source = Excel.Workbook(File.Contents("C:\Users\xueding\Desktop\DateTime.xlsx"), null, true),
    Sheet2_Sheet = Source{[Item="Sheet2",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet2_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"DateTime", type datetime}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1)
in
    #"Added Index"
Column = var last = CALCULATE(MAX('Table'[DateTime]),FILTER('Table','Table'[Index] =EARLIER( 'Table'[Index]) -1))
return
DATEDIFF(last,MAX('Table'[DateTime]),SECOND)

2.PNG

Best Regards,

Xue Ding

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

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

View solution in original post

2 REPLIES 2
v-xuding-msft
Community Support
Community Support

Hi @Anonymous ,

You could try to add an index column and use the function of DATEDIFF to calculate. I created a calculated column to implement it that you can have a try.

let
    Source = Excel.Workbook(File.Contents("C:\Users\xueding\Desktop\DateTime.xlsx"), null, true),
    Sheet2_Sheet = Source{[Item="Sheet2",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet2_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"DateTime", type datetime}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1)
in
    #"Added Index"
Column = var last = CALCULATE(MAX('Table'[DateTime]),FILTER('Table','Table'[Index] =EARLIER( 'Table'[Index]) -1))
return
DATEDIFF(last,MAX('Table'[DateTime]),SECOND)

2.PNG

Best Regards,

Xue Ding

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

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
kentyler
Solution Sage
Solution Sage

I think the standard advice for doing calculations involving adjacent cells is to do it in Excel....Excel is designed for it, Power BI is not. Then load your data into Power BI to use its features for analysis.





Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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