Smart Systems for Home

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

Power Query Duration.TotalDays: 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.TotalDays converts a duration value into a decimal number of days. In Power Query, a duration is a length of time (days, hours, minutes, seconds), and this function returns that whole length expressed in days, including fractional parts. For example, a duration of 1 day 12 hours returns 1.5. You use it when you need to calculate elapsed days between two timestamps, average handling times, or any measure where a duration must become a number you can aggregate or compare.

What You'll Need

  • Power Query available in Excel, Power BI Desktop, or another host that supports it. The steps below use Power BI Desktop and Excel; labels can differ slightly by version, device, or region.
  • A table with at least one duration column, or two date/time columns you can subtract to create one.
  • Basic familiarity with the Power Query Editor: the ribbon, the Queries pane, and 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 editor opens with your query loaded.
  2. Create or locate a duration column. If you already have a column of type Duration, select its header and confirm the data type shown in the column header is Duration. If you have two date/time columns, select the first, hold Ctrl, select the second, then go to Add Column > Subtract > Subtract Days (or use the Date > Subtract menu depending on your version). This creates a duration column. If you only need the difference in whole days, Duration.Days is simpler, but TotalDays keeps the fractional part.
  3. Add a custom column. Go to Add Column > Custom Column. Give it a name such as Total Days.
  4. Enter the formula. In the formula box, type Duration.TotalDays([YourDurationColumn]), replacing YourDurationColumn with the exact name of your duration column. Column names are case-sensitive in M and must match the header exactly. If the column name contains spaces, wrap it in square brackets as shown; the brackets are required.
  5. Check the result. Select OK. Power Query adds the new column and shows the computed decimal values. A duration of 2 days 6 hours appears as 2.25.
  6. Set the data type. Select the new column header and set the type to Decimal Number (or Whole Number if you want to round later, though that truncates the fraction). Leaving it as Any can cause issues when you load the data.
  7. Handle nulls and errors. If the source duration column contains nulls, the result will be null. If it contains text or errors, the function returns an error. Use the column's filter menu to remove errors or replace nulls with 0 before applying the function, or wrap the formula in try ... otherwise null if you prefer to keep rows.
  8. Apply and close. Go to Home > Close & Load (Excel) or Home > Close & Apply (Power BI). The new numeric column is available in your data model or worksheet.

Common Mistakes

  • Using Duration.Days instead of Duration.TotalDays. Duration.Days returns only the whole-day component and drops hours, minutes, and seconds. A 1.9-day duration returns 1, not 1.9. Use TotalDays when you need the full elapsed time.
  • Passing a date or datetime instead of a duration. Duration.TotalDays expects a duration value. If you feed it a date, you get an error. Subtract two dates first to create a duration.
  • Misspelling the column name. M is case-sensitive. [duration] and [Duration] are different. Copy the name from the header or the formula bar.
  • Forgetting to set the data type. A custom column may come in as Any. Set it to Decimal Number so it aggregates correctly in pivot tables and visuals.
  • Expecting a formatted duration. TotalDays returns a number, not text like "1.5 days." Format it in the report layer if you need a label.

Tips

  • Use Duration.TotalHours, Duration.TotalMinutes, or Duration.TotalSeconds when days are not the right unit. They follow the same pattern and return decimals.
  • To round to a set number of decimals, wrap the result: Number.Round(Duration.TotalDays([Duration]), 2).
  • If you need whole days only and want to discard the time portion, use Duration.Days. If you need to round up partial days, use Number.RoundUp on the TotalDays result.
  • Name your custom column clearly, such as Elapsed Days, so report authors know it is a number and not a duration.
  • Test with a known value: a duration of 0.00:00:00 returns 0, and 1.12:00:00 returns 1.5. This confirms the function is reading the column correctly.

Troubleshooting

If the custom column shows errors, select the error cell to read the message. "We cannot convert the value ... to type Duration" means the source column is not a duration. Change the column type to Duration first, or create a duration by subtracting two datetime columns. If the column is empty, check that the source column name matches exactly and that you used square brackets. If the result is null for every row, the duration column likely contains nulls; filter or replace them. If the number looks wrong, confirm you did not use Duration.Days. If the query is slow on large tables, add the custom column after filtering unnecessary rows, since the function runs row by row. If the new column does not appear after Close & Load, check the Applied Steps pane to ensure the step was not deleted, and refresh the query.

Frequently Asked Questions

What is the difference between Duration.Days and Duration.TotalDays?

Duration.Days returns the whole-day portion of a duration, discarding hours and minutes. Duration.TotalDays returns the entire duration expressed in days as a decimal, so 36 hours becomes 1.5.

Can I use Duration.TotalDays on a date column?

No. It requires a duration value. Subtract one date/time column from another to create a duration, then apply the function.

Why does my result show as a whole number?

The column's data type is likely set to Whole Number. Change it to Decimal Number to keep the fractional part.

How do I handle nulls in the duration column?

Filter out nulls, replace them with a zero duration using Replace Values, or wrap the formula in try ... otherwise null to keep the rows.

Does Duration.TotalDays work in Excel Power Query?

Yes. It is part of the M language used by Power Query in Excel, Power BI, and other hosts that support Power Query. The menus may look slightly different, but the formula is the same.

Can I format the result as days and hours?

TotalDays returns a number, so formatting as a duration text requires a separate expression. For display, use the number in visuals or build a text column with Number.Round and concatenation.