Forum Discussion
Issue passing variable to SQL Query
If you wish to create a dynamically changing SQL statement based on parameters in PowerQuery, I'd recommend essentially the following:
First, create a function to execute a SQL query, then import the SQL text from a .sql or .txt (really whatever you prefer), use the Text.Replace function to replace a placeholder in your code (For example, supposing your text was:
SELECT *
FROM DB.Table
WHERE ConditionColumn = [DYNAMICCHANGINGVALUE]You'd run Text.Replace on that filtering [DYNAMICCHANGINGVALUE] and substituting your parameter,
and then run the query on your SQL in a blank query.
This would allow you to dynamically run what you'd like. The only limitation that I've found is that if you want this to automatically run on the service, you need a specific gateway configuration, but if you're doing this in desktop, you shouldn't have problems. Hope this helps!
If you want more details I can probably provide those as well, just don't know how much is useful.