Date.IsInPreviousNYears

Date

Returns true if the date falls within the previous N calendar years.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Date.IsInPreviousNYears(dateTime as any, years as number) as logical

Parameters

NameTypeRequiredDescription
dateTimeanyYesA date, datetime, or datetimezone value to test.
yearsnumberYesThe number of years back to include. Must be a positive integer.

Return Value

logicaltrue if the date falls within the N years before the current year, false otherwise.

Remarks

Date.IsInPreviousNYears returns true if the input date falls within the N complete calendar years preceding the current year. The current year itself is not included; use Date.IsInCurrentYear to also match the current year. The function is re-evaluated on each query refresh.

For example, if the current year is 2026 and years = 3, it matches any date in 2023, 2024, or 2025. This is useful for multi-year historical analysis such as a trailing-3-year revenue trend, a 5-year cohort comparison, or long-term asset depreciation data.

Note that this function counts whole calendar years, not a rolling 365-day window. If you need a rolling lookback, calculate the boundary using Date.AddYears(Date.From(DateTime.LocalNow()), -N) and compare directly.

Examples

Example 1: Filter orders from the previous 3 years

Table.SelectRows(
    Sales,
    each Date.IsInPreviousNYears([OrderDate], 3)
)
Result
OrderID
OrderDate
111/15/2024
221/18/2024
332/1/2024
442/10/2024
553/5/2024
663/12/2024
774/1/2024
884/15/2024

Example 2: Flag records within the past 2 years

Table.AddColumn(
    Sales,
    "IsRecentHistory", each Date.IsInPreviousNYears([OrderDate], 2), type logical
)
Result
OrderID
OrderDate
IsRecentHistory
111/15/2024TRUE
2423/8/2026FALSE
3305/15/2023FALSE

Example 3: Label rows by historical period for trend analysis

Table.AddColumn(
    Sales,
    "Period",
    each
        if Date.IsInCurrentYear([OrderDate]) then "Current Year"
        else if Date.IsInPreviousNYears([OrderDate], 3) then "Past 3 Years"
        else "Older",
    type text
)
Result
OrderID
OrderDate
Period
111/15/2024Past 3 Years
2423/8/2026Current Year
3305/15/2022Older

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks