Power Query Error on DateTimeZone Conversion

Devendra Gundecha 1 Reputation point
2021-06-04T06:02:24.92+00:00

Hi,

I'm trying to convert timezone for a column but I keep getting errored out as below:

Expression.Error: We cannot convert the value #datetime(2021, 6, 4, 10, 0, 0) to type DateTimeZone.
Details:
Value=6/4/2021 10:00:00 AM
Type=[Type]

Formula Used: = DateTimeZone.SwitchZone([Start Time], 0)

Details:
let
Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{<!-- -->{"Change Request", type text}, {"Start Time", type datetime}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each DateTimeZone.SwitchZone([Start Time], 0))
in
#"Added Custom"

Community Center | Not monitored
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lz._ 38,106 Reputation points Volunteer Moderator
    2021-06-04T07:25:03.527+00:00

    Hi @Devendra Gundecha

    Your query code:

    102383-querycode.png

    The error clearly indicates the reason: the type of the value ([Start Time]) isn't "good":

    102384-repro.png

    According to doc. function DateTimeZone.SwitchZone expects a value of type datetimezone but you provide a value of type datetime (your #"Changed Type" step):

    102385-datettimezonedoc.png

    So change your #"Changed Type" step with:

    #"Changed Type" = Table.TransformColumnTypes(Source,  
        {{"Change Request", type text}, {"Start Time", type datetimezone}}  
    ),  
    

    OR, if you want column [Start Time] to be of type datetime, then convert the value to type datetimezone as follow when adding your custom column:

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each  
        DateTimeZone.SwitchZone(  
            DateTimeZone.From([Start Time]),  
            0  
        )  
    )  
    

    Hope this all makes sense & helps

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.