Power Query variables are a great way to store data that can be used multiple times and can be easily updated in one place. There are a few ways you can create variables.
1. From the worksheet, which means you don’t even need to open the query editor to update the variable. This is great if you’re handing the file over to someone else to maintain.
2. You can declare a variable inside the query editor. This makes it more difficult for users to easily change the variables and keeps the data all in one place.
3. You can derive a variable from another query making it dynamic. As the data in the query changes, the variable automatically updates on refresh based on the latest data.
Watch the Video
Download Workbook
Enter your email address below to download the sample workbook.
Power Query Variables from the Worksheet
We’ll look at Power Query variables stored in the worksheet. Here I have a table of employee salaries (TblSalaries):
I want to calculate the bonus using Power Query and I want the user of this file to be able to adjust the bonus percentage without opening Power Query. I’ve stored the bonus variable in a separate table in the worksheet (BonusRate):
I’ve loaded both of these tables to Power Query. In the BonusRate table I simply right-click > Drill Down on the rate cell:
I’ll rename this query ‘SheetBonusRate’ for the purpose of this tutorial, so we know it comes from the sheet. In practice you’re more likely to simply call it ‘BonusRate’.
In the Query Editor I now have two queries, my QuerySalaries and SheetBonusRate variable:
In the QuerySalaries I can add a custom column:
That uses the SheetBonusRate variable to calculate the bonuses:
And my QuerySalaries table now looks like this:
I’ll Close & Load to a table in the Excel file:
If I want to update the bonus rate, I simply enter a new rate in cell E5 (1) and then right-click the QuerySalaries table and Refresh (2):
And just like that, the table is updated with the new bonus amounts:
Power Query Variables in the Query Editor
If you prefer to store your variables inside the Query Editor, you can simply add them via the Advanced Editor. In the example below I have sales data (TblSales) and I need to add a column to calculate the sales tax.
To add a variable in the Power Query editor, go to the Home tab > Advanced Editor:
After the ‘let’ enter the variable name* and the variable value, followed by a comma:
*If your variable name has spaces in it, you must declare it with # and surround it in double quotes, e.g.: #"Tax Rate"
If your variable is text, it must also be surrounded by double quotes.
You can now see the variable in the Applied Steps list, and going forward you can edit it in the formula bar:
I can use the variable in a calculated column by referencing the name:
Note: if your variable name has spaces in it, you must reference with the hash and double quotes like so:
= [Sale] * #"Tax Rate"
Power Query Dynamic Variables
Lastly, you can derive a variable from another query making it dynamic. As the data in the query changes, the variable automatically updates on refresh based on the latest data.
In the example below my source data contains sales by product (TblProdSales):
I want to add a column that calculates the percentage of total sales and obviously this will change with each month’s new data.
In the Query Editor I need to duplicate QueryProdSales – right-click query name > Duplicate:
Then select the Sales column > Transform tab > Statistics > Sum:
This returns a single value, i.e. your TotalSales variable. Rename the query accordingly:
Then back in the QueryProdSales query add a custom column that calculates the percentage of total sales:
Which results in:
As you add to or replace the data in your source table (TblProdSales) and refresh the query, it dynamically updates.
luqmaan s
This tutorial brilliantly demonstrates the power of Power Query variables, making data manipulation in Excel more flexible and user-friendly. The step-by-step explanation and practical examples are incredibly helpful. Thanks for simplifying complex tasks!
Philip Treacy
Thanks Luqmaan, glad it was useful for you.
fcatcher
Hi, thx for this: very clear.
I was trying to use the cell to filter my table (in my case just came from a table connected trough odbc) wich contains a column req_id
I created the filter query, via the drill down but I Cant figure how to filter the table with the value of query created (req_id = SheetBonusRate).
any Idea how to do this ?
fcatcher
in the end I thought it was easier to directly filter the query on the select to the db and I solved it this way
Source = Odbc.Query(“dsn=matrixpy”, “select * from dl_req #(lf) where req_id='” & MyId & “‘” )
thx again for your post
Mynda Treacy
Thanks for sharing your solution.
Lebenya
Very informative…
I have a question for you though…
How do I go about dynamically changing a table name in an sql script using power query in excel?
Mynda Treacy
Depends what you mean by dynamically and what the new name is based on. If you add a step in Power Query to change the column name, it will make that change every time you refresh the query, if that’s what you want. If it’s something else, then perhaps you can post your question on our Excel forum where you can also upload a sample file and we can help you further.
Zaigham uddin Farooqui
I always found your tutorials very helpful, very well explained and easy to understand. It shows your firm grip on Excel and PQ. Though I have a little use of PQ. However I’ve tried it in my some projects by following the tutorials available at various sites.
I have a request, can you make a tutorial on getting data from folder from many Workbooks having multiple worksheets with common layout but different names using PQ?
Mynda Treacy
Hi Zaigham,
Please see this tutorial: Importing multiple files containing multiple sheets with Power Query.
Mynda
Zaigham uddin Farooqui
Thanks a lot for guiding. May God bless you and your family.
RichardW
Very useful. Thanks.
I’ve always put my worksheet variables for PQ in cells with range names. But that requires knowing the PQ code for referencing a named range. As I can’t remember the code, I have to look it up every time. The worksheet variable method given here (using a table instead of a named range) is simpler.
Mynda Treacy
Glad it’ll be useful to you, Richard 🙂
Jim
A fourth way is to use a named range.
variable = Excel.CurrentWorkbook(){[Name=”NamedRange”]}[Content]{0}[Column1]
will return the value contained in NamedRange. However, this method is susceptible to language/regionalization issues in that “Column1″ is English-centric.
A language-insensitive approach is:
variable = Table.FirstValue(Excel.CurrentWorkbook(){[Name=”NamedRange”]}[Content])
(thanks to Pascal D.)
Mynda Treacy
Thanks for sharing, Jim!