Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
villa1980
Resolver II
Resolver II

filter previous 2 years power query help

Hi all,

 

 I have a table that is currently looking at data from 2021 in column TRANSACTION_DATE.

 I would like to filter this in Power Query M to Transaction_Date is >= 2 years previous (which will change next year to 2024, 2025, 2026 etc), so would see 2023, 2024 and 2025 this year.

 Cannot figure out how to do this within the applied steps formula.


 Your help would be appreciated.

 Many Thanks

Alex

2 ACCEPTED SOLUTIONS
villa1980
Resolver II
Resolver II

Managed to find the solution....

 

Table.SelectRows(#"Changed Type", each [TRANSACTION_DATE] >= DateTime.Date(Date.AddYears(DateTime.LocalNow(),-2)))

View solution in original post

Bibiano_Geraldo
Super User
Super User

Hi @villa1980 ,

In power query, please add a new custom column using this M code to get the year:

Date.Year([TRANSACTION_DATE])

 Now in APPLIED STEPS, right click in last step and inser step after with this M code:

= Table.SelectRows(#"Added Custom", each [Custom] >= Date.Year(DateTime.LocalNow()) - 2)

This will dynamically adjust each year and include data for the last two years plus the current year.

 

 

View solution in original post

3 REPLIES 3
J_S_A
Helper I
Helper I

Hi @Bibiano_Geraldo ,
Just found this query because I need to do the same and saw the solution that you have proposed. I'd like to know what is the difference between that solution and the following one:

Table.SelectRows(FilteredYears, each Date.IsInPreviousNYears([Estimated delivery date], 2))

That is created in a step in Power Query, by going to the filter of the column "Estimated delivery date" ---> Date Filters --> Is in the previous --> 2 years

J_S_A_0-1763552944975.png

 

 

Thanks in advance!

Bibiano_Geraldo
Super User
Super User

Hi @villa1980 ,

In power query, please add a new custom column using this M code to get the year:

Date.Year([TRANSACTION_DATE])

 Now in APPLIED STEPS, right click in last step and inser step after with this M code:

= Table.SelectRows(#"Added Custom", each [Custom] >= Date.Year(DateTime.LocalNow()) - 2)

This will dynamically adjust each year and include data for the last two years plus the current year.

 

 

villa1980
Resolver II
Resolver II

Managed to find the solution....

 

Table.SelectRows(#"Changed Type", each [TRANSACTION_DATE] >= DateTime.Date(Date.AddYears(DateTime.LocalNow(),-2)))

Helpful resources

Announcements
FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.