Forum Discussion
Dynamic Date Parameter for Import Queries
- 5 years ago
Yes
First, create a new blank query in Power Query. Use this as the formula. This assumes SQL is using a date field.
= Date.AddYears(DateTime.Date(DateTime.LocalNow()), -1)This will create a scalar value of 10/12/2019 - one year ago. (US Date format - your format might show 19/10/2019 depending on system setttings.)
- Rename the query varStartDate
- Right-click on the query and uncheck "Enable Load"
Then, in your SQL table, filter the date to whatever you want. Doesn't matter. You just want the code to be generated. It might look like this
now you have a Table.SelectRows() that looks like this in the formula bar:
Change it to look like this:
You are getting rid of the date you used to create the filter with varStartDate - which is a dynamically calculated date. It will change every day.
If this is running against a SQL server, this has the added benefit of folding the query, so the server gets this code:
Every time it runs, it will dynamically calculate varStartDate, then will change the native query for SQL Server to use that date. Tomorrow, it would change it to '2019-10-13 00:00:00' and so on.
Yes
First, create a new blank query in Power Query. Use this as the formula. This assumes SQL is using a date field.
= Date.AddYears(DateTime.Date(DateTime.LocalNow()), -1)
This will create a scalar value of 10/12/2019 - one year ago. (US Date format - your format might show 19/10/2019 depending on system setttings.)
- Rename the query varStartDate
- Right-click on the query and uncheck "Enable Load"
Then, in your SQL table, filter the date to whatever you want. Doesn't matter. You just want the code to be generated. It might look like this
now you have a Table.SelectRows() that looks like this in the formula bar:
Change it to look like this:
You are getting rid of the date you used to create the filter with varStartDate - which is a dynamically calculated date. It will change every day.
If this is running against a SQL server, this has the added benefit of folding the query, so the server gets this code:
Every time it runs, it will dynamically calculate varStartDate, then will change the native query for SQL Server to use that date. Tomorrow, it would change it to '2019-10-13 00:00:00' and so on.
Perfect! Worked like a charm!! Thank you!!!
Hopefully, someday, implementing date parameters will be easier. π
- edhans5 years agoCommunity Champion
Glad to help out jcampbell474 .
- Laser_Tim_882 years agoAdvocate I
Agreed! Even here at the end of 2023 Dynamic Paramters are hard to implentment into Import Queries.