Power Apps Filter Gallery By Dates In Previous ‘N’ Years

Filter a gallery to show only dates within the previous ‘N’ number of years where N is a number specified by the app maker.



Input

Calendar Dates is a SharePoint list with consecutive days starting 1/1/2018 and ending 12/31/2024 (2,557 rows)

TitleCalendarDate
Monday, January 1, 20181/1/2018
Tuesday, January 2, 20181/2/2018
Wednesday, January 3, 20181/3/2018
Thursday, January 4, 20181/4/2018
Friday, January 5, 20181/5/2018
Tuesday, December 31, 202412/31/2024


Code

Use this code in the Items property of a gallery.

With(
    {
        StartDate: Date(
            Year(Today())-2, // change this number
            Month(Today()),
            Day(Today())
        )+ 1,
        EndDate: Date(
            Year(Today()),
            Month(Today()),
            Day(Today())
        )
    },
    Filter(
        'Calendar Dates',
        CalendarDate >= StartDate,
        CalendarDate <= EndDate
    )
)


Output

Gallery shows only rows with a date in the previous 2 years. Current date is 6/16/2021

TitleCalendarDate
Monday, June 17, 20196/17/2019
Tuesday, June 18, 20196/18/2019
Wednesday, June 19, 20196/19/2019
Thursday, June 20, 20196/20/2019
Friday, June 21, 20196/21/2019
Wednesday, June 16, 20216/16/2021

(731 rows)