Forum Discussion
Convert Date/Time in UTC to Local Time with Daylight savings
- 7 years ago
Hi Anonymous ,
I think there are many ways, for example I tried to find a pattern in order to catch the November first Sunday or March second Sunday, and for your specific needs, maybe this custom function could work:
(datetimecolumn as datetime) => let date = DateTime.Date(datetimecolumn), time = DateTime.Time(datetimecolumn), firstSundayOfNovember = Date.StartOfWeek(#date(Date.Year(date), 11, 7), Day.Sunday), SecondSundayOfMarch = Date.StartOfWeek(#date(Date.Year(date), 3, 14), Day.Sunday), isSummerTime = (date = SecondSundayOfMarch and time >= #time(1,0,0)) or (date > SecondSundayOfMarch and date < firstSundayOfNovember) or (date = firstSundayOfNovember and time >= #time(1,0,0)), timeZone = (7 - Number.From(isSummerTime))*-1, MDT = DateTime.From(date) + #duration(0,Time.Hour(time),Time.Minute(time),Time.Second(time)) + #duration(0, timeZone, 0, 0) in MDT
So for dates from March Second Sunday at 1:00am until November First Sunday at 12:59:59am you will get your datetime - 6 hours and for dates from November First Sunday 1:00am until March Second Sunday at 12:59:59am you will get your datetime - 7 hoursAccording to Saint Google, the time is changed after 1:00am if you need it to be changed after 12:00am instead just remove first and last condition from isSummerTime
If you have any question or if you find any error on the code, just let me know.
Regards,
Gian Carlo Poggi
- 7 years ago
Sure Anonymous ,
Right click on Queries pane and add a new Blank Query:
Then right click on this new query and select Advanced Editor:
In this new window erase all, paste the my code and click DONE:
Now that query was converted into a function, you can rename it if you like, for example to "UTC_to_MDT":
Then in order to use this function in your table you have different options, one option is going to your query or table, then click on Add Column / Invoke Custom Function, then put a name to this new column, select your function (in my case UTC_to_MDT) and select the column from your table you need to apply this function to (in my case "Date"):
And then you will see the new date added :
Hope this helps.
Regards,
Gian Carlo Poggi
Hi Anonymous ,
I think there are many ways, for example I tried to find a pattern in order to catch the November first Sunday or March second Sunday, and for your specific needs, maybe this custom function could work:
(datetimecolumn as datetime) =>
let
date = DateTime.Date(datetimecolumn),
time = DateTime.Time(datetimecolumn),
firstSundayOfNovember = Date.StartOfWeek(#date(Date.Year(date), 11, 7), Day.Sunday),
SecondSundayOfMarch = Date.StartOfWeek(#date(Date.Year(date), 3, 14), Day.Sunday),
isSummerTime = (date = SecondSundayOfMarch and time >= #time(1,0,0))
or
(date > SecondSundayOfMarch and date < firstSundayOfNovember)
or
(date = firstSundayOfNovember and time >= #time(1,0,0)),
timeZone = (7 - Number.From(isSummerTime))*-1,
MDT =
DateTime.From(date)
+ #duration(0,Time.Hour(time),Time.Minute(time),Time.Second(time))
+ #duration(0, timeZone, 0, 0)
in
MDT
So for dates from March Second Sunday at 1:00am until November First Sunday at 12:59:59am you will get your datetime - 6 hours and for dates from November First Sunday 1:00am until March Second Sunday at 12:59:59am you will get your datetime - 7 hours
According to Saint Google, the time is changed after 1:00am if you need it to be changed after 12:00am instead just remove first and last condition from isSummerTime
If you have any question or if you find any error on the code, just let me know.
Regards,
Gian Carlo Poggi
for replenishment and minor correction '>=' to '<' for proper operation on the day of change from summer time to winter time.
UTC(GMT) to CET
= (datetimecolumn as datetime) =>
let
date = DateTime.Date(datetimecolumn),
time = DateTime.Time(datetimecolumn),
lastSundayOfOctober = Date.StartOfWeek(#date(Date.Year(date), 10, 31), Day.Sunday),
lastSundayOfMarch = Date.StartOfWeek(#date(Date.Year(date), 3, 31), Day.Sunday),
isSummerTime = (date = lastSundayOfMarch and time >= #time(2,0,0))
or
(date > lastSundayOfMarch and date < lastSundayOfOctober)
or
(date = lastSundayOfOctober and time < #time(2,0,0)),
timeZone = 1 + Number.From(isSummerTime),
CET =
DateTime.From(date)
+ #duration(0,Time.Hour(time),Time.Minute(time),Time.Second(time))
+ #duration(0, timeZone, 0, 0)
in
CET
For summer time dates from March last Sunday at 2:00am until October last Sunday at 2:59:59am you will get your datetime +2 hours and for winter time dates from October last Sunday 3:00am until March last Sunday at 1:59:59am you will get your datetime +1 hour.
- Anonymous4 years agoNot applicable
Hi Jan_3
This is really great! I'm just not quite sure how to implement this in Power BI. I'll paste my query so for below. Could you give me a quick pointer on how to integrate this into the query? The datetimecolumn is called "created".let Source = *****, *****, #"Andre kolonner fjernet" = Table.SelectColumns(eplehuset_order_events, {"id", "fk_order_id", "fk_user_id", "created", "type", "data", "data_id"}), #"Filtrere ut kun statusendringer" = Table.SelectRows(#"Andre kolonner fjernet", each [type] = 20), #"Filter etter 01.11.19" = Table.SelectRows(#"Filtrere ut kun statusendringer", each [created] >= #datetime(2019, 11, 1, 0, 0, 0)), #"Fjerne tomme" = Table.SelectRows(#"Filter etter 01.11.19", each [fk_order_id] <> null and [fk_order_id] <> "" and [created] <> null and [created] <> ""), #"BRIDGE Statusendringer inkrementell-63726561746564-autogenerated_for_incremental_refresh" = Table.SelectRows(#"Fjerne tomme", each DateTime.From([created]) >= RangeStart and DateTime.From([created]) < RangeEnd) in #"BRIDGE Statusendringer inkrementell-63726561746564-autogenerated_for_incremental_refresh"
Thanks in advance.
Aleks- Anonymous4 years agoNot applicable
Hi Aleks
It is best to creat a new function:
right click on Queries -> New Query -> Other Sources -> Blank Query -> right click on this new query and select Advanced Editor -> insert code
= (datetimecolumn as datetime) =>
let ...
Then apply to the desired column: Add Column -> Invoke a custom function (maybe different in EN version)
good luck
- Syndicate_Admin4 years ago
Administrator
Good spot for catching this, but a minor correction - it should be "<" not "<="
- Anonymous4 years agoNot applicable
Thanks for your comment, you are right. I edited my post.