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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
h4tt3n
Helper V
Helper V

Convert a columns of datatype datetimezone to datetime

Hello,

 

I have a table with timestamped data of datatype datetimezone, eg. 07-08-2020 08:00:00 + 02:00. 

How do I convert this to datetime in a way that takes tz info into account, eg. 07-08-2020 10:00:00?

Note that the + 02:00 part has been added to the timestamp, not simply stripped away.

If I try to manually convert the datatype, I get an error.

 

Cheers, Mike

11 REPLIES 11
dufoq3
Super User
Super User

EDIT: I've just noticed that this is almost 5 years old topic, but maybe it will help to someone...

Hi @h4tt3n, check this:

dufoq3_0-1739553498801.png

 

 

let
    Source = #table(null, {{"07-08-2020 08:00:00 +02:00"}}),
    Ad_CorrectDateTime = Table.AddColumn(Source, "CorrectDateTime", each let a = DateTimeZone.From([Column1]) in DateTime.From(a) + #duration(0, DateTimeZone.ZoneHours(a), DateTimeZone.ZoneMinutes(a), 0), type datetime)
in
    Ad_CorrectDateTime

 

 

If there is a space after + sign (as you showed us in your sample) use this:

dufoq3_0-1739553678936.png

 

let
    Source = #table(null, {{"07-08-2020 08:00:00 + 02:00"}}),
    Ad_CorrectDateTime = Table.AddColumn(Source, "CorrectDateTime", each let a = DateTimeZone.From(Text.Combine(Text.Split([Column1], " + "), " +")) in DateTime.From(a) + #duration(0, DateTimeZone.ZoneHours(a), DateTimeZone.ZoneMinutes(a), 0), type datetime)
in
    Ad_CorrectDateTime

 

 


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

ajerovich
Advocate II
Advocate II

Unsure if this is helpful to anyone. I was finding the solution getting the most kudos was substracting the timezone and this was the opposite of I wanted it. I ended up just making this stupidly long calculation instead:

 

if [DateTimeZone] is null then null else
DateTime.From(Text.BeforeDelimiter(Text.From([DateTimeZone], "en-AU"), "+"))
+
#duration(0,
Time.Hour(Time.From(Text.AfterDelimiter(Text.From([DateTimeZone], "en-AU"), "+"))),
Time.Minute(Time.From(Text.AfterDelimiter(Text.From([DateTimeZone], "en-AU"), "+"))),
Time.Second(Time.From(Text.AfterDelimiter(Text.From([DateTimeZone], "en-AU"), "+"))))

 

First part just makes sure the value isn't null.

Then it extracts the text before the delimiter "+" which in my case is the datetime, then adds the text after the "+" delimiter which is my timezone information.

Not exactly convient but does the job.

Don't forget to convert the column to a datetime once done.

AGo
Post Patron
Post Patron

First convert column to Date/Time/Zone and then here's my solution in PowerQuery:

Table.TransformColumns(#"PREVIOUS STEP NAME",{{"FIELD NAME", each if _=null then null else DateTime.From(_)+#duration(0,Number.From(Text.Start(Text.End(Text.From(_),6),3)),0,0), type datetime}})

Ajinkya369
Resolver III
Resolver III

Hi @h4tt3n ,

 

You can acheive this by splitting the column.Below i have attached the screenshots for your understanding.

 

This is your orignal column with DatetimezoneThis is your orignal column with Datetimezone

 

 

 

Split ColumnSplit Column

 

 Split column:

Select or enter delimeter : Custom

Use +

Split at: Left-most delimeter

Click on OK

 

 

Result :

 

 

Result,which is a datetime datatype.Result,which is a datetime datatype.

 

If your problem is solved please accept this as solution.

 

Thank you

orry, this simply discards timezone info, it doesn't incorporate it into the time. However, I have found the solution with a python script.

Hi, Can you share the steps you used to add the timestamp to the date using python

@Macc-PA I think this is the script. After all, it's one year ago, and we ended up not using it anyway.

import pytz
import datetime

tz = pytz.timezone('Europe/Copenhagen')
dt = datetime.datetime.utcnow()

offset_seconds = tz.utcoffset(dt).seconds

offset_hours = offset_seconds / 3600.0

#print ("{:+d}:{:02d}".format(int(offset_hours), int((offset_hours % 1) * 60)))

print( offset_hours )

Thanks for that - in the end we didnt need to use it.

 

The problem was solved by ensuring our odata datasource was sending the datetime as UTC, ie not converting it to local time.

 

Once that happened,  converting the column to DataTime applied the UTC offset as expected even for datetimes where daylight saving was involved.

artemus
Microsoft Employee
Microsoft Employee

In effect you are asking to convert a datetimezone value to a datetime in UTC:

let

    dtz = ...,

    udtz = DateTimeZone.SwitchZone(dtz, 0),

    dt = DateTimeZone.RemoveZone(udtz)

in

    dt

 

Mariusz
Community Champion
Community Champion

Hi @h4tt3n 

 

Convert into datetime but add another step

image.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDXNbDQNTIwMlAwsLAyMAAiBW0DIyClFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type datetimezone}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Column1", type datetime}})
in
    #"Changed Type1"
Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

I have tried to add the conversion as another step, but unfortunately this doesn't work either. Challenge remains unsolved.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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