Forum Discussion
PowerQuery -How to apply a parameter to all tabs within a spreadsheet
- Jul 12, 2020
One approach to using a list of values as a filter is to create a filter table and use Merge with an inner join to filter the queries.
I created a filter table on a separate sheet in my workbook and put two countries in there:
Now I select the PopulationStatistics query and use Home>Merge Queries and configure it like this:
Because I'm using an inner join, this has the effect of removing rows from the PopulationStatistics table that don't exist in the CountryFilter query.
This will add a column to the right of the PopulationStatistics query called CountryFilter, but we don't need that, so just right-click it and select Remove.
I then repeat the whole merge process for the LandUse query (or whatever other query has country in it), then use Home>Close&Load.
When the Close&Load has finished, you can delete the additional sheet created for the filter query so that you just have one sheet for each of your original queries and one sheet for your filter table and the list of queries will show "Connection only" against the filter:
Now, when you change the rows in the filter table (add/remove/edit), you can refresh all queries to re-filter them.
To demonstrate how to use a parameter in the way you described, I created two queries against two separate lists of countries from Wikipedia:
1. https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population
After filtering away the duplicated column headers, I have this:
2. https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population_density
Similarly:
I have also created a parameter using Home>Parameters>Manage Parameters>New Parameter:
In my example, I want to filter both lists by the same country.
I've configured the New Parameter like this:
Now, I can filter each query on the column that contains the country name.
From the filter drop-down in the PowerQuery Editor, I select Text Filters>Equals, then I configure the Filter Rows dialog to be equal to the value of my parameter:
Note that I have changed the second box on the first row to Parameter and the SelectedCountry parameter was selected automatically. If you have more than one parameter, you may need to select this yourself.
I repeat this step for each query containing a country column, then Close&Load back to the workbook.
Now, when I want to change the parameter, I simply change the value of the parameter once and use Refresh All to re-filter all the queries by the new country.
It should be simple to imagine and implement how this will work for multiple parameter-filters.
Thanks, This is a great Idea. I am trying to create a list of Inventory Number as a parameter. However, it seems I could only choose one inventory number at a time. Does that mean I need to create a parameter for each Inventory Number? Or if there is a way to create a list of Inventory Number at once?
- OwenPriceJul 12, 2020Iron Contributor
One approach to using a list of values as a filter is to create a filter table and use Merge with an inner join to filter the queries.
I created a filter table on a separate sheet in my workbook and put two countries in there:
Now I select the PopulationStatistics query and use Home>Merge Queries and configure it like this:
Because I'm using an inner join, this has the effect of removing rows from the PopulationStatistics table that don't exist in the CountryFilter query.
This will add a column to the right of the PopulationStatistics query called CountryFilter, but we don't need that, so just right-click it and select Remove.
I then repeat the whole merge process for the LandUse query (or whatever other query has country in it), then use Home>Close&Load.
When the Close&Load has finished, you can delete the additional sheet created for the filter query so that you just have one sheet for each of your original queries and one sheet for your filter table and the list of queries will show "Connection only" against the filter:
Now, when you change the rows in the filter table (add/remove/edit), you can refresh all queries to re-filter them.
- user_2020Jul 12, 2020Copper Contributor
Thanks a lot. This works like a charm.
On a separate note, I am little surprised that we cannot create a list as a filter in power query. I thought it'd be easy if we could create a parameter equivalent to SQL like Inventory_number in ('aaaaa','bbbb','cccc')
- SergeiBaklanJul 13, 2020Diamond Contributor
I see nothing complex here as well. If we have such source and table with filter
that is one step to apply the filter and receive the result. Entire script is
let GetFilter = Excel.CurrentWorkbook(){[Name="tblFilter"]}[Content], Source = Excel.CurrentWorkbook(){[Name="tblSource"]}[Content], FilterTable = Table.SelectRows(Source, each List.Contains(GetFilter[A],[B])) in FilterTable