Power Query DateTime.IsInPreviousNSeconds Function Guide
In this article
The DateTime.IsInPreviousNSeconds function in Power Query checks whether a given datetime value falls within the last N seconds from the current system time. It returns true or false and is useful for filtering recent data, such as live feeds, sensor logs, or real-time transactions. This function requires no external setup but depends on the system clock of the machine running Power Query.
What You'll Need
- Microsoft Excel with Power Query enabled (Excel 2016 or later)
- Power BI Desktop (any recent version)
- A table or query containing a datetime column
- A valid system clock on your device (time must be synchronized)
Step-by-Step
- Open your workbook in Excel or your report in Power BI Desktop.
- Load the data containing your datetime column into Power Query by selecting the table and choosing Data > From Table/Range (in Excel) or Get Data > From Table/Query (in Power BI).
- In the Power Query Editor, identify the column that contains your datetime values. Ensure it is formatted as a DateTime type — if not, right-click the column header, select Change Type > Date/Time.
- Click on the Add Column tab in the ribbon, then select Custom Column.
- In the Custom Column dialog box, enter a name for your new column, such as "IsRecent".
- In the Custom column formula box, type:
DateTime.IsInPreviousNSeconds([YourDateTimeColumn], 60). Replace[YourDateTimeColumn]with the actual name of your datetime column, including the square brackets. For example, if your column is named "Timestamp", the formula becomesDateTime.IsInPreviousNSeconds([Timestamp], 60). - Click OK. A new column will appear containing TRUE or FALSE values based on whether each datetime is within the last 60 seconds of the current system time.
- To filter only recent entries, click the filter dropdown in the new column header, uncheck FALSE, and leave only TRUE selected.
- Click Close & Load to return the filtered data to your worksheet or report.
- Verify the result: if your system clock is correct and your datetime values are recent, you should see only rows that occurred within the specified N-second window.
Common Mistakes
- Using a text column instead of a DateTime column — the function fails silently or returns errors if the input is not a proper datetime value.
- Typing the function name incorrectly, such as missing the capitalization (e.g., "datetime.isinpreviousnseconds") — Power Query is case-sensitive for function names.
- Forgetting the square brackets around the column name — without them, Power Query treats the name as a literal string, not a reference.
- Using a value for N that is too small (e.g., 1 or 2 seconds) when data refreshes slowly — this can result in zero matches even if data is fresh.
- Assuming the function uses UTC or server time — it always uses the local system time of the machine running Power Query, which may differ from data source time zones.
Tips
- Use this function with a refresh interval in mind — if your data refreshes every 5 minutes, setting N to 300 seconds (5 minutes) ensures you capture all recent entries.
- Combine it with DateTime.LocalNow() if you need to compare against a dynamic reference point, though DateTime.IsInPreviousNSeconds already uses the current system time internally.
- For dashboards that update frequently, consider using this filter in a separate query stage before loading to reduce data volume.
- Always validate the system clock on your computer — if it’s off by even a few minutes, the function will exclude valid recent data or include outdated entries.
- Use conditional formatting in Excel on the resulting TRUE/FALSE column to visually highlight recent records.
- When working in Power BI, you can use this column in a visual filter to dynamically show only the last N seconds of data without changing the underlying query.
Troubleshooting
- If you see an error like "Expression.Error: The name 'DateTime.IsInPreviousNSeconds' wasn't recognized", ensure you’re using Excel 2016 or later, or Power BI Desktop from 2019 or newer — older versions do not support this function.
- If all results are FALSE, check the datetime values in your source. Are they in the past? Are they in a different time zone? Convert them using DateTimeZone.SwitchZone if needed.
- If results are inconsistent across multiple users, confirm that each user’s system clock is synchronized with an internet time server (Windows: Settings > Time & Language > Date & Time > Sync now).
- If the function returns TRUE for data that is clearly older than N seconds, check for time zone mismatches — your source data might be in UTC while your system is set to EST, creating a 5-hour offset.
- If the column doesn’t appear after adding the custom column, recheck your formula syntax. Ensure you typed the exact function name and referenced the column correctly.
- Restart Power Query after making changes if the results seem cached — sometimes the query engine doesn’t re-evaluate DateTime.IsInPreviousNSeconds on every refresh unless forced.
Frequently Asked Questions
Can I use DateTime.IsInPreviousNSeconds with dates only, without time?
No. The function requires a full DateTime value. If your column contains only dates, add a time component (e.g., 00:00:00) using DateTime.From or combine it with Time.From to make it valid.Does this function work in Power Query Online or Power Automate?
No. This function is only available in the desktop versions of Excel and Power BI. Power Query Online and Power Automate do not support DateTime.IsInPreviousNSeconds.What happens if my computer is off when the data refreshes?
If your machine is powered off, the query won’t run, so no refresh occurs. The function depends on the system clock at the moment of execution. Scheduled refreshes in Power BI Service use Microsoft’s cloud servers, not your local machine, so this function cannot be used in those scenarios.Can I use a variable for N instead of a fixed number?
Yes. You can reference another column or parameter. For example:DateTime.IsInPreviousNSeconds([Timestamp], [ThresholdSeconds]) where ThresholdSeconds is a parameter you defined. This allows dynamic filtering based on user input.