I am using a query like below in power query Excel 2016:
let
Source = Odbc.Query("dsn=AS400", "select * from libm61.emleqpm1 where STN1 = '03' ")
in
Source
I want to replace '03' with a value form cell AD2
Is this possible to do ?
I think there's a better and friendlier approach to both other answers, which is both dynamic and doesn't create extra entries other than the parameters themselves:
In Excel:
Then in Power Query:
(ParameterName as text) =>
let
ParamSource = Excel.CurrentWorkbook(){[Name="Parameters"]}[Content],
ParamRow = Table.SelectRows(ParamSource, each ([Parameter] = ParameterName)),
Value=
if Table.IsEmpty(ParamRow)=true
then null
else Record.Field(ParamRow{0},"Value")
in
Value
Finally, click Done and rename the function to fnGetParameter.
From then on you can use it - in this specific case fnGetParameter("STN1")
- anywhere like:
let
Source = Odbc.Query("dsn=AS400", "select * from libm61.emleqpm1 where STN1 = '" & fnGetParameter("STN1") & "'")
in
Source
Reference: https://www.excelguru.ca/blog/2014/11/26/building-a-parameter-table-for-power-query/
"a string with a $parameter inside it"
.
Rajesh S' answer can already satisfy your requirement. However, the weakness on his answer is that your parameter is dependent on its location on the table. I am suggesting a better solution:
let Source = Odbc.Query("dsn=AS400", "select * from libm61.emleqpm1 where STN1 = '"&STN1"' ") in Source
I know my steps looks tedious, but I am very forgetful so I need use descriptive variable names to easily remember what my Power Query Does. You can also do a "Change Type" step after you pivoted the parameters if you want to use cell values for calculations with other queries. Here is my reference
This is now much easier to do with newest version of Excel (O365): you can simplify declare a variable in the PowerQuery request using advanced editor, indicate that the variable value has to be fetched from an Excel table, then refer to the variables in the following steps.
For example : myVar = Excel.CurrentWorkbook(){[Name="MyParameterTable"]}[Content]{0}[MyColumnName]
So using a parameter table having key / value columns, here is how I would make dynamic the directory and file name of the .csv file to be imported
let
directy = Excel.CurrentWorkbook(){[Name="Param"]}[Content]{0}[Value],
newDirectory = if Text.EndsWith(directy,"\") then directy else directy&"\",
filePath = Excel.CurrentWorkbook(){[Name="Param"]}[Content]{1}[Value],
Source = Csv.Document(File.Contents(newDirectory&filePath),[Delimiter=";", Columns=2, Encoding=1252, QuoteStyle=QuoteStyle.None])
in
Source
No, directly you can't use the cell reference. What you need to do is to create a separate Data Range and convert it to Table.
Now convert this Range into Table and assign a proper name like ParaTAB or ParameterTAB, then you command should be like this,
let
Source = Odbc.Query("dsn=AS400", "select * from libm61.emleqpm1 where STN1 = ParaTAB[Value]{2} ")
in
Source
Here Power Query will pick the third value or third row which is ADM, since it counts from 0.
NB: Remember B6 and A2 are cell references.