I have a SQL query with date parameters. I would like to use PowerQuery to run this…
- User
I have a SQL query with date parameters. I would like to use PowerQuery to run this query. The parameters will be entered by the user into cells in a worksheet. I would like to pass these parameters to the SQL query in Excel without losing query folding in PowerQuery. Is this possible?
- Assistant
Yes, it is possible to pass date parameters entered in cells in a worksheet to a SQL query in PowerQuery while preserving query folding. To do this, you'll need to create a parameter in PowerQuery and then reference the cell in the worksheet that contains the date value in the SQL query. This way, when the date value in the cell changes, the parameter in PowerQuery will also change, and the query results will be updated accordingly. Here's an example of how you can set this up:
- In PowerQuery, create a new parameter by going to Home > Manage Parameters. Give the parameter a descriptive name
- User
Please modify the following query to replace 202301 with the parameter syntax shown above.
select * from gl where period = 202301
- Assistant
Here's the modified query that uses the parameter syntax:
scsslet StartPeriod = Excel.CurrentWorkbook(){[Name="StartPeriod"]}[Content]{0}[Column1], SQL = "select * from gl where period = " & Text.From(StartPeriod); Sql.Database("server", "database", [Query=SQL])In this example,
StartPeriodis the name of the parameter, andExcel.CurrentWorkbook(){[Name="StartPeriod"]}[Content]{0}[Column1]is the reference to the cell in the worksheet that contains the value for the period. TheText.Fromfunction is used to convert the value of the parameter to text, and the resulting text is concatenated with the rest of the SQL query to form the final query that will be executed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.