Power Query Variables 3 Ways

Mynda Treacy

August 4, 2022

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

Subscribe YouTube



Download Workbook

Enter your email address below to download the sample workbook.

By submitting your email address you agree that we can email you our Excel newsletter.

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):

Power query variables 1

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):

Power query variables 2

I’ve loaded both of these tables to Power Query. In the BonusRate table I simply right-click > Drill Down on the rate cell:

Power query variables 3

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:

Power query variables 4

In the QuerySalaries I can add a custom column:

Power query variables 5

That uses the SheetBonusRate variable to calculate the bonuses:

Power query variables 6

And my QuerySalaries table now looks like this:

Power query variables 7

I’ll Close & Load to a table in the Excel file:

Power query variables 8

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):

Power query variables 9

And just like that, the table is updated with the new bonus amounts:

Power query variables 10


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.

Power query variables 11

To add a variable in the Power Query editor, go to the Home tab > Advanced Editor:

Power query variables 12

After the ‘let’ enter the variable name* and the variable value, followed by a comma:

Power query variables 13

*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:

Power query variables 14

I can use the variable in a calculated column by referencing the name:

Power query variables 15

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):

Power query variables 16

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:

Power query variables 17

Then select the Sales column > Transform tab > Statistics > Sum:

Power query variables 18

This returns a single value, i.e. your TotalSales variable. Rename the query accordingly:

Power query variables 19

Then back in the QueryProdSales query add a custom column that calculates the percentage of total sales:

Power query variables 20

Which results in:

Power query variables 21

As you add to or replace the data in your source table (TblProdSales) and refresh the query, it dynamically updates.

14 thoughts on “Power Query Variables 3 Ways”

  1. 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!

    Reply
  2. 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 ?

    Reply
    • 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

      Reply
  3. 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?

    Reply
    • 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.

      Reply
  4. 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?

    Reply
  5. 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.

    Reply
  6. 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.)

    Reply

Leave a Comment

Current ye@r *