Power Query – Filter with wildcards

Power Query - Wildcard Filter Featured Image

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.

Demonstration of Wildcard Filter in Power Query

Download the example file
I recommend you download the files which support this post, as you’ll be able to work along with examples.  This is the best way to learn.  You’ll be able to see the solutions in action, plus the file will be useful for future reference.  The support files are available for FREE to newsletter subscribers.

Click below to subscribe and gain access to the subscriber area.  You will also receive:

  • My favorite tips and tricks direct to your inbox
  • Exclusive content (which is only available to subscribers)
  • FREE tools and downloads

Download Icon

If you’re already a subscriber, click here to log-in to the subscriber downloads area.

The filename for this post is 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.

Data Scenario with search box

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:

  1. Load the data table into Power Query
  2. Remove all the columns except the column to be filtered (in our example, Product is the only column remaining)
  3. Remove all duplicates from the column by clicking Home > Remove Rows > Duplicates
  4. Call the query Unique List
    Unique List in Power Query Preview Window
  5. 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.

Unique List loaded into Excel

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:

  • AVERAGEIF
  • AVERAGEIFS
  • COUNTIF
  • COUNTIFS
  • SUMIF
  • SUMIFS
  • VLOOKUP
  • HLOOKUP
  • MATCH
  • SEARCH
  • XLOOKUP

Enter the following formula against the first item in the unique list.

=IFERROR(MATCH($E$2,[@Product],0),0)

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.

Unique list with wildcard MATCH added

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.

Asterisk (*)

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”.

Tilde (~)

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.

Wildcard examples

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.

Wildcard - Example 1

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.

Wildcard - Example 3

Wildcards example 3:

Below we can see that only products starting with Brown and ending with Loaf will match.

Wildcard - Example 2

Download the 100 Excel Macros ebook

100 Excel Macros Book

  • Contains 100 Excel VBA macros
  • Learn VBA by following along with the example codes
  • Apply to your macros, automate Excel, save time.

Download the ebook today!

Click the button below to subscribe, you’ll gain access to the subscriber area in which you can download the ebook.

Download Icon (on mid-green background)

Load unique list and filter

The next step is to load the unique list with the added formula into Power Query

  1. Load the Unique List table into Power Query
  2. Filter on the Keep column to only include the 1’s (i.e., the matched items)
  3. Call the query Filter List
  4. Load the query as a connection only.

Filter List in Power Query

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.

  1. Load the original data table into Power Query.
  2. Click Home > Merge Queries
  3. In the Merge dialog box, select the two matching columns, and use a right-outer merge.
    Merge Dialog Box - Power Query Filter Wildcards
    Click OK to execute the transformation.
  4. Remove the Filter List column.
  5. Call the query Data
    Power Query - Filtered by Wildcard Preview Window
  6. 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.

Demonstration of Wildcard Filter in Power Query

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.

Conclusion

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.

Don’t forget:

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:

  1. Read other blogs, or watch YouTube videos on the same topic.  You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office.  It’s amazing what things other people know.
  3. 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.
  4. Use Excel Rescue, who are my consultancy partner.   They help by providing solutions to smaller Excel problems.

What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

2 thoughts on “Power Query – Filter with wildcards

  1. Mitchell Allen says:

    Hi Mark!
    I liked your method of wildcard filtering, using Power Query. I gave myself the challenge to create a method that uses neither Power Query nor VBA. Guess what? I found something!

    The pieces you will need:
    * A column in your table named “Match”
    * A defined range named “SearchString”
    * A formula based on Excel’s MATCH function
    * A slicer based on the table column “Match”

    Create the Match column.
    Select the cell where you will type the wildcard and define that cell as SearchString. (Alt+I,N,D brings up the name dialog).
    Put this formula into one of the cells in the new Match column:

    =IFERROR(IF(MATCH(SearchString,[@Product],0)=1,TRUE,FALSE),FALSE)

    Make sure it copies down all cells in the column.
    At this point you have the same functionality as your Power Query solution. The Match column displays TRUE or FALSE, depending on what’s in SearchString.
    To make the search a bit more interactive, you can create a slicer. Select “Match” as the only column. This gives you a two-button slicer, labeled FALSE and TRUE.
    If you type a search with no matches, the TRUE will disappear immediately. Otherwise, click on TRUE to see the matches. Click on FALSE if you need to see products that DON’T match. 🙂

    Finally, when you add data to the table, just click the TRUE button again to update the results.

    Cheers,

    Mitch

Leave a Reply

Your email address will not be published. Required fields are marked *