Power Query DateTime.IsInPreviousSecond Function Guide
In this article
The DateTime.IsInPreviousSecond function in Power Query checks whether a given datetime value falls within the second immediately before the current system time. It returns true or false and is useful for filtering data streams or validating real-time logs against the prior second. No additional setup is required beyond having Power Query enabled in Excel or Power BI.
What You'll Need
- Microsoft Excel 2016 or later, or Microsoft Power BI Desktop
- A table or query containing at least one column of datetime values
- Power Query Editor open and accessible via Data > Get & Transform or the Power Query tab
Step-by-Step
- Open your Excel workbook or Power BI Desktop file containing the data you want to analyze.
- Select the table or query you wish to modify, then click Data > Get & Transform > Edit Query (in Excel) or Transform Data (in Power BI).
- In the Power Query Editor, locate the column containing datetime values. Ensure the column data type is set to Date/Time — if not, right-click the column header, select Data Type > Date/Time.
- Click the Add Column tab in the ribbon above the query editor.
- Select Custom Column from the group. A dialog box will appear.
- In the New column name field, enter a descriptive name like
IsInPreviousSecond. - In the Custom column formula box, type:
DateTime.IsInPreviousSecond([YourColumnName]). ReplaceYourColumnNamewith the exact name of your datetime column (e.g.,EventTime). - Click OK. A new column will appear with boolean values:
trueif the datetime is within the previous second relative to when the query was refreshed,falseotherwise. - To test the function, click Home > Refresh. The result will update based on the current system time at refresh.
- Use the new column to filter rows: click the filter dropdown in the new column header, uncheck
false, and click OK to show only records from the previous second. - Click Close & Load to apply the changes back to your worksheet or report.
Common Mistakes
- Using a text column instead of a Date/Time column — the function will return an error because it cannot parse non-datetime values.
- Typing the function name incorrectly, such as
DateTime.IsInPreviousSecond()with extra spaces or incorrect capitalization — Power Query is case-sensitive for function names. - Assuming the function compares to a fixed reference time like midnight or the start of the day — it always compares to the system clock at the moment of query refresh.
- Expecting the function to work in real-time continuously — it only evaluates when the query is refreshed; it does not update dynamically like a live dashboard.
- Forgetting to refresh the query after modifying the data — changes to the source data won’t trigger a re-evaluation unless manually refreshed.
Tips
- Combine this function with
DateTime.LocalNow()to create a reference point for custom logic, such as comparing against a 10-second window. - Use it alongside
Table.SelectRowsin advanced queries to dynamically filter incoming data streams, like IoT sensor logs or transaction timestamps. - For debugging, add a second custom column with
DateTime.LocalNow()to visually compare the system time with your datetime column values. - Apply this function only to columns with high-precision timestamps (down to the second) — using it on data truncated to minutes will always return false.
- When working with time zones, ensure your datetime values are in UTC or consistently adjusted; mismatches can cause unexpected
falseresults. - Consider using this function in staging queries before loading to production — it’s not meant for reporting but for data validation or cleanup.
Troubleshooting
- Error: “We cannot apply operator IsInPreviousSecond to types Date and DateTime.” → Check that your column is set to Date/Time, not Date only. Right-click the column > Data Type > Date/Time.
- Error: “The name 'DateTime.IsInPreviousSecond' wasn't recognized.” → Confirm you are using Excel 2019, Excel 365, or Power BI Desktop. Older versions of Excel (2016 with outdated updates) may not support this function. Update via File > Account > Update Options > Update Now.
- All values are false, even when timestamps look correct. → Refresh the query and check the system clock on your device. If your system time is off by more than a second, the function will fail. Sync your computer’s clock with an internet time server.
- Function works in Power Query Editor but not after loading. → Ensure the query is set to refresh when the file opens. Go to Data > Queries & Connections > Right-click query > Properties > Check ‘Refresh data when opening file’.
- Query takes too long to refresh. → This function must evaluate each row against the system clock. If your dataset has over 100,000 rows, consider filtering the data first or moving this check to a downstream step.
- Results change unexpectedly between refreshes. → This is normal behavior. The function is designed to be time-sensitive. If you need stable results, store the reference time as a variable using
let refTime = DateTime.LocalNow() in ...and compare against that fixed value.
Frequently Asked Questions
Can I use DateTime.IsInPreviousSecond with time zones other than my local time?
Yes, but only if your datetime values are already converted to UTC or your local time zone. The function uses the system’s local time for comparison. To handle multiple time zones, convert your source timestamps to the same zone as your system before applying the function.Does this function work in Power BI Service (online)?
Yes, but only if the dataset is refreshed in Power BI Service. The function evaluates against the server’s system clock at refresh time, not the user’s local clock.Can I use this function to detect duplicate entries within a second?
Indirectly. You can combine it with grouping or a custom column that flags rows within the same second. However, DateTime.IsInPreviousSecond only compares to the prior second — use DateTime.IsInCurrentSecond or a custom difference calculation for same-second duplicates.Why does it sometimes return true for timestamps that seem to be from minutes ago?
It doesn’t. If the function returns true, the timestamp must be within one second before the exact moment the query refreshed. If you're seeing this, your system clock is likely incorrect or the data source has been cached with stale values.Is this function available in Power Query for Excel 2016?
Only if you have the latest updates installed. Microsoft added DateTime.IsInPreviousSecond in late 2018. Check your version under File > Account > About Excel — if you're on version 16.0.10730 or later, it should be supported.Can I use DateTime.IsInPreviousSecond in Power Query Online (Power Automate)?
No. Power Automate uses a different version of Power Query that does not include this function. Use conditional logic with DateTime functions likeDateTime.AddSeconds and DateTime.Compare instead.