Power Query DateTime.ToRecord Function: How to Use It
In this article
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
- 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).
- 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).
- Click on the Add Column tab in the ribbon at the top of the Power Query Editor.
- 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.
- 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.
- 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.
- Remove the original datetime column if it’s no longer needed: right-click its header and select Remove.
- 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.