Smart Systems for Home

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

Power Query Duration.TotalSeconds: How to Use It

· 6 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.TotalSeconds converts a duration value into a single decimal number representing the total number of seconds it contains. You use it in Power Query (the Get & Transform Data engine in Excel and Power BI) when you need a numeric field for math, sorting, or charting instead of a duration record. The function takes one argument, a duration, and returns a number. For example, Duration.TotalSeconds(#duration(0,0,1,30)) returns 90 because one minute and thirty seconds equals 90 seconds. It is not an M function you type into a worksheet cell; it belongs in the Power Query Editor's formula bar or Advanced Editor.

What You'll Need

  • Excel 2016 or later, Excel for Microsoft 365, or Power BI Desktop. The Power Query Editor is built into these versions. Older Excel releases require the separate Power Query add-in, which Microsoft no longer supports for new downloads.
  • A table or query already loaded into Power Query, with at least one column that Power Query recognises as a Duration type. If your data is text like "01:30:00" or a number of minutes, you will convert it first.
  • Basic familiarity with the Power Query Editor ribbon and the formula bar. You do not need to write full M code from scratch, but you will type a short custom column formula.

Step-by-Step

  1. Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open the Power Query Editor with your existing query. Menu labels can differ slightly by version and region, but the Data and Home tabs are consistent.
  2. Confirm the column type is Duration. Click the small type icon to the left of the column header. If it shows a clock symbol, the column is already Duration. If it shows ABC or 123, click the icon and choose Duration. If your source stores durations as text, Power Query may fail to convert values like "1:30:00" directly; in that case, split the text into hours, minutes, and seconds columns, convert each to numbers, and build a duration with #duration in a later step.
  3. Add a custom column. Go to Add Column > Custom Column. Give it a name such as "TotalSeconds". In the formula box, type Duration.TotalSeconds([YourDurationColumn]), replacing YourDurationColumn with the exact name of your duration column. Column names with spaces must be wrapped in square brackets and, if they contain special characters, in double quotes inside the brackets, for example [#"Elapsed Time"].
  4. Check the result type. After you click OK, Power Query adds the new column. Click its type icon and confirm it is set to Decimal Number or Whole Number. The function returns a number, but Power Query sometimes leaves the type as Any; setting it explicitly keeps later steps predictable.
  5. Handle nulls before you calculate. If any rows have a null duration, Duration.TotalSeconds returns null for those rows. That is usually fine, but if you plan to sum or average the column, either filter out nulls or wrap the expression in try ... otherwise 0, for example try Duration.TotalSeconds([Elapsed]) otherwise 0.
  6. Round if you need whole seconds. Wrap the result in Number.Round if you want an integer: Number.Round(Duration.TotalSeconds([Elapsed])). Use Number.RoundDown or Number.RoundUp when you need consistent direction rather than nearest.
  7. Apply and load. Click Close & Load (Excel) or Close & Apply (Power BI). The new numeric column is now available for measures, pivots, and charts.

Common Mistakes

  • Passing text instead of a duration. Duration.TotalSeconds expects a duration value. If the column is typed as Text, the function returns an error. Change the type first, or convert the text with Duration.FromText, which accepts ISO 8601 duration strings such as "PT1H30M" but not arbitrary clock formats.
  • Confusing Duration.TotalSeconds with Duration.Seconds. Duration.Seconds returns only the seconds component of the duration, from 0 to 59. Duration.TotalSeconds returns the whole span in seconds. A duration of 1 hour, 2 minutes, and 3 seconds gives 3 from Duration.Seconds but 3723 from Duration.TotalSeconds.
  • Forgetting the column reference brackets. In a custom column, every column reference must be in square brackets. Typing the column name without brackets produces a syntax error.
  • Assuming time-of-day works the same way. A Time value is not a Duration. If your column is typed as Time, convert it first, for example by subtracting a zero time or by building a duration from its components.
  • Ignoring locale differences in source text. If you convert text durations, the separator and order can vary by locale. Check a sample of rows after conversion rather than trusting the preview.

Tips

  • Use Duration.TotalMinutes or Duration.TotalHours when the downstream chart or measure reads better in those units. The pattern is identical.
  • To convert seconds back into a readable duration, use #duration(0,0,0,[TotalSeconds]) and set the column type to Duration.
  • Keep the original duration column and add the seconds column alongside it. You can hide the original later without losing it.
  • If you are building the duration yourself, #duration takes four arguments: days, hours, minutes, and seconds. Negative values are allowed and produce a negative duration.
  • In the Advanced Editor you can see the generated step, which is useful for copying the pattern into other queries.

Troubleshooting

If the custom column shows an error in every row, the most likely cause is the column type. Select the source column, set it to Duration, and refresh the preview. If only some rows error, those rows probably contain nulls or text that could not be converted; filter them out or use try ... otherwise to handle them.

If the formula bar rejects your expression, check the brackets around the column name and confirm the function name is spelled exactly Duration.TotalSeconds with a capital D and T. M is case-sensitive.

If the numbers look wrong, compare a few rows by hand. A duration of 2 days, 3 hours, 4 minutes, and 5 seconds should return 183845 seconds. If you see a much smaller number, you are probably using Duration.Seconds instead of Duration.TotalSeconds.

If the column disappears after a refresh, a step may have renamed or removed the source column. Open the Applied Steps pane and click through each step to find where the column changes.

If the query is slow on a large table, add the custom column after filtering and removing unneeded columns, not before.

Frequently Asked Questions

Does Duration.TotalSeconds work in Excel and Power BI the same way?

Yes. Both use the same Power Query engine, so the function behaves identically. The only differences are the ribbon labels for loading the result.

Can I use Duration.TotalSeconds in a DAX measure?

No. It is an M function used in Power Query. In DAX, durations are typically stored as numbers or as a duration data type, and you multiply or convert them depending on how the column was loaded.

What does Duration.TotalSeconds return for a negative duration?

It returns a negative number. For example, a duration of minus 30 seconds returns -30.

Why does my result show decimals?

Durations can include fractional seconds. If your source has milliseconds, the total will include them. Wrap the result in Number.Round if you need whole seconds.

Can I convert the seconds back into hours and minutes?

Yes. Divide by 3600 for hours, or use #duration to rebuild a duration and then format it as text with Duration.ToText.

Does the function handle nulls automatically?

It returns null when the input is null. Use try ... otherwise or filter the rows if you need a numeric zero instead.