Forum Discussion
How do you do a parameter query when not using Microsoft Query?
Matt Appel did you ever figure this out? It is driving me absolutely bananas.
Sorry, bwdur I never did. I've poured over every forum and StackOverflow post I could find and there's always solutions posted (like the one above), but I can never get them to work.
To update this thread, I'm still running the latest version of Office 365 Edition of Excel, the SQL databases I'm querying are SQL Server 2016 and 2008. PowerQuery is now built into Excel, and i'm using it extensively, but there's still not a clear way to pull a value from a cell and inject it into the SQL code before sending it to the server.
The workbook that spawned all this is still in use and has about 8 different SQL queries that all need the date updated to refresh. What I ended up doing is setting the dates in each query to variables, then just updating the variable dates each time I need to pull data. It's not ideal, but at least it works.
Edit: added some clarification
- bwdurFeb 20, 2020Copper Contributor
Crazy this used to work fine and dandy with Microsoft Query but lately all of my spreadsheets have been breaking randomly. I did just get this to work finally on a file using this thread with some modifications.
First I named some cells.
Then I went down the 2349 clicks to Advanced Editor and did what the above poster did, except I had to put DateTime instead of Date.
let
StartDate = Excel.CurrentWorkbook(){[Name="StartDate"]}[Content][Column1]{0},
EndDate = Excel.CurrentWorkbook(){[Name="EndDate"]}[Content][Column1]{0},
Source =Sql.Database("server", "database", [Query="select myColumn from myTable where MyDate between '" & DateTime.ToText(StartDate) &"' and '" & DateTime.ToText(EndDate) & "' and Annoyed = yes"])
in
SourceThe location of the ' and " really matter around the parameters to get it to go. I'm surprised there aren't more threads about this online honestly, it is way too difficult to do. I guess maybe other people are returning the entire table then filtering afterwards in Excel?
- Matt AppelFeb 20, 2020Copper Contributor
This works! You beautiful genius!
Here's the code I ended up using. The only thing I'm changing are the server IP and database name:
let
StartDate = Excel.CurrentWorkbook(){[Name="StartDate"]}[Content][Column1]{0},
EndDate = Excel.CurrentWorkbook(){[Name="EndDate"]}[Content][Column1]{0},
Source = Sql.Database("192.168.1.1", "MyDatabase", [Query="SELECT TOP 1000 * FROM Invoices#(lf) WHERE InvoiceDate BETWEEN '" & DateTime.ToText(StartDate) & "' and '" & DateTime.ToText(EndDate) & "' ORDER BY WorkDate DESC"])
in
SourceFor anyone else trying to figure out what the heck is happening here, the reason the " and ' are so critical and finnicky is because they're breaking up the text to send it to the right place. The " characters are switching between the primary coding language (M, I think) and SQL, and the ' need to be passed to SQL for the dates to work.
In straight SQL this query would look something like:
SELECT * FROM Invoices WHERE WorkDate BETWEEN '2020-01-01' AND '2020-02-01'
So we need to keep the ' between a pair of " so they're passed to SQL. If they're missing it'll throw an error. The & are concatenating the date values into the string that eventually is passed to the SQL engine.
Hopefully that makes sense.
I'm still a little unclear why there's the need for [Content][Column1]{0} after declaring each variable, but whatever.
Thanks again, bwdur
- bwdurFeb 21, 2020Copper Contributor
Matt Appel no problem. I've been trying to fix this on and off for a year but everything was just too complicated for the simple pulls I wanted to do. I'm glad we figured it out!
- SergeiBaklanFeb 20, 2020Diamond Contributor
I guess the issue was since on the time of previous post Power Query returned Excel date as date, and now as datetime. I'd don't rely on default behavior, it's better to force date value and use date format converting dates to texts, like
let Source = Excel.CurrentWorkbook(), StartDate = Date.From(Source{[Name="StartDate"]}[Content][Column1]{0}), EndDate = Date.From(Source{[Name="EndDate"]}[Content][Column1]{0}), SQLstart = Date.ToText(StartDate,"yyyy-mm-dd"), SQLend = Date.ToText(EndDate,"yyyy-mm-dd") ...
- bwdurFeb 21, 2020Copper Contributor
SergeiBaklan Thanks that'll surely "future proof" things.