Smart Systems for Home

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

Power Query Date.IsInNextNYears 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.IsInNextNYears function in Power Query checks whether a given date falls within the next N years from today (or from a specified reference date). It returns true or false and is useful for filtering upcoming events, contracts, or deadlines in data models. This function is built into Power Query and requires no additional tools or setup beyond having Excel (2016 or later) or Power BI Desktop installed with Power Query enabled.

What You'll Need

  • Microsoft Excel 2016 or later (including Microsoft 365), or Power BI Desktop
  • A data table containing at least one date column (e.g., StartDate, ExpiryDate)
  • Basic familiarity with Power Query Editor (opening it, adding a custom column)

Step-by-Step

  1. Open your Excel workbook or Power BI report and load the data containing your date column into Power Query. In Excel, go to Data > Get Data > From Other Sources > From Table/Range (or select any cell in your table and click Data > From Table/Range in Excel 365). In Power BI, click Home > Transform data.
  2. In the Power Query Editor, locate your date column (e.g., DueDate). If your column is not already formatted as a date, right-click the column header > Change Type > Date (or Date/Time if times are present). This prevents type errors in the function.
  3. Click Add Column > Custom Column to open the custom column dialog box. This is where you’ll write the Date.IsInNextNYears expression.
  4. In the Custom column name field, enter a descriptive name like InNext3Years or IsUpcoming.
  5. In the Custom column formula box, enter the full function call. The syntax is: Date.IsInNextNYears(date, years, optional referenceDate). For a simple check against today’s date, use: Date.IsInNextNYears([DueDate], 3). This returns true for any date within the next three years from today.
  6. To use a fixed reference date instead of today (e.g., to simulate a scenario as of 1 January 2025), add the optional third argument: Date.IsInNextNYears([DueDate], 3, #date(2025, 1, 1)). Ensure the reference date is written in #date(year, month, day) format.
  7. Click OK. The new column appears with true or false values. You can now sort, filter, or flag rows accordingly—for example, right-click the new column > Filter > True to show only items due in the next N years.
  8. To finalize, click Close & Load (Excel) or Close & Apply (Power BI) to load the updated table back into your workbook or report model.

Common Mistakes

  • Using text instead of dates: If your source column contains dates stored as text (e.g., "2025-06-30"), the function will throw an error. Always convert the column to Date or Date/Time first.
  • Incorrect number of arguments : The function requires at least two arguments: the date and the number of years. Omitting years (e.g., ) results in a Expression.Error .
  • Using decimal or negative years: The years argument must be a whole number ≥ 0. Passing 0.5 or -1 causes an error. For half-year windows, multiply by 2 and use Date.IsInNextNMonths instead.
  • Assuming it includes today : The function checks the *next N years starting from today*, so dates on or before today return false . To include today, use and combine with Date.IsInCurrentYear logic if needed.
  • Time zone confusion: Date.IsInNextNYears uses your system’s local date, but if your data contains UTC timestamps, convert them to local time first using Date.From(#datetimezone(...), timezone).

Tips

  • Combine with Date.IsInCurrentYear for clarity : To explicitly include the current year plus the next N years, wrap your logic: .
  • Parameterize the year count : Instead of hardcoding 3 , create a parameter (e.g., NextYears = 3 ) and reference it: . This makes it easy to adjust the window in one place.
  • Use in conditional columns: For more readable outputs, create a custom column with if Date.IsInNextNYears([Expiry], 1) then "Renew Soon" else "OK".
  • Handle nulls safely : If your date column may contain blanks, wrap the check: to avoid errors.
  • Combine with Date.Year for custom logic : To compare only years (e.g., same year as today + 2), use: .

Troubleshooting

If Date.IsInNextNYears returns an error or unexpected results, follow these checks:

  1. Verify the input column type: Hover over the column header in Power Query. If it shows Any or Text, convert it to Date (right-click > Change Type).
  2. Test with a static date: Create a test row with a known future date (e.g., 1 January 2027). Use Date.IsInNextNYears(#date(2027,1,1), 3) in a new custom column. It should return true.
  3. Check for time components: If your date column includes times (e.g., 2025-12-31 23:59:59), ensure you’re comparing dates only. Use Date.From([DateTimeColumn]) inside the function: Date.IsInNextNYears(Date.From([DateTimeColumn]), 2).
  4. Review the years argument: Ensure it’s an integer. If you derived it from another column (e.g., [YearsToAdd]), confirm that column contains whole numbers and no text or nulls.
  5. Inspect the reference date: If you used the optional third argument, verify the #date(...) syntax. A typo like #date(2025,13,1) (month 13) causes a Expression.Error.
  6. Restart Power Query: Occasionally, the editor caches errors. Close and reopen Excel/Power BI, reload the query, and reapply the step.

Frequently Asked Questions

Does Date.IsInNextNYears include today’s date?

No. It checks strictly *after* today. For example, Date.IsInNextNYears(Date.From(DateTime.LocalNow()), 0) returns false because zero years from today means no future window. To include today, combine with Date.IsInCurrentYear.

Can I use this function to check if a date is in the *past* N years?

No. The function only checks *future* periods. For past dates, use Date.IsInPreviousNYears (e.g., ).

Does this function work with time zones?

Not directly. The function uses your local system date. If your data includes time zones (e.g., #datetimezone(2025,6,30,14,0,0,#duration(0,5,0,0))), convert to local time first using Date.From(#datetimezone(...), #duration(0,0,0,0)) to align with your system’s date context.

What happens if my date column has invalid dates (e.g., 30 February)?

Power Query will flag the row as having an error during evaluation. Fix invalid dates in your source or use try ... otherwise in M code: .

Is Date.IsInNextNYears case-sensitive or locale-dependent?

Neither. The function uses numeric year comparisons and is not affected by regional date formats. However, Date.From() behavior can vary by locale—use explicit #date(year, month, day) for reliability.

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

No. Date.IsInNextNYears is a Power Query (M language) function. In DAX (used for measures and calculated columns in Power BI), use to replicate the logic.