Smart Systems for Home

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

Power Query DateTime.ToRecord Function: How to Use It

· 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 DateTime.ToRecord function in Power Query converts a datetime value into a record with separate fields for year, month, day, hour, minute, second, and millisecond, making it easier to extract and manipulate individual components of a date-time value for analysis or transformation. No additional tools or installations are required—this function is built into Power Query in Excel, Power BI, and other Microsoft data tools that support M language.

What You'll Need

  • Microsoft Excel 2016 or later, or Power BI Desktop (latest version recommended)
  • A table or query containing at least one column with valid datetime values
  • Access to the Power Query Editor (via Data > Get & Transform in Excel, or Transform Data in Power BI)

Step-by-Step

  1. Open your Excel workbook or Power BI Desktop file and load the data containing your datetime column into Power Query Editor by selecting the data range or table, then choosing Data > Get & Transform > From Table/Range (Excel) or Transform Data (Power BI).
  2. In the Power Query Editor, locate the column with datetime values. Ensure the column’s data type is set to Date/Time—right-click the column header, select Data Type > Date/Time, and confirm the values display correctly (e.g., 2023-10-05 14:30:00).
  3. Click on the Add Column tab in the ribbon at the top of the Power Query Editor.
  4. Click the DateTime dropdown menu and select To Record. This will add a new column titled DateTime.ToRecord with a record icon next to each row.
  5. Click the expand icon (two arrows) on the new column header to open the expand dialog. Check all fields: Year, Month, Day, Hour, Minute, Second, and Millisecond. Click OK to create individual columns for each component.
  6. Optionally, rename the new columns for clarity—right-click each column header and select Rename. For example, rename Year to TransactionYear if the data represents sales timestamps.
  7. Remove the original datetime column if it’s no longer needed: right-click its header and select Remove.
  8. Click Close & Load (Excel) or Close & Apply (Power BI) to return the transformed data to your worksheet or report.

Common Mistakes

  • Applying DateTime.ToRecord to a column with text or null values—this will return an error. Always validate that the column contains only valid datetime values before using the function.
  • Forgetting to expand the record—after applying the function, the result is a single record column. Many users assume the values are automatically broken out, but you must manually expand using the expand icon to see the individual fields.
  • Using the function on a column with time-only or date-only values—while Power Query may allow it, the missing components (e.g., year in a time-only field) will return nulls, leading to incomplete data.
  • Renaming columns before expanding the record—this can cause confusion when trying to locate the correct fields in the expand dialog. Expand first, then rename.
  • Assuming the function works on non-datetime columns—even if the data looks like a date (e.g., "2023-10-05"), if the column type is set to Text, the function will fail. Convert to Date/Time first.

Tips

  • Use DateTime.ToRecord when building date-based filters or aggregations—extracting year and month allows you to group sales by fiscal period without relying on date hierarchies.
  • Combine with Table.AddColumn in advanced M code if you need to apply the function programmatically: Table.AddColumn(Source, "DateTimeRecord", each DateTime.ToRecord([Timestamp])).
  • Use the millisecond field sparingly—it’s often zero in imported data and can clutter your dataset unless you’re working with high-frequency transaction logs.
  • After expanding, apply data type formatting (e.g., Whole Number) to the extracted fields for better performance and clarity in visualizations.
  • If you frequently extract datetime components, consider creating a custom function: define a function that wraps DateTime.ToRecord and expands the fields automatically, then reuse it across queries.
  • Test your transformation on a small sample first—use Keep Rows > Keep First Rows to reduce dataset size during development, then apply the full transformation once validated.

Troubleshooting

  • Error: "We cannot apply operator ToRecord to type Text" — This means the column isn’t a datetime. Fix: Right-click the column > Data Type > Date/Time. If values still don’t convert, check for invalid entries like "N/A" or blank cells and clean them first.
  • Expanded columns show "null" — This usually means the original datetime value was invalid or incomplete (e.g., only a date with no time). Use Table.SelectRows to filter out null or invalid dates before applying the function.
  • Record column doesn’t appear after selecting "To Record" — Refresh the Power Query Editor view or restart the application. Rarely, UI glitches prevent the new column from rendering immediately.
  • Missing "To Record" option in DateTime dropdown — Ensure you’re using a supported version (Excel 2016+, Power BI Desktop). Older versions of Power Query (in Excel 2010 or 2013) don’t include this function.
  • Performance slowdowns after expanding — If your dataset has over 100,000 rows, expanding all seven fields can increase memory use. Only expand the fields you need—uncheck Year, Month, etc., in the expand dialog if unused.

Frequently Asked Questions

Can I use DateTime.ToRecord with time zones?

No, DateTime.ToRecord does not handle time zone offsets. The function assumes the datetime is in the local system time. For time zone-aware data, convert to UTC first using DateTimeZone.ToLocal or DateTimeZone.SwitchZone.

What happens if the datetime value is null?

If the source value is null, DateTime.ToRecord returns a record with all fields set to null. Use Table.SelectRows to filter out nulls before applying the function to avoid downstream errors.

Can I reverse this process? Turn a record back into a datetime?

Yes. Use DateTime.FromRecord(record) to reconstruct a datetime from a record with Year, Month, Day, Hour, Minute, Second, and Millisecond fields. This is useful for rebuilding timestamps after transformation.

Does DateTime.ToRecord work with dates from Excel's serial number format?

Yes, as long as the column is recognized as Date/Time. Excel serial numbers (like 44835.60417 for 2023-10-05 14:30) are automatically converted to proper datetime values in Power Query and work correctly with this function.

Why are my seconds always zero?

Most data sources (like CSV, Excel, or SQL) don’t store milliseconds or seconds unless explicitly included. If your source data lacks second precision, the function will correctly return 0 for Second and Millisecond fields.

Can I use DateTime.ToRecord in Power BI DirectQuery mode?

No. Functions that require local computation, including DateTime.ToRecord, are not supported in DirectQuery mode. Use them only in Import mode, or replace the logic with DAX calculated columns in the data model.