Power Query DateTimeZone.ToUtc: How to Convert Times
In this article
DateTimeZone.ToUtc converts a datetimezone value to Coordinated Universal Time (UTC) while preserving the original time zone offset information. Use it when you need a consistent reference time across different zones, such as when combining data from multiple regions or preparing timestamps for storage. The function takes a single datetimezone argument and returns a datetimezone value shifted to UTC.
What You'll Need
- Power Query Editor open (in Excel, Power BI Desktop, or another host that supports Power Query).
- A column or value of type datetimezone. If your data is stored as text or datetime, you will need to convert it first.
- Basic familiarity with adding a custom column or editing an existing step in the Power Query Editor.
Step-by-Step
- Load your data into Power Query Editor. In Excel, go to Data > Get Data > From Table/Range (or another source). In Power BI Desktop, use Home > Transform data to open the editor.
- Locate the column that contains datetimezone values. If the column header shows a calendar-with-clock icon and the data type is DateTimeZone, you are ready. If not, select the column, go to the Transform tab, and set the data type to DateTimeZone. Note that changing data type may produce errors if the original text does not include a time zone offset.
- With the column selected, go to the Add Column tab and click Custom Column. (You can also use Transform > Custom Column if you want to replace the existing values, but adding a new column is safer for verification.)
- In the Custom Column dialog, enter a name for the new column, such as "UTC Time".
- In the formula box, type the function call. For a column named "Timestamp", the formula is:
DateTimeZone.ToUtc([Timestamp]). Replace "Timestamp" with your actual column name. The column name is case-sensitive and must be enclosed in square brackets. - Click OK. Power Query adds the new column with values converted to UTC. The resulting values still have a time zone offset of +00:00, but the date and time reflect the UTC equivalent of the original instant.
- Verify a few rows manually. For example, if the original value was 2024-01-15T10:00:00+02:00, the UTC value should be 2024-01-15T08:00:00+00:00. The function subtracts the offset to normalize to UTC.
- If the results look correct, you can remove the original column if desired, or keep both for reference. Then close and load the query (Home > Close & Load in Excel, or Home > Close & Apply in Power BI).
Common Mistakes
- Using a datetime column instead of datetimezone. DateTimeZone.ToUtc expects a datetimezone value. If you pass a datetime (which has no offset), you will get an error or unexpected results. Convert the column to DateTimeZone first, ensuring the original data includes an offset.
- Incorrect column name. The column name in the formula must match exactly, including spaces and capitalization. If the name contains spaces, enclose it in square brackets as usual, but double-check spelling.
- Assuming the function changes the displayed time zone. DateTimeZone.ToUtc returns a datetimezone value with offset +00:00. It does not change the underlying instant; it only expresses it in UTC. If you then change the data type to DateTime, the offset is dropped and the UTC time is shown as a local time without zone information.
- Forgetting to handle nulls. If the column contains nulls, DateTimeZone.ToUtc will return null for those rows. That is usually fine, but be aware if you later perform calculations that assume non-null values.
- Using the function on text values. If the column is text, you must first convert it to DateTimeZone using the data type dropdown or by using DateTimeZone.FromText. Otherwise, you will get an error.
Tips
- Use DateTimeZone.ToUtc when you need a consistent time basis for merging or comparing data from different time zones. For example, if you have sales transactions from New York and London, converting both to UTC lets you compare timestamps directly.
- If you want to convert to a specific time zone other than UTC, use DateTimeZone.SwitchZone instead. DateTimeZone.ToUtc is a shortcut for switching to UTC (offset 0).
- To remove the time zone offset after converting to UTC, change the data type to DateTime. This is useful if you need a simple datetime value for calculations that do not require zone awareness.
- When working with large datasets, adding a custom column with DateTimeZone.ToUtc is efficient. However, if you only need to convert once, you can also use the Transform tab's "Convert to UTC" option if available in your version. The custom column approach gives you more control and visibility.
- Remember that Power Query is case-sensitive for column names. If your column is named "timestamp" (lowercase), using [Timestamp] will cause an error.
Troubleshooting
- Error: "We cannot convert the value ... to type DateTimeZone." This means the input value is not a valid datetimezone. Check the column data type and the format of the original data. If the data is text, ensure it includes a time zone offset (e.g., "2024-01-15T10:00:00+02:00"). Use DateTimeZone.FromText to convert explicitly.
- Error: "The field 'Timestamp' of the record wasn't found." The column name in the formula does not match any column in the table. Verify the exact name, including spaces and capitalization. You can rename the column to a simpler name without spaces to avoid issues.
- Results are off by an hour or more. Check the original offset. If the original data had an offset that does not match the actual time zone (e.g., daylight saving time issues), the conversion will reflect the offset as given. Ensure the source data has the correct offset for the instant.
- Null values appear where there were none. If the original column had nulls, DateTimeZone.ToUtc returns null. If you expected a value, check the source data for blanks or errors.
- The function is not recognized. Ensure you are using a version of Power Query that supports DateTimeZone.ToUtc. It is available in Excel 2016 and later, Power BI Desktop, and Power Query Online. If you are on an older version, you may need to update.
Frequently Asked Questions
What is the difference between DateTimeZone.ToUtc and DateTimeZone.SwitchZone?
DateTimeZone.ToUtc converts a datetimezone value to UTC (offset +00:00). DateTimeZone.SwitchZone changes the time zone offset to a specified number of hours and minutes, adjusting the datetime accordingly. ToUtc is essentially SwitchZone with offset 0.
Can I use DateTimeZone.ToUtc on a column that contains datetime values without time zone information?
No. DateTimeZone.ToUtc requires a datetimezone value. If you have a datetime column, you must first convert it to datetimezone by assigning a time zone (e.g., using DateTimeZone.From or DateTimeZone.FromText with an offset). Without an offset, the function cannot determine the UTC equivalent.
Does DateTimeZone.ToUtc account for daylight saving time?
DateTimeZone.ToUtc uses the offset provided in the datetimezone value. It does not perform any time zone database lookups or adjust for daylight saving time. The offset in the value is assumed to be correct for that instant. If the original data has the wrong offset (e.g., due to DST transitions), the conversion will be incorrect.
How do I convert a UTC datetimezone back to local time?
Use DateTimeZone.SwitchZone with the desired offset. For example, to convert to Eastern Standard Time (UTC-5), use DateTimeZone.SwitchZone([UtcColumn], -5, 0). Note that this does not handle daylight saving time automatically; you need to provide the correct offset for the date.
Is DateTimeZone.ToUtc available in all Power Query environments?
It is available in Excel 2016 and later, Power BI Desktop, Power Query Online, and other hosts that use the Power Query engine. If you are using an older version, check for updates or use an alternative method such as DateTimeZone.SwitchZone with offset 0.
What happens if the input column has a mix of datetimezone and null values?
DateTimeZone.ToUtc returns null for null inputs. For non-null datetimezone values, it returns the UTC equivalent. The column will remain of type datetimezone, with nulls preserved.