You can create variables in Power Query by using the "Blank Query" option to define a value and then referencing that query name within other steps or queries. This achieves a single point of control for your data transformations, allowing you to change a file path, a date range, or a tax rate in one place rather than hunting through multiple steps in several different queries. To get started, you simply need a dataset loaded into the Power Query Editor in Excel or Power BI.
What You'll Need
- Microsoft Excel or Power BI Desktop.
- The Power Query Editor open (Data > Get Data > Launch Power Query Editor).
- A basic understanding of how to create a query.
Step-by-Step
- Create a Blank Query: In the Power Query Editor, go to the Home tab. Click on New Source and select Blank Query. This creates a fresh, empty query that you can use as a container for your variable.
- Define the Variable Value: In the formula bar at the top (if you do not see it, go to the View tab and check the Formula Bar box), type the value you want to store. For a text variable, like a folder path, wrap the text in double quotes. For example:
"C:\\Users\\Documents\\Reports\\". For a number, simply type the number, such as0.15for a tax rate. Press Enter. - Rename the Query: In the Queries pane on the left, right-click the new query (likely named Query1) and select Rename. Give it a clear, descriptive name that describes the value it holds, such as
FolderPathorTaxRate. This name is exactly how you will call the variable in other queries. - Reference the Variable in Another Query: Open the query where you want to use this variable. Locate the step in the Applied Steps pane where the variable is needed. Click the gear icon next to the step or click directly into the formula bar.
- Replace Hard-Coded Values: Delete the hard-coded value (the text in quotes or the number) and type the name of your variable query exactly as you named it in Step 3. For example, if your step was
= Folder.Files("C:\\Users\\Documents\\Reports\\"), change it to= Folder.Files(FolderPath). - Verify the Connection: Press Enter. Power Query will now look at the
FolderPathquery to determine which directory to access. If the data loads correctly, the variable is active. - Update the Variable: To change the value for all dependent queries, simply click on the variable query in the left pane, change the value in the formula bar, and press Enter. All queries referencing that variable will update automatically upon the next refresh.
Common Mistakes
One of the most frequent errors is a case-sensitivity mismatch. Power Query (M language) is strictly case-sensitive. If you name your variable TaxRate but attempt to call it using taxrate or Taxrate, Power Query will return an "Expression.Error: The name 'taxrate' wasn't recognized" message. Always ensure the capitalization matches exactly.
Another common mistake is forgetting the double quotes when defining a text variable in a blank query. If you type C:\Reports without quotes, Power Query thinks you are trying to call another function or variable named C, which does not exist, resulting in an error. Text must always be enclosed in double quotes.
Users often mistakenly try to create variables inside a single step of a query without understanding the let...in block. While you can create local variables within a query using let, these are not accessible to other queries. To create a global variable that works across your entire workbook or report, you must use the separate Blank Query method described above.
Finally, some users forget to disable the "Load" option for variable queries. By default, Power Query may try to load your variable (like a single text string) into a table in your Excel sheet or Power BI model. Since a single value doesn't make a useful table, this creates unnecessary clutter.
Tips
To keep your workspace organized, create a Group for your variables. Right-click in the Queries pane and select New Group. Name it "Parameters" or "Variables." Drag all your blank query variables into this group so they are separated from your actual data transformation queries.
To prevent variable queries from loading into your data model, right-click the variable query in the Queries pane and uncheck "Enable Load." This keeps the variable active for the Power Query engine to use but prevents it from appearing as a table in your final report or spreadsheet.
If you have a set of variables that change frequently based on user input, consider creating a small Excel table in your workbook with two columns: "Parameter" and "Value." You can then import this table into Power Query and create a custom function to drill down into the specific value you need. This allows non-technical users to change variables in an Excel cell without ever opening the Power Query Editor.
When dealing with file paths, use a variable for the root folder and then use the Text.Combine function or the ampersand & operator to append specific file names. For example: FolderPath & "Sales_2023.csv". This makes your queries incredibly flexible when moving projects between different computers or servers.
Troubleshooting
If your query returns an error after you've inserted a variable, the first step is to check for typos. Verify that the variable name in the formula bar matches the query name in the left pane exactly, including uppercase and lowercase letters. If the names match, check the data type. If your variable is a text string but the function expects a number, the query will fail.
If the variable seems to be ignored or is returning an old value, ensure you have clicked "Close & Apply" (in Power BI) or "Close & Load" (in Excel). Changes made to variables within the editor are not pushed to the final output until the query is refreshed or the editor is closed.
If you encounter a "Circular Reference" error, it means you have accidentally created a loop. This happens if Query A references Variable B, and Variable B is somehow configured to reference Query A. Check the dependencies of your queries to ensure the flow of information is one-way: Variable → Query.
When a file path variable fails, check for trailing backslashes. A common issue is having a variable "C:\Reports" and then adding "Data.csv" to it, resulting in "C:\ReportsData.csv". Ensure your variable ends with a backslash ("C:\Reports\") or that you add one during the concatenation process.
Frequently Asked Questions
Can I use a cell value from Excel as a Power Query variable?
Yes, by creating a named range for that cell and then importing that named range as a query. You can then drill down into the value to treat it as a scalar variable.
Is there a difference between a variable and a parameter?
In Power BI, "Parameters" are a built-in feature (Manage Parameters) that provides a user interface for changing values. The "Blank Query" method is a manual way to achieve the same result in both Excel and Power BI.
How many variables can I have in one Power Query file?
There is no hard limit on the number of variables you can create. However, for the sake of performance and organization, it is best to group related variables or use a parameter table.
Will variables work if I share the file with someone else?
Yes, but if the variable is a file path (e.g., C:\Users\YourName\...), the other person will need to update that variable to match their own local folder path for the query to run.
Can a variable be a list or a record?
Yes. Instead of a single value, you can define a list using curly brackets, such as {"January", "February", "March"}, and then use that list to filter columns or rows in other queries.