Smart Systems for Home

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

Power Query Duration.ToRecord: 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

Duration.ToRecord converts a duration value into a record with named fields for days, hours, minutes, seconds, and fractional seconds. You use it to break a duration apart so you can extract, display, or calculate with individual components. No add-ins or special setup are required, but you need a query with a duration column and a basic understanding of Power Query's transformation steps.

What You'll Need

  • Power Query available in Excel, Power BI Desktop, or another Microsoft tool that supports it.
  • A query with a column that contains duration values. If your column holds text like "2.05:30:00" or numbers, convert it to duration first.
  • Access to the Power Query Editor to add a custom column or edit the formula bar.

Step-by-Step

  1. Open the Power Query Editor. In Excel, select a cell in your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data. The exact menu labels can differ slightly by version, device, or region.
  2. Confirm your column is a duration type. Select the column and check the icon in the column header. A duration column shows a clock-like icon. If it shows ABC (text) or 123 (number), you need to convert it. For text values, right-click the column header, choose Change Type > Using Locale, and pick a locale that matches your text format. For numbers that represent days, you can use Duration.From in a custom column.
  3. Add a custom column. Go to Add Column > Custom Column. In the dialog, give the column a name such as "DurationRecord".
  4. Enter the formula. In the formula box, type Duration.ToRecord([YourDurationColumn]), replacing [YourDurationColumn] with the actual name of your duration column. For example, if the column is named "Elapsed", use Duration.ToRecord([Elapsed]). Click OK.
  5. Expand the record. The new column contains record values. Click the expand icon (two arrows) in the column header and select the fields you want to keep: Days, Hours, Minutes, Seconds, and possibly a fractional seconds field. You can also expand all fields at once. This creates separate columns for each selected field.
  6. Use the fields. Once expanded, you can rename the columns, change their data types, or use them in calculations. For example, to get total hours as a number, you might combine Days * 24 + Hours. To display a formatted string, use Text.From on each field and concatenate.
  7. Apply and close. When finished, go to Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to load the results.

Common Mistakes

  • Using a text column without converting. Duration.ToRecord expects a duration value. If the column is text, you will get an error. Convert it first with Change Type > Using Locale or Duration.From.
  • Forgetting to expand the record. The custom column will show "Record" in each cell. You must expand it to see the individual fields.
  • Assuming field names are always the same. The record contains fields like Days, Hours, Minutes, Seconds, and a fractional seconds field. The exact field names are consistent in Power Query, but the fractional field may be named differently in some versions. Check the expansion dialog to see the available fields.
  • Ignoring negative durations. Duration.ToRecord handles negative durations, but the sign applies to the whole duration. When you expand, the individual fields may not carry the sign in the way you expect. Test with a negative value and adjust your calculations if needed.
  • Expecting a formatted text output. Duration.ToRecord returns a record, not a formatted string. If you need a display format, build it with Text.From and concatenation after expanding.

Tips

  • You can use Duration.ToRecord directly in a calculated column or measure in Power BI, but it is most useful in Power Query for shaping data.
  • To avoid expanding all fields, expand only the ones you need to keep the query lean.
  • If you need total seconds as a number, use Duration.TotalSeconds instead of Duration.ToRecord. Duration.ToRecord is for breaking the duration into parts.
  • Combine Duration.ToRecord with other Duration functions like Duration.Days, Duration.Hours, etc., to extract specific parts without creating a record.
  • When working with durations that exceed 24 hours, the Days field will be greater than 0. Use that to your advantage when calculating total hours.

Troubleshooting

If you get an error saying the value is not a duration, check the column type. Select the column and look at the icon. If it is not a clock, change the type. For text values, use Change Type > Using Locale and select a format that matches your data. For example, if your text is "1.02:30:00" (1 day, 2 hours, 30 minutes), choose a locale that uses that pattern, or use Duration.From with a custom format string.

If the custom column shows "Record" but expanding does not show the fields you expect, click the expand icon and review the list. The fields are typically Days, Hours, Minutes, Seconds, and a fractional seconds field. If you see only a few, your duration may have zero values for some parts, but the fields should still appear.

If the expanded columns have unexpected data types, set them manually. For example, Days might come through as a number, but Hours could be a number as well. Use the Transform tab to set the data type to Whole Number for days, hours, minutes, and seconds, and Decimal Number for fractional seconds.

If you are working with durations that come from subtracting two datetime values, ensure both are datetime type. The result of subtraction is a duration, so Duration.ToRecord will work directly on that column.

Frequently Asked Questions

What does Duration.ToRecord return?

It returns a record with fields for days, hours, minutes, seconds, and fractional seconds. The exact field names are Days, Hours, Minutes, Seconds, and a fractional field.

Can I use Duration.ToRecord on a column that contains text?

No, it requires a duration value. Convert the text to duration first using Change Type > Using Locale or the Duration.From function.

How do I get total hours from a duration?

Use Duration.TotalHours instead of Duration.ToRecord. Duration.ToRecord breaks the duration into parts; it does not give a single total.

Why do I see "Record" instead of values?

Because the custom column contains record values. Click the expand icon in the column header to extract the fields into separate columns.

Does Duration.ToRecord work with negative durations?

Yes, it handles negative durations, but the sign applies to the whole duration. When expanded, the individual fields may not show the sign as you expect, so test and adjust calculations if needed.

Can I use Duration.ToRecord in Power BI measures?

Yes, but it is more common in Power Query for data shaping. In DAX, you would use other functions to extract parts of a duration.