Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query DateTimeZone ZoneHours Function

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Use the Power Query DateTimeZone.ZoneHours function to extract the numeric offset from Coordinated Universal Time (UTC) for a given datetime zone value, which is essential for standardizing time data across different geographic regions.

What You'll Need

  • A working computer with Microsoft Excel, Power BI Desktop, or the Power Query Editor in Power Automate.
  • A dataset containing datetime zone values, either as a column of records (e.g., {#datetimezone(2023,1,1,12,0,0), 1, 0}) or as a separate column for the offset hours.
  • A basic understanding of how Power Query M code functions and how to access the Advanced Editor.

Step-by-Step

  1. Open the Power Query Editor: Launch your application (Excel, Power BI Desktop, or Power Automate). Navigate to the "Data" tab and select "Get Data" to connect to your source (e.g., Excel Workbook, SQL Server, or CSV). Once the data loads, you will be automatically taken to the Power Query Editor window.
  2. Locate the Target Column: In the Applied Steps pane or the main query editor grid, identify the column that contains the datetime zone values you wish to analyze. Ensure that the values are in a format recognized by Power Query as a DateTimeZone type.
  3. Access the Formula Bar: Click on the header of the target column to select it. Look for the formula bar located at the top of the editor grid, just above the column headers. Click into the formula bar to begin editing the formula for that column.
  4. Enter the Function: Type the following syntax into the formula bar: DateTimeZone.ZoneHours([YourColumnName]). Replace [YourColumnName] with the actual name of your column containing the datetime zone data.
  5. Apply the Change: Press the Enter key on your keyboard. The column will update to display the numeric value representing the offset in hours from UTC. For example, if the input is a datetime zone representing New York time, the result will likely be -5 or -4 depending on Daylight Saving Time status.
  6. Verify the Results: Scan through the resulting column to ensure the values are accurate. You can sort the column to quickly see how the time zones differ across your dataset.
  7. Save and Close: Once you are satisfied with the transformation, click the "Close & Apply" button (or "Done" in Power Automate) to return to your main application. The changes will be reflected in your report or data model.

Common Mistakes

One of the most frequent errors is attempting to apply the function to a standard DateTime column instead of a DateTimeZone column. Power Query treats these two types differently: a DateTime lacks the timezone information required to calculate an offset, while a DateTimeZone includes it. If you try to use DateTimeZone.ZoneHours on a standard date, you will receive an error. Always ensure your data is imported or converted to the correct type by checking the column's data type icon in the viewer pane.

Another common issue arises from data type mismatches imported from sources like CSV files. CSVs are text-based and do not inherently store time zone information or data types. If you import a CSV, Power Query often defaults the date column to text or to a local system time without a zone. You must first use the "Change Type" feature (Transform tab) to convert the text to a DateTime or DateTimeZone type before the ZoneHours function will work. Failing to do this results in the function returning null or an error message.

Users also often confuse the ZoneHours function with DateTimeZone.FixedLocalNow(). While both deal with time zones, they serve opposite purposes. ZoneHours extracts the existing offset from a specific record or value. DateTimeZone.FixedLocalNow(), on the other hand, generates a new current timestamp based on the computer's local system time and forces that specific timezone offset onto it. Using the extraction function where a generation function is needed will not update the data to the current time.

Tips

When working with datetime zone data, it is highly recommended to use the "DateTimeZone.Kind" function alongside ZoneHours. The kind indicates whether the time is Local, UTC, or Union. This helps prevent logic errors where you might apply a standard UTC offset to a time that is already in UTC. For instance, if a value is UTC, adding a standard offset would result in an incorrect local time.

To make your queries more readable and easier to debug, consider wrapping the function in a named function. You can do this by selecting the step in the Applied Steps pane, going to the View tab, and clicking "Advanced Editor.” Define a function like GetTimeZoneOffset as number => DateTimeZone.ZoneHours(GetTimeZoneOffset). This allows you to reuse the logic in other queries or steps without repeating the code.

Always check your data for missing values. If your source data contains empty cells or nulls, the ZoneHours function will return null. Depending on your needs, you might want to replace these nulls with a standard value (like 0) or handle them with error handling functions like try and otherwise to ensure your final report does not crash.

Troubleshooting

If you see an error message when running the formula, the most likely cause is that the column referenced does not exist or has been renamed. Check the formula bar to ensure the column name matches exactly what is displayed in the grid. Power Query is case-sensitive, so and are treated as two different columns.

If the function returns null values, it implies that the input column contains nulls or the data type is incorrect. Go back to the source data and check for empty rows. Additionally, ensure you have performed the necessary type conversion steps. If you are unsure of the data type, right-click on the column header and select "Change Type" > "Detected Type" to let Power Query guess, or manually select "DateTimeZone" if you know the data includes timezone info.

If you are working in Power Automate and the function does not appear to work as expected, verify that the flow is running in the correct environment. Power Automate flows often run on different time zones depending on the connection used. If you are expecting a specific offset but getting a different one, it might be due to the flow's execution time zone settings, which can be adjusted in the flow's configuration under "Settings" > "Run-only user" or similar options depending on the specific connector.

Frequently Asked Questions

What is the difference between ZoneHours and ZoneMinutes?

ZoneHours returns the offset in whole hours, while ZoneMinutes returns the total offset in minutes. You typically use ZoneHours for display purposes (e.g., showing "UTC-5") and ZoneMinutes for calculations that require finer granularity, though Power Query often handles these conversions automatically.

Why does the function return -4 instead of -5?

ZoneHours returns the offset based on Daylight Saving Time (DST) rules. If the datetime zone value falls within a DST period, the offset will be one hour different from standard time. Standard offsets are static, but daylight offsets vary by region and date.

Can I use this function on columns that only contain dates?

ZoneHours requires a full DateTimeZone value. If your column only has a date (e.g., 2023-10-01), you cannot use this function directly. You would first need to add a default time and a default time zone to the column using the Date.ToDateTime and DateTimeZone.FromDateTime functions.

How do I handle time zones that are not in the list of supported zones?

ZoneHours expects a specific record format representing a time zone. If your data uses a custom label (like "EST") that is not a standard IANA time zone identifier, you will need to map that label to the correct record format using a lookup table or a conditional statement before you can extract the hours.

Is the output always an integer?

ZoneHours is designed to return a whole number representing hours. While the underlying data might support fractional hours for complex offsets, the function itself will truncate or round the result to an integer value based on the standard definition of the offset.