Smart Systems for Home

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

Power Query Date.IsInPreviousNQuarters 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.IsInPreviousNQuarters function in Power Query checks whether a given date falls within the previous N quarters relative to today’s date (or a specified reference date). It returns true if the date is in one of the last N quarters, and false otherwise. This function is useful for filtering data for reporting, such as selecting all transactions from the last two quarters for a YTD vs. prior-year analysis. No special setup is needed beyond having a valid date column in your data and knowing how to add custom columns or apply filters using Power Query’s editor.

What You’ll Need

  • A Power Query environment (available in Excel, Power BI Desktop, or Microsoft Dataflows)
  • A table or query containing at least one column with valid date values (e.g., Order Date, Transaction Date)
  • Basic familiarity with Power Query’s editor interface (opening queries, viewing columns, editing M code)

Step-by-Step

  1. Open Power Query by loading your data into Excel (Data > Get Data > From Other Sources) or Power BI (Home > Transform data). Ensure your source table is loaded as a query.
  2. In the Power Query Editor, locate the column containing your dates. Right-click the column header and select Remove Other Columns if you want to work with just the date column for simplicity, or keep your full dataset.
  3. With the date column visible, go to the Add Column tab on the ribbon. Click Custom Column to open the dialog box.
  4. In the Custom Column dialog, enter a name like IsLast2Quarters in the Column name field.
  5. In the Custom column formula box, type or paste the function. For example, to check if the date is in the previous 2 quarters relative to today, use: Date.IsInPreviousNQuarters([YourDateColumn], 2) Replace YourDateColumn with the actual column name (include brackets if it’s a field reference).
  6. Click OK. A new column appears with true or false values based on the comparison.
  7. To filter rows to only those in the last 2 quarters, click the dropdown arrow on the new column header. Uncheck false and leave true selected, then click OK.
  8. Optionally, rename or reposition the column using the column header context menu (Move Up, Move Down, or Rename).
  9. Click Close & Apply (Power BI) or Close & Load (Excel) to return the filtered data to your worksheet or report.

Common Mistakes

  • Using text instead of date values: If your date column is formatted as text (e.g., “2024-03-15”), the function will return an error or false unexpectedly. Convert the column to the Date type using Transform > Data Type before applying the function.
  • Incorrect reference quarter: The function uses the system’s current date by default. If you need a fixed reference date (e.g., for historical analysis), you must use Date.IsInPreviousNQuarters(date, n, referenceDate) with a third argument (see Tips).
  • Off-by-one confusion: Date.IsInPreviousNQuarters(date, 1) includes only the immediately preceding quarter, not the current one. To include the current quarter, use Date.IsInCurrentQuarter separately or combine conditions.
  • Mistyping the function name: M is case-sensitive in some contexts, but Power Query accepts Date.IsInPreviousNQuarters in any casing. However, avoid abbreviations like IsInPrevNQ — they won’t work.

Tips

  • Use a reference date for reporting periods: For consistent analysis across reports (e.g., always comparing to Q4 2024), define a parameter or custom column for the reference date. Example: Date.IsInPreviousNQuarters([OrderDate], 2, #datetime(2024, 12, 31, 23, 59, 59)) ensures the function evaluates relative to year-end 2024.
  • Combine with other functions: To build more complex filters (e.g., last 2 quarters or current quarter), use or in a custom column: .
  • Test with sample data: Add a few known dates (e.g., today, 6 months ago, 2 years ago) to verify the logic before applying to your full dataset.
  • Understand quarter boundaries: Quarters run Jan–Mar (Q1), Apr–Jun (Q2), Jul–Sep (Q3), Oct–Dec (Q4). Date.IsInPreviousNQuarters uses these standard calendar quarters regardless of fiscal year settings.

Troubleshooting

If your function returns errors or unexpected results, follow these checks:

  1. Check column data type: Select the date column, go to the Transform tab, and verify the type is Date or DateTime. If it’s Any or Text, click Change Type and choose the correct type.
  2. Inspect the error message: If the custom column shows Error, click the cell to see details. Common issues include referencing a non-existent column name (e.g., typo in [Order Date] vs [OrderDate]) or passing a non-date value (e.g., null).
  3. Handle nulls explicitly: If your date column may contain blanks, wrap the function in a conditional:
  4. Verify system clock: The function uses your computer’s current date/time. If your system clock is wrong (e.g., set to last month), the result will be inaccurate. Check Date & Time settings.
  5. Test manually: In the Power Query formula bar (visible when a cell is selected), try a simple test: =Date.IsInPreviousNQuarters(#date(2024,7,15), 1). If today is Q3 2024, this should return true (since Q2 2024 is the previous quarter).

Frequently Asked Questions

Does Date.IsInPreviousNQuarters work with fiscal quarters?

No — it strictly uses calendar quarters (Jan–Mar, Apr–Jun, etc.). For fiscal years, you must calculate your own quarter numbers using Date.QuarterOfYear and Date.Year, then compare relative to your fiscal start date.

Can I use this function in a DAX measure instead of Power Query?

No. Date.IsInPreviousNQuarters is an M function and only works in Power Query (data transformation). In DAX (for measures or calculated columns in Power BI), use functions like DATEADD, SAMEPERIODLASTYEAR, or custom logic with QUARTER and YEAR.

What happens if I pass a future date?

Future dates always return false, since they are not in the previous N quarters relative to today. For example, if today is Q2 2025, a date in Q3 2025 will return false for n=1 or n=2.

Does this function account for time zones?

No. Power Query uses your local system time zone for the current date. If your data contains UTC timestamps and your system is set to a different zone, the quarter evaluation may shift across a day boundary. To avoid this, convert timestamps to your target time zone first using DateTime.LocalFromUniversal or similar.

Is there a limit to how high N can be?

No hard limit exists — N can be any positive integer. However, very large values (e.g., N=100) may impact performance on large datasets, as the function evaluates each row individually. For extreme cases, consider filtering by year first.

Can I use this in a custom table filter instead of a column?

Yes — you can apply a row filter directly in the query. Click the filter icon on your date column, choose Filter Rows > Custom Filter , and enter: . This skips creating a helper column entirely.