KNOWLEDGE BASE

How to Filter Data By Wildcard Search With Multiple Keywords


Published: 09 Apr 2021
Last Modified Date: 27 Apr 2023

Question

How to create a single wildcard search filter that allows searching by multiple keywords that are not consecutive.

For example, a search on "quartz sphinx" should return "Sphinx of black quartz, judge my vow."

Environment

  • Tableau Desktop

Answer

The default wildcard search behavior in Tableau is an exact phrase match, which can allow for mismatched text at the beginning of the phrase, the end, or both. The wildcard search allows users to more quickly filter views to specific values when the dimension contains too many values to scroll through. Using parameters and calculations, it is possible to create a view with different search behavior that may be more helpful in other use cases, such as searching for potential duplicates.

The attached example workbook (downloadable from the right-hand pane of this article) uses the sample data set Superstore to demonstrate the following directions:
  1. Right-click Product Name in the left-hand data pane and select Create Parameter...
  2. In the Create Parameter dialog, do the following and close the dialog:
    1. Name the parameter. In this example, the parameter is named "Search Product Name"
    2. For Allowable Values, select All
  3. Right-click Search Product Name in the data pane and check Show Parameter.
  4. Select Analysis > Create Calculated Field.
  5. In the Calculated Field dialog box that opens, do the following, and then click OK:
    1. Name the calculated field. In this example, the calculated field is named "Product Name Search Filter"
    2. In the formula field, create a calculation that will result in the search behavior desired. Below are two examples.

      AND - the filter results must include all search terms where search terms are separated by a space
      [Search Product Name]=""
      //show everything if the parameter is blank, aka everything is TRUE
      OR (
          CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",1)))
          //if [Product Name] contains the first typed search term
          AND ( 
              SPLIT([Search Product Name]," ",2) = ""
              //if there isn't a second search term, just return TRUE
              OR CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",2)))
              //check that the second search term is also in [Product Name]
          )
          AND (SPLIT([Search Product Name]," ",3)="" OR CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",3))))
          AND (SPLIT([Search Product Name]," ",4)="" OR CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",4))))
          AND (SPLIT([Search Product Name]," ",5)="" OR CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",5))))
      )

      OR - the filter result must include at least one of the search terms where search terms are separated by a space
      [Search Product Name]=""
      OR CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",1)))
      OR (
          NOT SPLIT([Search Product Name]," ",2)=""
          AND CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",2)))
      ) //If the 2nd search term is NOT blank and is contained in [Product Name] then return TRUE
      
      OR (NOT SPLIT([Search Product Name]," ",3)="" AND CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",3))))
      OR (NOT SPLIT([Search Product Name]," ",4)="" AND CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",4))))
      OR (NOT SPLIT([Search Product Name]," ",5)="" AND CONTAINS(LOWER([Product Name]),LOWER(SPLIT([Search Product Name]," ",5))))
      
  6. Drag Product Name Search Filter to the Filters shelf.
  7. In the Filter dialog, check True and click OK.
  8. Type search terms in the Search Product Name parameter and hit Enter to filter the view.

Additional Information

  • The calculation must include a condition for the number of search terms a user may enter. In the above example, if the user enters more than 5 search terms then additional search terms are ignored
  • For some data sources where Regex expressions are supported, a Regex calculation can automatically detect the number of search terms; However Regex is more complex and not supported by Tableau Technical Support. Some Regex Calculations that may work are:
    • [Regex String (OR)]   "(" + REGEXP_REPLACE([Search Product Name], ',', '|') + ")"
    • [Regex String (AND)]   "(?=.*" + REPLACE([Search Product Name], ',', ")(?=.*") + ").*"
    • [Regex Filter]   REGEXP_MATCH([Product Name], [Regex String...])
Did this article resolve the issue?