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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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

 

regardspic.JPG

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
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"

Jimmy801_0-1606285246952.png

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

 

View solution in original post

4 REPLIES 4
watkinnc
Super User
Super User

Actually @Jimmy801 that's pretty slick. 


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
watkinnc
Super User
Super User

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

 


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
Jimmy801
Community Champion
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"

Jimmy801_0-1606285246952.png

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

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors