Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Datediff for duration

Team,

I want to find duration in hours (upto 2 decimal places) from two datetime formatted cells. While i use datediff but it shows half negative and half postive integer values. i have attached the snap of the cell.

 

Basically i want duration between Enddatetime and begindatetime in hours.

can someone help with a proper query

 

regards

  • Helllo Anonymous 

     

    if you need a solution in power query, add a new colunn and use this formula

    Number.Round(Duration.TotalHours([EndDateTime]-[BeginDateTime]), 2)

    Here the complete example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjLQMzTUMzJQMDSwMjFT0lEyMoUKGJhbGZorxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [BeginDateTime = _t, EndDateTime = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"BeginDateTime", type datetime}, {"EndDateTime", type datetime}}, "de-DE"),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Duration in hours", each Number.Round(Duration.TotalHours([EndDateTime]-[BeginDateTime]), 2), type number)
    in
        #"Added Custom"

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

     

4 Replies

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Helllo Anonymous 

     

    if you need a solution in power query, add a new colunn and use this formula

    Number.Round(Duration.TotalHours([EndDateTime]-[BeginDateTime]), 2)

    Here the complete example

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjLQMzTUMzJQMDSwMjFT0lEyMoUKGJhbGZorxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [BeginDateTime = _t, EndDateTime = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"BeginDateTime", type datetime}, {"EndDateTime", type datetime}}, "de-DE"),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Duration in hours", each Number.Round(Duration.TotalHours([EndDateTime]-[BeginDateTime]), 2), type number)
    in
        #"Added Custom"

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thankyour so much!!, it worked just the way as was desired!!!!!

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hey there. For me, it's always easier to do duration arithmetic with the numbers:

     

    Table.AddColumn(PriorStep, "Duration",  each Number.Round((Number.From([EndDateTime])-Number.From([BeginDateTime]))*24,2))

     

    That's it!--Nate