You may be quite familiar with parameter queries with their use in SQL or Microsoft Query. However Power Query parameters have key differences:
-
Parameters can be used in any query step. In addition to functioning as a data filter, parameters can be used to specify such things as a file path or a server name.
-
Parameters don’t prompt for input. Instead, you can quickly change their value using Power Query. You can even store and retrieve the values from cells in Excel.
-
Parameters are saved in a simple parameter query, but are separate from the data queries they are used in. Once created, you can add a parameter to queries as needed.
Note If you want the other way to create parameter queries, see Create a parameter query in Microsoft Query.
You can use a parameter to automatically change a value in a query and avoid editing the query each time to change the value. You just change the parameter value. Once you create a parameter, it is saved in a special parameter query which you can conveniently change directly from Excel.
-
Select Data > Get Data > Other Sources > Launch Power Query Editor.
-
In the Power Query Editor, select Home > Manage Parameters > New Parameters.
-
In the Manage Parameter dialog box, select New.
-
Set the following as needed:
Name
This should reflect the parameter's function, but keep it as short as possible.
Description
This can contain any details that will help people correctly use the parameter.
Required
Do one of the following:
Any Value You can enter any value of any data type in the parameter query. List of Values You can limit the values to a specific list by entering them in the small grid. You must also select a Default Value and a Current Value below. Query Select a list query, which resembles a List structured column separated by commas and enclosed in braces. For example, an Issues status field could have three values: {"New", "Ongoing", "Closed"}. You must create the list query beforehand by opening the Advanced Editor (select Home > Advanced Editor), removing the code template, entering the list of values in the query list format, and then selecting Done. Once you finish creating the parameter, the list query is displayed in your parameter values.Type
This specifies the data type of the parameter.
Suggested Values
If desired, add a list of values or specify a query to provide suggestions for input.
Default Value
This only appears if Suggested Values is set to List of values, and specifies which list item is the default. In this case, you must choose a default.
Current Value
Depending on where you use the parameter, if this is blank the query might return no results. If Required is selected, Current Value cannot be empty.
-
To create the parameter, select OK.
Here's a way to manage changes to data source locations and help prevent refresh errors. For example, assuming a similar schema and data source, create a parameter to easily change a data source and help prevent data refresh errors. Sometimes the server, database, folder, file name, or location changes. Perhaps a database manager occasionally swaps out a server, a monthly drop of CSV files goes into a different folder, or you need to easily switch between a development/test/production environment.
Step 1: Create a parameter query
In the following example, you have several CSV files that you import using the import folder operation (Select Data > Get Data > From Files > From Folder) from folder C:\DataFilesCSV1. But sometimes a different folder is occasionally used as a location to drop the files, C:\DataFilesCSV2. You can use a parameter in a query as a substitute value for the different folder.
-
Select Home > Manage Parameters > New Parameter.
-
Enter the following information in the Manage Parameter dialog box:
Name
CSVFileDrop
Description
Alternate file drop location
Required
Yes
Type
Text
Suggested Values
Any value
Current Value
C:\DataFilesCSV1
-
Select OK.
Step 2: Add the parameter to the data query
-
To set the folder name as a parameter, in Query Settings, under Query Steps, select Source, and then select Edit Settings.
-
Make sure the File path option is set to Parameter, and then select the parameter you just created from the drop-down list.
-
Select OK.
Step 3: Update the parameter value
The folder location just changed, so now you can simply update the parameter query.
-
Select Data > Connections & Queries > Queries tab, right click the parameter query, and then select Edit.
-
Enter the new location in the Current Value box, such as C:\DataFilesCSV2.
-
Select Home > Close & Load.
-
To confirm your results, add new data to the data source, and then refresh the data query with the updated parameter (Select Data > Refresh All).
Sometimes you want an easy way to change the filter of a query to obtain different results without either editing the query or making slightly different copies of the same query. In this example, we change a date to conveniently change a data filter.
-
To open a query, locate one previously loaded from the Power Query Editor, select a cell in the data, and then select Query > Edit. For more information see Create, load, or edit a query in Excel.
-
Select the filter arrow in any column header to filter your data, and then select a filter command, such as Date/Time Filters > After. The Filter Rows dialog box appears.
-
Select the button to the left of the Value box, and then do one of the following:
-
To use an existing parameter, select Parameter, and then select the parameter you want from the list that appears on the right.
-
To use a new parameter, select New Parameter, and then create a parameter.
-
-
Enter the new date in the Current Value box, and then select Home > Close & Load.
-
To confirm your results, add new data to the data source, and then refresh the data query with the updated parameter (Select Data > Refresh All). For example, change the filter value to a different date to see new results.
-
Enter the new date in the Current Value box.
-
Select Home > Close & Load.
-
To confirm your results, add new data to the data source, and then refresh the data query with the updated parameter (Select Data > Refresh All).
In this example, the value in the query parameter is read from a cell in your workbook. You don't have to change the parameter query, you just update the cell value. For example, you want to filter a column by the first letter, but easily change the value to any letter from A to Z.
-
On the worksheet in a workbook where the query you want to filter is loaded, create an Excel table with two cells: a header and a value.
MyFilter
G
-
Select a cell in the Excel table, then select Data > Get Data > From Table/Range. The Power Query Editor appears.
-
In the Name box of the Query Settings pane on the right, change the query name to be more meaningful, such as FilterCellValue.
-
To pass the value in the table, and not the table itself, right-click the value in Data Preview, and then select Drill Down.
Notice that the formula changed to = #"Changed Type"{0}[MyFilter]
When you use the Excel Table as a filter in step 10, Power Query references the Table value as the filter condition. A direct reference to the Excel Table would cause an error.
-
Select Home > Close & Load > Close & Load To. You now have a query parameter named "FilterCellValue" that you use in step 12.
-
In the Import Data dialog box, select Only Create Connection, and then select OK.
-
Open the query you want to filter with the value in the FilterCellValue table, one previously loaded from the Power Query Editor, by selecting a cell in the data, and then selecting Query > Edit. For more information see Create, load, or edit a query in Excel.
-
Select the filter arrow in any column header to filter your data, and then select a filter command, such as Text Filters > Begins With. The Filter Rows dialog box appears.
-
Enter any value in the Value box, such as "G" and then select OK. In this case, the value is a temporary placeholder for the value in the FilterCellValue table which you enter in the next step.
-
Select the arrow on the right side of the formula bar to display the whole formula. Here's an example of a filter condition in a formula:
= Table.SelectRows(#"Changed Type", each Text.StartsWith([Name], "G")) -
Select the value of the filter. In the formula, select "G".
-
Using M Intellisense, enter the first few letter of the FilterCellValue table you created, and then select it from the list that appears.
-
Select Home > Close > Close & Load.
Result
Your query now uses the value in the Excel Table that you created to filter the query results. To use a new value, edit the cell contents in the original Excel table in step 1, change "G" to "V", and then refresh the query.
You can control whether parameter queries are allowed or not allowed.
-
In the Power Query Editor, select File > Options and Settings > Query Options > Power Query Editor.
-
In the pane on the left, under GLOBAL, select Power Query Editor.
-
In the pane on the right, under Parameters, select or clear Always allow parameterization in data source and transformation dialogs.