Power Query Excel date and timezone

MartijnB

New Member
Joined
Jun 3, 2023
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I have a question about two columns (start_time and end_time).
Both columns have a 'date, 'time' and 'timezone'.
I want to calculate the columns into the right time in a extra column.
Example:

When you look at the first row
Start_time = 10-6-2023 12:30:00 +02:00
End_time = 10-6-2023 18:00:00 + 02:00

This is what I want:
Start_time = 14:30:00
End_time = 20:00:00
Third column = 05:30:00 (hours between the start and end time)

Sometimes the end_time date is greater then the start_time date. Example:
Start_time = 24-11-2022 23:00:00 +01:00
End_time = 25-11-2022 03:00:00 +01:00

What I want:
Start_time = 24:00:00 or 00:00:00
End_time = 04:00:00
Third column = 04:00:00 (hours between the start and end time)


Hope this is possible.
Martijn
 

Attachments

  • Time.png
    Time.png
    8.6 KB · Views: 10

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    fnDateTimeFrom = (dt as text) as datetime => DateTime.From(Text.BeforeDelimiter(dt, " +"), "en-GB") + Duration.From(Text.AfterDelimiter(dt, "+")),
    tbl = Table.TransformColumns(Source, {{"start_time", fnDateTimeFrom}, {"end_time", fnDateTimeFrom}}),
    tbl1 = Table.AddColumn(tbl, "duration", each [end_time]-[start_time]),
    Result = Table.TransformColumnTypes(tbl1,{{"start_time", type time}, {"end_time", type time}, {"duration", type duration}})
in
    Result

Book2
ABCDEFG
1start_timeend_timestart_timeend_timeduration
210-6-23 12:30:00 +02:0010-6-23 18:00:00 +02:0014:30:0020:00:000.05:30:00
311-2-23 23:00:00 +01:0012-2-23 00:00:00 +01:0000:00:0001:00:000.01:00:00
412-6-23 22:00:00 +00:0013-6-23 04:00:00 +00:0022:00:0004:00:000.06:00:00
5
Sheet2
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    fnDateTimeFrom = (dt as text) as datetime => DateTime.From(Text.BeforeDelimiter(dt, " +"), "en-GB") + Duration.From(Text.AfterDelimiter(dt, "+")),
    tbl = Table.TransformColumns(Source, {{"start_time", fnDateTimeFrom}, {"end_time", fnDateTimeFrom}}),
    tbl1 = Table.AddColumn(tbl, "duration", each [end_time]-[start_time]),
    Result = Table.TransformColumnTypes(tbl1,{{"start_time", type time}, {"end_time", type time}, {"duration", type duration}})
in
    Result

Book2
ABCDEFG
1start_timeend_timestart_timeend_timeduration
210-6-23 12:30:00 +02:0010-6-23 18:00:00 +02:0014:30:0020:00:000.05:30:00
311-2-23 23:00:00 +01:0012-2-23 00:00:00 +01:0000:00:0001:00:000.01:00:00
412-6-23 22:00:00 +00:0013-6-23 04:00:00 +00:0022:00:0004:00:000.06:00:00
5
Sheet2
Hi,

Thanks!
Just a question because I'm a beginner with Power Query.
Where should i put the code? In a new column or....
 
Upvote 0
open a blank query
open the advanced editor
replace the code in there with the code above
change Table4 in the first line to the name of your table
 
Upvote 0
open a blank query
open the advanced editor
replace the code in there with the code above
change Table4 in the first line to the name of your table
Ok, I get the next error.
Expression.Error: The column from the table has not found.
Details:
start_time

I changed the start_time and date_time column form text into date/time/timezone, see image.
Then I used your code and get the error.
 

Attachments

  • Change start_time.png
    Change start_time.png
    19.4 KB · Views: 9
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top