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 SourceI want to replace '03' with a value form cell AD2
Is this possible to do ?
3 Answers
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:
- Just as Rajesh suggested, create a table with proper headers:
- Make sure that your cursor is within the table, on the "Data" tab, click "From Table/Range"
- Right now, you'll be on the power query editor. First make sure that the "Values" column is of "Text" data type. Next, Click the "Parameter Name" column, and Under the "Transform" tab click "Pivot Column". Use the "Value" column as Values. make sure that you expand the "Advanced options" tab and select "Don't Aggregate" as "Aggregate Value Function".
- At this point, you will now have different columns with the parameter name as the column name. Right click the "Parameters" query and click "Reference"
- Right click on the value of your parameter and click "Drilldown"
- You will now have a Query that can be used as a variable from your sheet. go ahead and use it in your ODBC Query:
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
1I 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:
- Choose From Other Sources => Blank Query
- Go to View => Advanced Editor
- Replace all the code with the following:
(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
ValueFinally, 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 SourceReference:
2No, 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 SourceHere 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.