I have an Excel table in which the end user enters reference numbers into the "RefNum" column. I also created a "RefNum_SQL" column with a formula that automatically formats the list with commas and parentheses for the WHERE clause, in case that makes the SQL/M portions easier. We will refer to this table as the "Input" table:
RefNum RefNum_Formatted
2123 (2123,
2456 2456,
2789 2789)
I then have a SQL query which pulls additional details tied to those reference number and loads them into a table on another sheet of the Excel workbook.
SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum IN (2123, 2456, 2789)
The results of the query are then loaded to a table on another sheet in the workbook, which we will call the "Output" table:
RefNum LocationID ShipDate
2123 13321 12/3/2019
2456 16654 5/17/2019
2789 19987 8/24/2019
Can Power Query M code pull the values in the RefNum column directly into the WHERE clause of the SQL query, so that the end user does not have to manually adjust queries in the Advanced Editor as the list changes?
Here is the code I attempted based on the resources I have found thus far:
let
Source = Sql.Database("server", "database")
RefNum_Formatted = table.Column(Input, RefNum_Formatted)
Output = Value.NativeQuery( Source,
"SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum = @RefNum_Formatted")
in
Output
When attempting to run the above M code, I receive the following error:
DataSource.Error: Microsoft SQL: Must declare the scalar variable "@RefNum_Formatted".
I do understand that as it's currently written, Power Query expects @RefNum_Formatted to be a declared variable in SQL as opposed to one from an Excel table; however, I cannot figure out how to create a connection to the "Input" table values that can be pulled into the query using M. Can this be done?
Similar questions are asked in the links below but I have not been able to apply any of the answers successfully:
Power Query to Filter a SQL view based on an Excel column list
IN
with a subquery orEXISTS
? – Gordon Linoff