Power Query: Passing (Reference to) Excel Parameters
28 February 2018
Welcome to our Power Query blog. This week, I take a look at how to pass Excel data as a parameter in Power Query.
I am going to look at a simple scenario where I use a parameter in Power Query which is populated from a cell in my Excel workbook.
Below, I have a spreadsheet with charge data pertaining to items in my fictitious inventory:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image1.png/0b8bbcaa9a79e632d4e56deb93bc1ac6.jpg)
My plan is to create a query from ‘Charges’ data, and pass a parameter from the ‘Items’ sheet.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image2.png/2cebd229e1cbfa6f056d612fcd6a475a.jpg)
I am not claiming that this is the most efficient approach for this particular scenario, but it will serve to show the method!
I create the parameter first, so that it is ready to add to my main ‘Charges’ query. Since my cell is already in a table of items data, trying to create a new query from the ‘Data’ tab by choosing ‘From Table’ in the ‘Get and Transform’ section will assume I want the whole table. I can circumvent this issue by using the method I used in Power Query: Returning to Referencing Ranges. If I give my prospective parameter a range name, then I can locate it easily in my workbook. I name my cell ‘Parameter_Range’ and create a new blank workbook from the ‘Other sources’ option of the ‘New Query’ dropdown, viz.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image3.png/bb69be1b66ed0087b5b8ce015da499ba.jpg)
In my new blank query, I enter the following M function:
Excel.CurrentWorkbook(){[Name="Parameter_Range"]}[Content]{0}[Column1]
which will extract the value from the cell. I rename my new query ‘Item_Parameter’ and save it as a ‘Connection Only’ query.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image4.png/687d2e6113ec062d801c7e18d43e52a7.jpg)
Now I need to create a query on ‘Charges’ which will select charges that match a particular item. I will do this using the GUI options available. I create a new query ‘From Table’ whilst in my ‘Charges’ sheet:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image5.png/ddcbd0dce136c03defa3d945dde97a9a.jpg)
I can then choose to filter on Description:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image6.png/dffe5a9133323a82d586701d15043dc1.jpg)
It doesn’t matter which description I pick at this stage – I am simply getting Power Query to create the step for me.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image7.png/9cd4d96a001a892b7dc693abb1f056b9.jpg)
Now I have the step, instead of picking those charges that are pertaining to ’10 x 4 metre marquee’ I want to use my parameter instead. If I expand the ‘Queries’ pane to the left of the screen, I can make sure I have the correct name.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image8.png/ccc2575db82ad75e6681d2426b0ccbe6.jpg)
I then select the tick icon (left of the formula bar) to change the step to use my parameter.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image9.png/3c6f80ed6a3735d4db0e02bb81cf7df8.jpg)
The correct data is shown. I can update the parameter (by changing the cell or editing the named range) in my Excel worksheet and refresh the query to change the data that is selected. I try this by changing ‘Item_Parameter’ to point at the next item description.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image10.png/7aa3b894ca0bda18cdaa1db028cd044a.jpg)
Before I refresh my query, the original data appears:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image11.png/e4ed37828bf8a527b31263d7115a2c76.jpg)
Once I refresh my query, the new parameter value is used.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2018/power-query/02-feb/pq-65-image12.png/ca6a231966329ce39e8bee02dd784333.jpg)
Want to read more about Power Query? A complete list of all our Power Query blogs can be found here. Come back next time for more ways to use Power Query!