In Power Query, there is one key feature that is missing, the ability to filter using wildcards. Some have got around this with complex M code, while others suggest using the Text filter functions, such as Text.Contains, to create a similar effect. However, what I’m about to show you is an easier way to filter using wildcards. It’s not a perfect solution but might match the requirements for your scenario.
The animation below shows the outcome we will achieve in this post. In the search box we can enter wildcards, then click refresh. The table updates to only include those items which match the search box, including any wildcard characters.
Download the example file
I recommend you download the example file for this post. Then you’ll be able to work along with examples and see the solution in action, plus the file will be useful for future reference.
Download the file: 0026 Power Query – Wildcard filter.zip
Setting up the scenario
The screenshot below shows the data we are working with. It contains the period end date, product and units sold.
In our example, we want to filter the data where the Product matches the criteria in the Search box. So, for example, Brown*Sliced should match only products which begin with Brown, end with Sliced, and has any number of characters in between.
Create a unique list
First, using Power Query, we will create a unique list of products and load it into Excel:
- Load the data table into Power Query
- Remove all the columns except the column to be filtered (in our example, Product is the only column remaining)
- Remove all duplicates from the column by clicking Home > Remove Rows > Duplicates
- Call the query Unique List
- Close and load the unique list into an Excel Table.
If you’re following along with the example file, the spreadsheet should look like the following.
Wildcard match with formulas
Next, we will use standard Excel functions to test if each value in our unique list matches the search box. We need to use a function which can handle wildcards, there are many available, but I’ve decided to use MATCH.
NOTE: Common formulas which handle wildcards are:
Enter the following formula against the first item in the unique list.
This uses the MATCH function to test if any items match the value in the search box. If the result is 1, then the value matches the, otherwise the result will be 0.
The table will expand and copy the formula down into each row.
Give the column the header name Keep.
In the screenshot above, the search box can contain an asterisk, which is a wild>card character.
There are three wildcard characters:
Question mark (?)
A ? can take the place of any single character. If we had the text “Ex?el” then the ? could be any character (just like the blank Scrabble tile). So, the text could be “Excel”, “Exuel”, “Ex7el”, or any other letter single in the place of ?.
It is also possible to combine multiple question marks (?) together. For example, ??? would represent any three characters, no less, no more.
A * represents any number of characters, or even no characters at all. If we had the text “Ex*” then the matches could be “Excel”, “Exam”, “Explain”, or any other word starting with “Ex”, including just “Ex”.
This is not strictly a wildcard character. It is used before a ? or a * to indicate that the ? or * should not be used as a wildcard. For example, “Ex~*” would only match “Ex*”, as the * is no longer treated as a wildcard, but as a character in its own right.
Let’s look at a few examples of using wildcards:
Wildcards example 1:
In this first example, we see any item ending with the text Sliced matches.
Wildcards example 2:
In Example 2, there are 10 question marks, each one representing a single letter. Therefore, only values with exactly 10 characters match.
Wildcards example 3:
Below we can see that only products starting with Brown and ending with Loaf will match.
Load unique list and filter
The next step is to load the unique list with the added formula into Power Query
- Load the Unique List table into Power Query
- Filter on the Keep column to only include the 1’s (i.e., the matched items)
- Call the query Filter List
- Load the query as a connection only.
The screenshot above shows the result where the search box included Brown*Loaf.
Load data and merge
Now let’s bring this together to get the wildcard filter to work.
- Load the original data table into Power Query.
- Click Home > Merge Queries
- In the Merge dialog box, select the two matching columns, and use a right-outer merge.
Click OK to execute the transformation.
- Remove the Filter List column.
- Call the query Data
- Close and load the query into Excel.
Search & refresh
Everything is in place, all that’s left is to test it. Change the value in the search box, including wildcards, if you wish, then refresh the query. The data table will now show only the data which matches the search box.
While the example includes all the tables on a single worksheet, this isn’t a requirement. The source and final output tables can be used in the same way as any Power Query source and output (e.g. CSVs as an input, or Data Model as an output). However, it is necessary for the Unique List, and the formula must exist on the face of a worksheet.
Adding new data to the table
There is one big issue with this method. When adding new data to the source table we need to refresh twice. The first refresh updates the unique list, which then triggers the calculation of the filter calculation. The second refresh triggers the updating of the final output table. As an additional step, we could use a macro to force the events to occur in the right order and get back to a single click.
While not perfect, if you use Power Query, the flexibility created by this method provides an additional level of functionality that we can’t find in the native tool.. Where data is added infrequently, the double refresh is unlikely to be a significant issue.
Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.
If you’ve found this post useful, or if you have a better approach, then please leave a comment below.
Do you need help adapting this to your needs?
I’m guessing the examples in this post didn’t exactly meet your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.
But, if you’re still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise. List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don’t go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts: