DateTimeZone.FromText Power Query Guide and Usage
In this article
The DateTimeZone.FromText function in Power Query is used to convert a text string representing a date and time with a time zone offset into a datetimezone data type. This is essential when importing data from APIs, JSON files, or logs that provide timestamps in ISO 8601 format (e.g., "2023-10-27T10:00:00+02:00"), as it ensures the offset from UTC is preserved rather than stripped away or converted to a local system time prematurely.
What You'll Need
- Microsoft Excel or Power BI Desktop.
- Access to the Power Query Editor (Get Data > Launch Power Query Editor).
- A dataset containing date/time values currently stored as text strings.
- Basic familiarity with the Power Query formula bar or the Custom Column dialog.
Step-by-Step
- Open Power Query Editor: Launch your application and load your data. If you are already in the main window, go to the Data tab and select Get Data or click Transform Data to enter the Power Query Editor environment.
- Identify the Target Column: Locate the column containing the text-based timestamps. Ensure the column data type is currently set to "Text". If it has already been auto-detected as a Date or DateTime, the time zone offset may have been lost; you may need to go back to the "Source" or "Changed Type" step in the Applied Steps pane and remove the automatic type conversion.
- Create a Custom Column: Go to the Add Column tab on the top ribbon and select Custom Column. This opens a dialog box where you can write the M formula.
- Enter the Function: In the Custom column formula box, type the following:
DateTimeZone.FromText([YourColumnName]). Replace[YourColumnName]with the actual name of the column containing your text timestamps. - Specify Culture (Optional): If your text strings use a regional format that differs from your system settings (for example, using a comma as a date separator), you can add a second argument to the function:
DateTimeZone.FromText([YourColumnName], "en-US"). This tells Power Query which culture's formatting rules to apply when parsing the text. - Validate the Result: Click OK. A new column will appear. You will notice the values now include the date, time, and the UTC offset (e.g., +05:00). The icon next to the column header will show a small clock and a globe, indicating the
datetimezonetype. - Clean Up: Right-click the original text column and select Remove. You can then rename your new custom column to something more descriptive, such as "Adjusted Timestamp".
- Finalize Data Type: Although the function returns a
datetimezonevalue, it is good practice to explicitly set the data type by clicking the icon in the column header and selecting DateTimeZone to ensure subsequent steps in your query treat the data correctly.
Common Mistakes
One of the most frequent errors is attempting to use this function on a column that has already been converted to a standard "DateTime" type. The DateTimeZone.FromText function requires a text input. If the data is already a DateTime object, Power Query will throw an expression error because it cannot perform a text-to-datetimezone conversion on a non-text value. Always ensure the input column is explicitly typed as Text before applying the function.
Another common mistake is ignoring the ISO 8601 standard. While DateTimeZone.FromText is flexible, it performs best with standardized formats. If your text is in a non-standard format (e.g., "Oct 27, 2023 10 AM EST"), the function may fail because "EST" is a named time zone rather than a numerical offset. Power Query generally requires the offset in the format +HH:mm or -HH:mm. If your data uses names like "PST" or "GMT", you will need to perform a text replacement to change those names into numerical offsets before using the function.
Users also frequently confuse DateTimeZone.FromText with DateTime.FromText. The latter strips the time zone information entirely, leaving you with a local date and time. If your analysis depends on comparing events across different global regions, using the wrong function will lead to significant data inaccuracies, as all timestamps will appear to be in the same local time regardless of where they originated.
Tips
To make your queries more robust, combine DateTimeZone.FromText with a "Try...Otherwise" block. This prevents the entire data refresh from failing if a single row contains a malformed text string. For example: try DateTimeZone.FromText([Timestamp]) otherwise null. This will place a null value in the cell if the text cannot be parsed, allowing you to filter for errors later without crashing the query.
If you need to convert the resulting datetimezone value to a specific local time (like your own company's headquarters), use the DateTimeZone.SwitchZone function immediately after. For example, if your data is in UTC and you want it in Eastern Standard Time (UTC-5), you can nest the functions: DateTimeZone.SwitchZone(DateTimeZone.FromText([Timestamp]), -5). This keeps the data accurate while making it readable for local stakeholders.
When dealing with very large datasets, avoid creating a custom column if you can perform the transformation within the "Transform" tab. While the Custom Column method is easier to visualize, using Table.TransformColumns in the Advanced Editor is more memory-efficient as it modifies the column in place rather than adding a new one to the table schema.
Troubleshooting
If you encounter an "Expression.Error: We cannot convert the value... to type Text" error, check the Applied Steps pane. Look for any "Changed Type" steps that occurred before your custom column. If Power Query automatically changed the column to a Date or DateTime type, the FromText function will fail. Delete that step or move your custom column step above it.
If the function returns a value but the time zone offset is incorrect or missing, inspect the raw text in your source data. If the text string does not actually contain an offset (like "+00:00"), DateTimeZone.FromText will assume the time is in the local time zone of the machine running the query. To fix this, you may need to manually append the offset to the text string using [Column] & "+00:00" before wrapping it in the DateTimeZone.FromText function.
In cases where you see a "Format not recognized" error, verify the culture settings. If the date is formatted as DD/MM/YYYY but your system is set to US English (MM/DD/YYYY), the function will fail on any day greater than 12. Use the culture argument mentioned in the step-by-step section (e.g., "en-GB" for UK format) to resolve this mismatch.
Frequently Asked Questions
Can this function handle UTC 'Z' notation?
Yes, DateTimeZone.FromText recognizes the "Z" suffix (Zulu time) as an indicator of UTC (offset +00:00) and will convert it correctly into a datetimezone value.
What is the difference between DateTimeZone and DateTime?
A DateTime value is a "naive" timestamp; it knows the date and time but not where in the world that time occurred. A DateTimeZone value is "aware," meaning it stores the offset from UTC, allowing for accurate conversions between different time zones.
Does this function work in Power BI and Excel?
Yes, because both Power BI and Excel use the same Power Query (M) engine, this function works identically in both applications.
Can I use this to convert a date that has no time zone?
You can, but it is not recommended. If the text lacks a time zone offset, Power Query will assign the local system time zone of the computer performing the refresh, which can lead to inconsistent data when the report is published to the cloud (where the server may be in a different time zone).
How do I remove the time zone offset after using this function?
If you have used DateTimeZone.FromText to ensure accuracy but now want a simple date/time for a chart, use DateTimeZone.RemoveZone or simply change the data type to "DateTime" using the column header menu.