Smart Systems for Home

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

DateTime.IsInPreviousNMinutes: Power Query Guide

· 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

DateTime.IsInPreviousNMinutes is a Power Query M function that returns true if a given datetime value falls within the previous N minutes relative to the current system date and time, and false otherwise. In plain terms: it answers the question "was this timestamp somewhere in the last few minutes?" without you writing any date subtraction by hand. It is most useful for flagging fresh events, such as an alarm that fired in the last five minutes or a device that checked in recently. To use it you only need a datetime column and a new custom column in the Power Query Editor; the function itself takes two arguments: the datetime value to test and the number of minutes to look back. One important catch: it is evaluated against the moment the query refreshes, not against a stored snapshot, so results change every time the data is refreshed.

What You'll Need

  • Power BI Desktop, Excel with Power Query, or Analysis Services with Power Query M available, and a query that already loads a column containing valid datetime values.
  • Permission to edit the query in the Power Query Editor (the connection must not be read-only, and in the Power BI service you edit the data model in Desktop, not the browser).
  • A column typed as Date/Time. If the column is text, convert it first with the type menu at the top left of the column header; otherwise the function returns an error.
  • An understanding that the result depends on the refresh clock, so it is only meaningful when the underlying data is up to date.

Step-by-Step

  1. Open your report or workbook in Power BI Desktop or Excel, then launch the Power Query Editor: in Power BI Desktop use the Home tab and choose Transform data; in Excel choose Data and then From Table/Range or open the existing query through Queries & Connections. Menu labels can differ slightly by version and by whether you are on Windows or the web.
  2. Select the query that contains your timestamp column in the Queries pane on the left. If that column lives in another table, either merge the tables first or write the test against a lookup — the function only sees the value you hand it, so the column must be in the query you are editing.
  3. Confirm the column data type. Click the type icon to the left of the column header and make sure it reads Date/Time. A column of text that looks like a date will still throw an error, and a column typed as Time or Date alone has no time component to compare, so it will not give the answer you expect.
  4. On the Add Column tab, choose Custom Column. This opens a dialog with a name box and a formula box.
  5. Name the new column something readable, such as Is recent or Last 5 min.
  6. Enter the formula in the box, for example DateTime.IsInPreviousNMinutes([Created], 5). The first argument is the column reference in square brackets; the second is the number of minutes you want to look back. Do not put quotes around the column name or around the number.
  7. Click OK. Power Query adds a column of logical true/false values. The formula bar will show the step, and you can rename the step for clarity later.
  8. Check the whole column does not show Error. Click the whitespace at the top of the column to see any errors listed, then drill into a row to find the bad value. Fix the source value or filter it out.
  9. Optionally convert the logical result into something more readable. Add another custom column with if DateTime.IsInPreviousNMinutes([Created], 5) then "New" else "Older", or filter the logical column to keep only true rows.
  10. Click Close & Load (or Close & Apply in Excel) to bring the result back into the model, then build the visual, slicer or measure that uses the new column. Refresh the query to see the classification shift as time passes.

Common Mistakes

The most frequent error is passing the column name as text, for example DateTime.IsInPreviousNMinutes("Created", 5). The function expects a datetime value, so the string fails. Use square brackets with no quotes: [Created].

Second, people forget that the window is strictly the previous N minutes, meaning it looks from now back to now minus N, and it does not include timestamps in the future or exactly at the boundary in the way they assume. A timestamp one second older than the cut-off returns false, which surprises anyone expecting a rounded result.

Third, mixing 5 for minutes with 5 hoping for seconds or hours. The number is minutes only; for different units use DateTime.IsInPreviousNHours, DateTime.IsInPreviousNDays or the equivalent current-window functions such as DateTime.IsInCurrentHour.

Fourth, a column typed as text or as Date only. Add an intermediate step to convert it with DateTime.From, or change the type, and check for local versus UTC offsets, because a UTC timestamp compared against a local clock will be off by the offset.

Finally, relying on the column for historical filtering. Because the test recalculates on every refresh, a saved report will show different counts tomorrow. For a stable flag, compute the difference explicitly against a fixed reference date instead.

Tips

Keep the minute value as a named parameter if you reuse the same window in several queries, so one change updates everything. Test the function in a small query with one or two rows before applying it to a large table; debugging logic on a handful of rows is faster than on a million. To see exactly what the function evaluates, add a helper column with DateTime.LocalNow() beside the test; comparing the two side by side explains most "wrong" results. If your data is time-zone sensitive, convert to a consistent zone with DateTimeZone functions first, or the comparison will quietly drift by hours, not minutes.

Troubleshooting

If every row shows false, check the timestamp values themselves: they may all be older than the window, or they may be stored in UTC while the query runs in local time. Add a helper column with DateTime.LocalNow() and compare. If every row shows Error, the column type or a null value is the cause; filter out nulls first.

If the function name itself is rejected with a syntax error, confirm you are on a version of Power Query that supports it and that the name is spelled with the DateTime prefix, capital I in Is, and capital N, with no spaces. The function appears in the formula bar's autocomplete list when the name is correct.

If the column loads but filters wrongly when refreshed, remember the result is refresh-dependent. Check that the data source itself refreshed and that the gateway, if you use one, is running. If results differ between Desktop and the published report, the clocks or time zones differ. For anything deeper, open the query in the Advanced Editor and inspect the step order; a step placed before the type change can explain a lot.

Frequently Asked Questions

Does DateTime.IsInPreviousNMinutes include the current minute?

No. It tests the interval from now back N minutes, so the current moment is effectively the end boundary. Timestamps in the future return false.

What is the difference between this and DateTime.IsInCurrentMinute?

DateTime.IsInCurrentMinute checks only the single minute you are in right now. DateTime.IsInPreviousNMinutes checks a rolling window of your chosen length.

Can I use it on a date-only column?

No, it needs a time component. Convert the value with DateTime.From first, or you will get misleading results.

Why does the result change after refresh?

The function compares against the refresh clock through DateTime.LocalNow(), so each refresh re-evaluates every row against the new time.

Does it work in Excel as well as Power BI?

Yes. Power Query in Excel uses the same M engine and function, so the formula is identical, though the menu paths differ slightly.

How do I get hours or days instead of minutes?

Use DateTime.IsInPreviousNHours or DateTime.IsInPreviousNDays; the unit is fixed within each function name.