Create Parameterized Excel Refreshable Reports
In my [previous post] I provided steps to created a simple Excel Pivot Report. This Report queries your SQL Server Table and pulls data from it and renders into the Excel Sheet.
Many of the time we don’t require whole data, and want to add conditions to restrict the data, like what if we just need to pull data for Q2 (2nd Quarter) of year 2009. This would require a Date Range to be applied to the SQL Query.
How about Start Date & End Date that will be filled by the user on Excel Sheet on particular cells? And those values will be applied to the SQL Query WHERE clause and will pull just the data lying between those Date Ranges?
>> Here I’ve defined how we can pass Parameters from the Excel-Sheet to the SQL query to filter data.
1. Please refer to my previous post, stop at step 13.
2. Now, open Connection Properties and go to Definition Tab and add the following line at the end of your query:
where [DATE_FIELD] between ? and ?
Now, what does “?” means.
By applying ? (question-mark) in our query in WHERE clause Excel identifies them as 2 parameters and asks you from where to pick the data.
Now point both the parameters to the desired cell in the excel-sheet as shown below:
And finally you are done… when you will close the “Connection Properties” window the pivot report will get refreshed itself and pull the data as per the date you have applied.
Awesome thanks
Great post, but…
Once I retrieve the data for specific parameter in the query, I would like to save it as ‘parameter-value.xlsx’ file and remove connection, so that it will not refresh once another person will open the spreadsheet.
Should I do this (remove connection) manually from the existing connections or is there any better way.
I think you will have to manually remove the connection here !
But you can automate it by using SSIS, which will run the desired query with specific parameter and save the results in an Excel file.
Thank you!
I think that SSIS could be an option if I had an Administrator permissions on the SQL Server machine.
Regarding SQL / Excel –> I have an issue with obtaining results from query –> https://superuser.com/questions/1294178/dql-sql-to-excel-obtain-results-of-multiple-select-statements-in-spreadsheet
Could you tell me if you know what I’m doing wrong, please?