Power Query Time.Second Function: A Complete Guide
In this article
The Time.Second function in Power Query is used to extract the seconds component from a time or datetime value, returning an integer between 0 and 59. To use it, you simply wrap your time column or value within the function, such as Time.Second([YourTimeColumn]), which isolates the seconds for use in calculations, filtering, or reporting.
What You'll Need
- Microsoft Excel (with Power Query enabled) or Power BI Desktop.
- A dataset containing a column formatted as Time, DateTime, or DateTimeZone.
- Access to the Power Query Editor (Get Data > Launch Power Query Editor).
Step-by-Step
- Load Your Data: Open your application and import your data source. Go to the Data tab in Excel and select Get Data, or use the Home tab in Power BI to select Get Data. Once your data is loaded into the Power Query Editor, ensure you can see the column containing the time information.
- Verify Data Types: Look at the icon to the left of your column header. It must be a clock icon (Time) or a calendar-and-clock icon (DateTime). If it shows an "ABC" or "123" icon, right-click the column header, select Change Type, and choose Time or DateTime. The
Time.Secondfunction will return an error if it is applied to a text string. - Create a Custom Column: Navigate to the Add Column tab on the top ribbon. Click on Custom Column. This opens a dialog box where you can define your new calculation.
- Enter the Formula: In the Custom Column Formula box, type the following:
= Time.Second([YourColumnName]). Replace[YourColumnName]by double-clicking the actual column name from the list on the right side of the dialog box to ensure the syntax is perfect. - Confirm and Apply: Click OK. Power Query will now generate a new column containing only the seconds (0-59) from the original timestamp.
- Set the New Data Type: By default, the new column may be set to the "Any" type. Click the data type icon in the column header and select Whole Number to ensure the column is optimized for numerical analysis.
- Close and Apply: Once you have verified the results, go to the Home tab and click Close & Apply to load the transformed data back into your Excel sheet or Power BI model.
Common Mistakes
One of the most frequent errors is attempting to use Time.Second on a column that is formatted as Text. Even if the text looks like "12:30:45", Power Query treats it as a string of characters, not a temporal value. This results in an Expression.Error stating that the value cannot be converted to Time. Always check the data type icon before applying time functions.
Another common mistake is confusing Time.Second with Duration.Seconds. These are fundamentally different. Time.Second extracts the second digit from a specific point in time (e.g., the 45th second of a minute). Duration.Seconds is used for time spans (durations) and calculates the total seconds remaining after whole days and hours are removed. If you are trying to find the total elapsed time between two events, Time.Second is the wrong tool.
Users also occasionally forget that Power Query is case-sensitive. Writing time.second or Time.second with a lowercase 's' will result in a formula error. You must use the exact casing: Time.Second.
Tips
To perform a quick extraction without writing a manual formula, you can use the built-in GUI tools. Select your time column, go to the Add Column tab, click the Time button, select Time, and then choose Second. This automatically generates the Time.Second code in the background, saving you from typing the formula manually.
If you need to filter your data based on seconds—for example, to find all logs that occurred at the start of a minute—you can use the Filter dropdown on your newly created seconds column. Selecting "0" will isolate every record that occurred exactly at the 00-second mark.
For advanced users, you can combine Time.Second with other functions like Time.Minute and Time.Hour to create a unique "Time Key" for indexing. For example, a custom column formula like [Hour] & "-" & [Minute] & "-" & [Second] can create a readable timestamp string for auditing purposes.
Troubleshooting
If your column is returning "Error" for every row, the first step is to check for null values. If a row is empty (null), Time.Second cannot extract a value. To fix this, right-click the source column and select Replace Errors or use a conditional column to handle nulls before applying the function. Alternatively, wrap your formula in an if-statement: if [YourColumn] = null then null else Time.Second([YourColumn]).
If you receive a "Type Mismatch" error, double-check that you haven't accidentally applied the function to a Date column (without time). A pure Date column does not contain second information. Ensure the column is specifically set to DateTime or Time.
If the values seem incorrect (e.g., you expect 45 but see 15), check if your data source is in a different time zone. If you are using DateTimeZone types, Power Query may be converting the time to your local system time before extracting the second. You can use DateTimeZone.ToLocal or DateTimeZone.RemoveZone to standardize the time before extracting the seconds.
Frequently Asked Questions
Can Time.Second return a value higher than 59?
No. The Time.Second function only returns the seconds component of a time value, which is always an integer between 0 and 59 inclusive.
Does this function work with Date columns?
No, it requires a Time or DateTime data type. If applied to a column that only contains a Date, it will result in an error because there is no time component to analyze.
How is this different from Duration.TotalSeconds?
Time.Second extracts the "seconds" part of a clock time. Duration.TotalSeconds calculates the entire length of a time interval converted into seconds (e.g., 1 minute and 10 seconds becomes 70 seconds).
Can I use this in a calculated column in Power BI DAX?
No. Time.Second is a Power Query (M language) function. If you are working in the DAX layer of Power BI, you should use the SECOND() function instead.
What happens if my time column has milliseconds?
The Time.Second function ignores milliseconds. It only returns the whole number of seconds. To extract milliseconds, you would need to use a different approach or a custom formula involving Time.ToRecord.
Is the function case-sensitive?
Yes, Power Query is strictly case-sensitive. You must capitalize both the 'T' in Time and the 'S' in Second for the formula to be recognized.