Smart Systems for Home

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

Power Query Date.IsInPreviousNDays Function Guide

· 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

The Date.IsInPreviousNDays function in Power Query checks whether a given date falls within the last N days relative to today (or another reference date), returning true or false. It is useful for filtering recent data—like sales, logs, or alerts—without hardcoding date ranges. You must have Power Query enabled in Excel or Power BI, and your date column must contain actual date or datetime values (not text). No additional add-ins or libraries are required.

What You'll Need

  • Excel (2016 or later) or Power BI Desktop installed
  • A table loaded into Power Query with a column containing valid date or datetime values
  • Basic familiarity with Power Query Editor (opening it, adding custom columns, applying filters)

Step-by-Step

  1. Open Excel or Power BI Desktop, then load your data. In Excel, go to Data > Get Data > From Other Sources > Blank Query (or select your source like Excel, CSV, etc.). In Power BI, go to Home > Transform data to open Power Query Editor.
  2. Locate your date column in the Queries pane. Ensure it is typed as Date or DateTime—not Text (if it’s Text, right-click the column header > Change Type > Date or DateTime, and fix any errors).
  3. In Power Query Editor, click Add Column > Custom Column (or use the formula bar if you prefer typing directly).
  4. In the Custom Column dialog, enter a name like "IsRecent" in the Column name field.
  5. In the Custom column formula box, type the function call: Date.IsInPreviousNDays([YourColumnName], N). Replace [YourColumnName] with the actual column name (e.g., [OrderDate]) and replace N with the number of days (e.g., 7 for the past week). For example: Date.IsInPreviousNDays([OrderDate], 7).
  6. Click OK. Power Query evaluates each row and returns true if the date is within the last N days, false otherwise. Note: the function uses your system’s local date (based on time zone settings), so ensure your computer’s clock and time zone are correct.
  7. To filter for recent records only, click the filter icon ( funnel ) on the new column header, check only "true", and click OK. Alternatively, keep the column for conditional formatting or further logic.

Common Mistakes

  • Using text-formatted dates: If your column is Text, the function returns an error or false for all rows. Convert to Date or DateTime first using the UI or Table.TransformColumns.
  • Incorrect reference date: Date.IsInPreviousNDays always uses today’s date as the endpoint (unless wrapped in a custom expression). It does not accept a second date parameter—so it can’t compare against a static date like "2023-12-31". To filter relative to another date, subtract days manually: is invalid. Instead, use logic like .
  • Time zone mismatches: If your data includes UTC timestamps and you’re in a different time zone, the function may misclassify dates near midnight. Consider converting to local time first with DateTime.ToLocal or DateTime.LocalNow.
  • Assuming inclusive start: The function includes the current day if it falls within the N-day window. For example, with N=1, it includes today and yesterday (2 days total). Clarify your business definition of "previous N days"—is it strictly past days, or including today?
  • Confusing with Date.IsInPreviousNMonths/Years: This function is strictly for days. Don’t use it for weeks or months; use Date.IsInPreviousNWeeks or Date.IsInPreviousNMonths instead.

Tips

  • Combine with Date.From to ignore time components and avoid edge-case mismatches: Date.IsInPreviousNDays(Date.From([OrderDate]), 7).
  • Use in a conditional column (Add Column > Conditional Column) for more readable filters—e.g., "Recent", "Older"—instead of true/false.
  • Store N as a parameter: create a new query named "DaysBack" with value 7, then reference it: . This makes updating the window effortless.
  • For dynamic filtering (e.g., based on user selection), use parameters in Power BI (Modeling > New Parameter) and bind them to slicers.
  • Test edge cases: try dates exactly N+1 days ago, and today at 23:59:59, to verify behavior before applying broadly.
  • When building reports, add a comment in the Advanced Editor: .

Troubleshooting

If the function returns errors or unexpected results:

  1. Check column type: Hover over the column header in Power Query Editor—type should be Date or DateTime. If it’s Any or Text, convert it. Click the icon next to the column name and choose the correct type.
  2. Verify syntax: Open the Advanced Editor (Home > Advanced Editor) and search for your custom column. Ensure brackets match: [DateColumn], not "DateColumn" or date([DateColumn]).
  3. Check for nulls: Null dates cause errors. Wrap in if ... then ... else false : .
  4. Time zone issues: Add a column with DateTime.LocalNow() and DateTime.FixedLocalNow() to confirm the system time matches expectations. If your data is UTC, use before comparison.
  5. Power BI desktop vs. service: If it works locally but not in Power BI Service, check if the dataset uses direct query—some functions behave differently. Also, ensure your gateway (if applicable) is up to date.
  6. Refresh failures: If the query fails on refresh, open the error details. Common fixes: rename columns referenced, re-map sources, or simplify the expression to isolate the issue.

Frequently Asked Questions

Does Date.IsInPreviousNDays include today?

Yes, it includes today. For N=1, it covers today and yesterday. For N=7, it covers the last 7 days including today (i.e., 7 full calendar days ending now).

Can I use it with a datetime column?

Yes, but the function uses the full datetime. To avoid time-of-day mismatches (e.g., a record at 23:55 yesterday may be excluded if N is very small), wrap with Date.From to truncate to date-only before comparison.

What happens if the date column has invalid dates?

Power Query will return an error for those rows. Fix by filtering out errors first: right-click the date column > Filter > Filter Errors, or use try ... otherwise in your custom column.

Is this function available in Power BI Service (online)?

Yes, but only if the dataset is imported mode. In DirectQuery or Live Connection modes, some date functions may be translated differently or unsupported—check the Power BI documentation for your specific data source.

Can I compare against a specific date (e.g., end of last month)?

Not directly. Use Date.IsInPreviousNDays only for relative days from today. For fixed dates, use arithmetic: for last month’s start.

Why does my result differ from Excel’s TODAY() formula?

Power Query evaluates expressions at refresh time, not live. If you refresh at 11:59 PM, tomorrow’s midnight resets the window. Also, ensure your system time zone matches your data’s expected zone—mismatches cause off-by-one-day errors near midnight.