Smart Systems for Home

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

Power Query DateTime.IsInNextNSeconds: How to Use

· 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

DateTime.IsInNextNSeconds is a Power Query M function that returns true if a datetime value falls within the next full N seconds from now, excluding the current second. Use it to flag records that are about to happen in the immediate future—for example, a countdown timer, a just-in-time alert, or a rolling window of upcoming events. It takes a datetime and a number of seconds, and you can call it in a custom column or a filter step. There is no separate installation; the function is built into Power Query in Excel and Power BI.

What You'll Need

  • A table with a column of DateTime values (type date/time, not text).
  • Power Query Editor open in Excel (Data > Get Data) or Power BI (Transform data).
  • Basic familiarity with adding a custom column or editing an M formula.

Step-by-Step

  1. Load your data into Power Query. Select your table and choose Data > From Table/Range (Excel) or Transform data (Power BI). The Power Query Editor opens.
  2. Confirm the column you want to test is typed as Date/Time. Click the column header icon and choose Date/Time if it shows Text or Any. The function requires a datetime value; text will cause an error.
  3. Add a custom column: go to Add Column > Custom Column. Name it something like "InNext30s".
  4. In the formula box, enter DateTime.IsInNextNSeconds([YourDateTimeColumn], 30), replacing YourDateTimeColumn with your actual column name and 30 with the number of seconds you need. Use a whole number; decimals are allowed but rarely useful.
  5. Click OK. The new column shows true or false for each row.
  6. To keep only the upcoming rows, click the filter arrow on the new column and select true. To keep all rows and just flag them, leave the filter off.
  7. Close & Load (Excel) or Close & Apply (Power BI) to bring the result back to your worksheet or model.

If you prefer to write the logic directly in the formula bar, you can also use Table.AddColumn(YourTable, "InNext30s", each DateTime.IsInNextNSeconds([YourDateTimeColumn], 30)). The result is the same; the Custom Column dialog simply generates this step for you.

Common Mistakes

  • Passing text instead of datetime. If the column is typed as Text, the function returns an error. Change the type first.
  • Expecting the current second to count. The function checks the next N seconds starting from the next whole second; a value equal to "now" is not included. If you need to include the current second, add a small buffer or use a different comparison.
  • Using a negative or non-integer N. Negative values return false for all rows; very large values may be slow. Use a positive whole number.
  • Forgetting that "now" is evaluated at refresh time. The result changes each time the query refreshes. If you need a fixed reference point, store a timestamp in a parameter and compare against that instead.
  • Assuming it works on dates only. A pure date (no time) is treated as midnight; the function will usually return false unless midnight falls in the window.

Tips

  • Use a parameter for N so you can change the window without editing the formula. In Power Query, create a parameter (Home > Manage Parameters) and reference it in the custom column.
  • Combine with DateTime.LocalNow() or DateTime.FixedLocalNow() in a separate column to see the reference time used for the test.
  • For a rolling "next minute" view, use DateTime.IsInNextNMinutes with 1 instead of 60 seconds; it is easier to read and does the same job.
  • If you need to test a range (for example, from 10 seconds ago to 30 seconds ahead), combine DateTime.IsInPreviousNSeconds and DateTime.IsInNextNSeconds with or.
  • Keep the column name simple and avoid spaces; brackets in M formulas can be confusing if the name contains special characters.

Troubleshooting

If the new column shows errors, check the column type first: select the column, then Home > Data Type and set it to Date/Time. If the column contains nulls, the function returns null for those rows; filter them out or use try ... otherwise false to treat nulls as false. If every row returns false, confirm the reference time: the function uses the current date and time at refresh, so rows far in the past or future will never match. If the query is slow, reduce N or filter the table to a smaller set before adding the column. If the function name is not recognized, make sure you are using Power Query (not a DAX formula) and that you typed the name exactly, including capitalization. Finally, if the result does not update when you expect, refresh the query; the function is not volatile inside a single evaluation.

Frequently Asked Questions

What does DateTime.IsInNextNSeconds actually return?

It returns a logical value: true if the datetime is in the next N seconds from the current moment, false otherwise. The current second is excluded.

Can I use it with a column that contains only dates?

Yes, but a date is treated as midnight. It will only return true if midnight falls within the next N seconds, which is rare. Convert to datetime first if you need time-of-day precision.

Does the function work in Power BI and Excel alike?

Yes. It is part of the standard M library in both Power Query experiences. The menus may differ slightly by version, but the function behaves the same.

How do I include the current second in the test?

The function excludes it by design. To include it, compare directly: [YourColumn] >= DateTime.LocalNow() and [YourColumn] <= DateTime.LocalNow() + #duration(0,0,0,N).

Why do all my rows return false after refresh?

Because the reference time moved forward. If your data is static and in the past, no row will fall in the next N seconds. Use a fixed timestamp parameter for a stable test.

Can I use a decimal for N?

Yes, but the function works at second granularity. A decimal like 0.5 is effectively rounded or treated as less than one second; use whole seconds for predictable results.