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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
wasonj1
Frequent Visitor

SQL Statement Variable Date Range

Hi There,

I have a query that is using an SQL statement that is currently pulling data between 2018-07-01 and 2019-07-01. I want to update the SQL statement to be able to pull data between the current date and 2 years older without hardcoding the dates. Can someone tell me how to update my SQL statement to do this?

powerbi_sql_datevariable.jpg

2 ACCEPTED SOLUTIONS
danextian
Super User
Super User

Hi @wasonj1 ,

In Power Query, you can add a custom step to filter by an automatic date range:

= Table.SelectRows(#"Name of Previous Step", each [DateColumn] >= Date.AddYears(Date.From(DateTimeZone.LocalNow()), -2))

 

In SQL, you can use just filter by dates using the SQL syntax. I am not very familiar with SQL but the code should be something like this:

WHERE M.Response_Date >= DATEADD(YEAR, -2, GETDATE())

 or perhaps

WHERE M.Response_Date >= DATEADD(YEAR, -2, TODAY())




Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

dax
Community Support
Community Support

Hi @wasonj1 , 

You could try to refer to @danextian 's suggestions, or you also could refer to Pass-parameter-to-SQL-Queries-statement-using-Power-BI  for details.

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

2 REPLIES 2
dax
Community Support
Community Support

Hi @wasonj1 , 

You could try to refer to @danextian 's suggestions, or you also could refer to Pass-parameter-to-SQL-Queries-statement-using-Power-BI  for details.

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

danextian
Super User
Super User

Hi @wasonj1 ,

In Power Query, you can add a custom step to filter by an automatic date range:

= Table.SelectRows(#"Name of Previous Step", each [DateColumn] >= Date.AddYears(Date.From(DateTimeZone.LocalNow()), -2))

 

In SQL, you can use just filter by dates using the SQL syntax. I am not very familiar with SQL but the code should be something like this:

WHERE M.Response_Date >= DATEADD(YEAR, -2, GETDATE())

 or perhaps

WHERE M.Response_Date >= DATEADD(YEAR, -2, TODAY())




Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.