Forum Discussion

vincenardo's avatar
vincenardo
Helper I
5 years ago

Convert Date Time in UTC to PST

I have a date / time field coming in from a connection to Salesforce. The date/time is stored in UTC and I need to transform it to PST (our local time).

 

I tried to edit the query and 'transform' it by changing the data type to DATE/TIME/TIMEZONE. All this did was add the time zone to the field..

 

 

How do I convert this value to the current time zone??

 

Thanks!!

 

13 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    To make the Datetime correct its value, as opposed to only adding the Time Zone, just use this one function, instead of changing the data type to datetimezone:

    = Table.TransformColumns(PriorStepOrTableName, {{"AbsentStart", DateTimeZone .ToLocal}, {"AbsentEnd", DateTimeZone.ToLocal}})

     

    This will add your local time zone to the datetime,  and also offset the actual value by the difference in hours from UTC to PST.

     

    --Nate

    • vincenardo's avatar
      vincenardo
      Helper I

      Can you give me some additional instructions on how to add this? The table name is ‘Resource Absence’ not sure where to add this…

      Thank you!

  • Anonymous's avatar
    Anonymous
    Not applicable

    Try this:

     

    = Table.TransformColumns(#"Resource Absence", {{"AbsentStart", DateTimeZone .ToLocal}, {"AbsentEnd", DateTimeZone.ToLocal}})

    • vincenardo's avatar
      vincenardo
      Helper I

      When editing the table, can you give me the steps on where to add it? I can't figure where to place it, I assume it's not in a new column. Thanks!

      • Anonymous's avatar
        Anonymous
        Not applicable

        Sorry, under your applied steps, right click on the last step, click "Add new step". Then copy and paste everything including the "=" into the formula bar. 

    • vincenardo's avatar
      vincenardo
      Helper I

      I recieved this error; ??

      Expression.Error: A cyclic reference was encountered during evaluation

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Ok, your last step name is #"Reordered Columns1", so:

         

        = Table.TransformColumns(#"Reordered Columns1", {{"AbsentStart", DateTimeZone .ToLocal}, {"AbsentEnd", DateTimeZone.ToLocal}})

         

         

  • Anonymous's avatar
    Anonymous
    Not applicable

    Can you tell me what the error says, or post the M code from the advanced editor?

    You could also try adding the word "each" before the DateTimeZone functions. Or, try replacing DateTimeZone.ToLocal with each DateTimeZone.SwitchZone(_, -8). Whatever you do though, you should do it  once, in other words, don't change the type to DateTimeZone earlier in the query, unless you use each DateTime.AddZone([AbsentStart], -8).

     

    --Nate

     

    --Nate

    • vincenardo's avatar
      vincenardo
      Helper I

      Here is the error -

       

      Expression.Error: We cannot convert the value #datetime(2020, 8, 18, 19, 30, 0) to type DateTimeZone.
      Details:
      Value=8/18/2020 7:30:00 PM
      Type=[Type]

      • Anonymous's avatar
        Anonymous
        Not applicable

        At any rate, if the column was originally type datetime, you can use DateTime.AddZone([AbsentStart], -8) to convert to your time zone, or just [AbsentStart] - #duration(0,8,0,0)

         

        --Nate