Smart Systems for Home

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

Power Query DateTimeZone.ToRecord Function Guide

· 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 DateTimeZone.ToRecord function in Power Query converts a datetimezone value into a record containing its individual parts: year, month, day, hour, minute, second, and timezone offset. This is useful when you need to extract specific components for analysis or reporting. You can use it directly in the Power Query editor's custom column or in M code.

What You'll Need

  • Power Query Editor (available in Power BI Desktop, Excel, and Power Query Online)
  • A data source containing a datetimezone column or a way to create one
  • Basic familiarity with adding custom columns in Power Query

Step-by-Step

  1. Open Power Query Editor: In Power BI Desktop, go to Home tab > Transform Data. In Excel, go to Data tab > Get Data > Launch Power Query Editor. In Power Query Online, open your dataflow or dataset.
  2. Select the table that contains the datetimezone column you want to convert. If you don't have one, you can create a sample by using Enter Data and typing a value like 2025-01-15T10:30:00+05:00.
  3. Add a custom column: Go to the Add Column tab and click Custom Column. This opens a dialog where you can enter M code.
  4. Enter the function: In the custom column formula box, type DateTimeZone.ToRecord([YourDateTimeZoneColumn]) – replace YourDateTimeZoneColumn with the actual name of your column. For example, if your column is named DateTime, enter DateTimeZone.ToRecord([DateTime]).
  5. Name the new column: In the New column name field, type something like DateTimeParts and click OK. The new column will appear with a record value for each row.
  6. Expand the record: Click the expand icon (the double arrow) in the header of the new column. This opens a dropdown where you can select which fields to expand. Check the boxes for Year, Month, Day, Hour, Minute, Second, and TimezoneOffset (the field names may be Year, Month, Day, Hour, Minute, Second, TimezoneOffset). Click OK.
  7. Verify the output: The expanded columns will appear as separate columns in your table. Each will contain the corresponding numeric part of the datetimezone value. For example, Year will show 2025, Month will show 1, and so on.
  8. Use the parts as needed: You can now use these columns in further transformations, filters, or visualizations.

Common Mistakes

  • Using the wrong column type: The function only works with datetimezone values. If your column is text or a regular datetime, you'll get an error. Convert the column first using DateTimeZone.FromText() or DateTimeZone.From().
  • Forgetting to expand the record: If you don't expand the record, you'll see a column with records, not individual parts. Always click the expand icon after creating the custom column.
  • Incorrect field names: The record fields are case-sensitive. Use Year, Month, Day, Hour, Minute, Second, and TimezoneOffset exactly as shown.
  • Assuming timezone offset is a number: The TimezoneOffset field is a duration value (e.g., 05:00:00), not a plain number. If you need hours, convert it using Duration.TotalHours.

Tips

  • Use DateTimeZone.ToRecord when you need to group by year or month from a timezone-aware timestamp.
  • If you only need one part (e.g., year), you can access it directly without expanding: DateTimeZone.ToRecord([DateTime])[Year].
  • For performance, consider expanding only the fields you need, as expanding all fields adds columns and may slow down your query.
  • Combine with DateTimeZone.ToLocal() if you want to convert to a specific timezone before extracting parts.
  • Remember that the function is case-sensitive: DateTimeZone with a capital D, T, and Z.

Troubleshooting

Error: “The name 'DateTimeZone.ToRecord' wasn't recognized.” – This usually means you're using an older version of Power Query or the function name is misspelled. Ensure you're using the exact function name and that your Power Query version is up to date.

Error: “We cannot convert the value to type DateTimeZone.” – Your column is not in datetimezone format. Convert it first using DateTimeZone.FromText() if it's text, or DateTimeZone.From() if it's a datetime. For example, DateTimeZone.From([DateTimeColumn]).

Expanded columns show “null” values – This can happen if the original datetimezone value is null. Check your source data for nulls and handle them with if [Column] = null then null else ....

TimezoneOffset shows as duration, not a number – This is expected. To get hours, use Duration.TotalHours([RecordField]) after expanding, or in the custom column formula: Duration.TotalHours(DateTimeZone.ToRecord([DateTime])[TimezoneOffset]).

If the function still doesn't work, try restarting Power Query Editor or checking for updates to your application.

Frequently Asked Questions

Can I use DateTimeZone.ToRecord with a regular datetime column?

No, the function requires a datetimezone value. You can convert a datetime to datetimezone using DateTimeZone.From() or DateTimeZone.FromText().

What are the exact field names returned?

The fields are Year, Month, Day, Hour, Minute, Second, and TimezoneOffset. They are all numbers except TimezoneOffset, which is a duration.

Can I get the timezone offset as a number of hours?

Yes. Use Duration.TotalHours([TimezoneOffset]) to convert the duration to a decimal number of hours.

Does this function work in Excel Power Query?

Yes, it works in Excel's Power Query (Get & Transform) as long as you have a recent version of Excel.

Is there a way to extract just the year without expanding?

Yes, use DateTimeZone.ToRecord([Column])[Year] in a custom column to get the year directly.

What if my datetimezone value is null?

The function will return null. You can handle this by wrapping the function in a conditional, for example: if [Column] = null then null else DateTimeZone.ToRecord([Column])[Year].