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
shashank_0107
New Member

time difference from different column in power query

time difference calculation issuetime difference calculation issue

hello everone, 

I am working on some project, here i need to calculate time difference between 2 time columns. but i am facing problem to calculate time when date changes in midnight after 00:00. As you can see in screenshot attached, the time 12.45am is of 27.05.2014. so when i am subtracting two TIME in power query it assumed that 12:45am is of 26.05.2014. that's y showing negative time difference. there is a huge data in which this error is coming. 

 

anyone guide me further. thanks in advance. 

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @shashank_0107, check this:

 

Result

dufoq3_0-1713079457763.png

 

version 1 (whole code)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjLTNzDVNzIwNFHSUTI0tDI0sDIwUAjwBfGMrExMQTxHX6VYHfxKDa1MLYEIxIuNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, From = _t, To = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Date", type date}, {"From", type time}, {"To", type time}}, "sk-SK"),
    Ad_DifferenceTime = Table.AddColumn(ChangedType, "Difference Time", each if [To] < [From] then (Date.AddDays([Date], 1)&[To]) - ([Date]&[From]) else ([Date]&[To]) - ([Date]&[From]), type duration),
    Ad_TotalHours = Table.AddColumn(Ad_DifferenceTime, "Total Hours", each Duration.TotalHours([Difference Time]), type number)
in
    Ad_TotalHours

 

 

version 2 (add this as custom column)

 

[ a = Duration.TotalHours([To] - [From]),
  b = if a < 0 then a + 24 else a
][b]

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

2 REPLIES 2
dufoq3
Super User
Super User

Hi @shashank_0107, check this:

 

Result

dufoq3_0-1713079457763.png

 

version 1 (whole code)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjLTNzDVNzIwNFHSUTI0tDI0sDIwUAjwBfGMrExMQTxHX6VYHfxKDa1MLYEIxIuNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, From = _t, To = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Date", type date}, {"From", type time}, {"To", type time}}, "sk-SK"),
    Ad_DifferenceTime = Table.AddColumn(ChangedType, "Difference Time", each if [To] < [From] then (Date.AddDays([Date], 1)&[To]) - ([Date]&[From]) else ([Date]&[To]) - ([Date]&[From]), type duration),
    Ad_TotalHours = Table.AddColumn(Ad_DifferenceTime, "Total Hours", each Duration.TotalHours([Difference Time]), type number)
in
    Ad_TotalHours

 

 

version 2 (add this as custom column)

 

[ a = Duration.TotalHours([To] - [From]),
  b = if a < 0 then a + 24 else a
][b]

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

watkinnc
Super User
Super User

Do your math on these values as type datetime, and you will not have this issue.

 

--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!!

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