Smart Systems for Home

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

How to Use Power Query DateTimeZone.UtcNow Function

· 5 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

The Power Query DateTimeZone.UtcNow function retrieves the current Coordinated Universal Time (UTC) as a datetimezone value. This is essential when you need a timestamp that is not affected by daylight saving time changes or local time zone offsets. You do not need any software installation to use this built-in function, as it exists within the Power Query Editor in both Excel and Power BI Desktop.

What You'll Need

  • A computer with Microsoft Excel (2016 or later) or Power BI Desktop installed.
  • Access to the Power Query Editor, which is found under the Data tab in Excel or the Home tab in Power BI.
  • A basic understanding of Power Query formulas, also known as M.

Step-by-Step

  1. Open the Power Query Editor. In Excel, go to the Data tab and select Get Data > From Table/Range if you have an existing table. If you want to start fresh, go to Data > From Table/Range and select your data. In Power BI Desktop, go to Home > Get Data and choose your data source, or simply open your query from the Queries pane.
  2. Navigate to the Custom Column formula bar. Once your data is loaded into the Power Query Editor, click on any column header to ensure the data is selected. Look for the "Add Column" tab at the top of the ribbon. Click on "Custom Column" in the ribbon.
  3. Enter the function. In the "Custom Column" dialog box, name your new column (for example, "Current UTC Time"). In the formula field, type the function name exactly as it appears. The syntax is DateTimeZone.UtcNow(). Ensure you use the correct closing parenthesis. The formula bar will now show DateTimeZone.UtcNow().
  4. Apply the changes. Click the "OK" button to create the new column. Power Query will evaluate the function for every row in your table. Since this function relies on the system clock of the machine running Power Query, all rows should now contain the same UTC datetimezone value.
  5. Verify the result. Expand the new column to view the data. The value will be in the format of a datetimezone, such as 2023-10-27T14:30:00+00:00. The +00:00 indicates the offset from UTC. You can change the data type of this column to "Datetime Zone" from the Transform tab to ensure it displays correctly.

Common Mistakes

One of the most frequent errors when working with date functions is confusing the DateTime.UtcNow() function with DateTimeZone.UtcNow(). The former returns a standard datetime (without a time zone offset), while the latter returns a datetimezone (with an offset). If your data model requires time zone awareness for downstream analysis, using the non-timezone version can lead to incorrect calculations when converting between regions.

Another common issue arises from the reliance on the local system clock. If the machine running Power Query is set to the wrong time, the UTC function will return an incorrect value. Ensure your operating system time is set to automatic and is accurate before running this query. Power Query does not fetch the time from an external time server; it simply reads the local system time.

Tips

When combining DateTimeZone.UtcNow() with other functions, remember that the output is a datetimezone type. To perform simple date math, such as adding days, you must first convert the datetimezone to a standard datetime using DateTimeZone.ToLocal() or DateTimeZone.ToUtc(). For example, to add one day to the current UTC time, you would use DateTime.AddDays(DateTimeZone.ToUtc(DateTimeZone.UtcNow()), 1). This ensures the math operates on a standard time value.

For reporting purposes, it is often useful to convert the UTC value to a specific time zone. You can use DateTimeZone.ConvertZone() to shift the time zone of your current UTC value to your target region. This is particularly useful when presenting data to users in different countries, as it converts the Coordinated Universal Time back into their local daylight saving time.

Troubleshooting

If the DateTimeZone.UtcNow() function is not working as expected, the first step is to check the system time of the computer running Power Query. Ensure the time is set to UTC or is correctly synced with an internet time server. If the system time is off by hours, the function will return a value that is equally off. You can check this by right-clicking the system clock in the Windows taskbar and selecting "Adjust date/time".

If the column is returning an error like "Expression.Error: The function DateTimeZone.UtcNow was not found," it is likely due to a typo in the formula. Double-check that you have typed DateTimeZone.UtcNow() exactly as written, including the capitalization and the correct number of zeros in Utc. In Power Query, function names are case-sensitive, so using datetimezone.utcnow or Datetimezone.UtcNow will result in an error.

If the column is returning a value that is not a datetimezone, such as a plain text, you may have selected the wrong data type for the column. Go to the Transform tab and select "Data Type" from the ribbon. Choose "Datetime Zone" from the dropdown menu to force the column into the correct format.

Frequently Asked Questions

Does DateTimeZone.UtcNow use the server time?

No, the function uses the time of the machine that is currently processing the query. If you are using Power BI Service online, it uses the time of the gateway or the server processing the data refresh, not your local computer time.

Why is my result showing a negative offset?

The offset represents the difference between the time zone of the data and UTC. For example, a value with an offset of -05:00 means the time is five hours behind UTC. If you are seeing negative offsets, it means your data originates in a time zone west of the Prime Meridian.

Can I use this function in a calculated column in DAX?

No, DateTimeZone.UtcNow is a Power Query M function. In DAX, you would use the UTCNOW() function instead to get the current UTC time.

How do I handle daylight saving time with this function?

This function returns the current UTC time, which is standard and does not observe daylight saving time. To convert to a local time that observes DST, you must use a conversion function that accounts for the specific region's rules.

Does the result change when I refresh the data?

Yes, every time you refresh the query, the function will run again and return the current time at that moment. The values in the column will update to reflect the new timestamp.