Forum Discussion

MI04's avatar
MI04
New Member
5 years ago
Solved

Searching for Multiple Substrings within a column

Hi everyone, 

 

I'm new to Power BI and I had a query regarding how to go about searching for multiple substrings within a string. I have a table of recipes where the columns are RecipeID which is just a numeric value, and Recipe_Ingredients which is a set of ingredients as shown below: 

 

RecipeIDRecipe_Ingredients

1

Flour, Butter, Sugar
2Sugar, Eggs, Salt
3Honey, Milk, Sugar
4Tea Powder, Sugar, Honey
5Sugar, Flour, Salt

 

I also have table of individual ingredients which I have set up as a Slicer: 

 

Ingredients
Salt
Sugar
Honey

 

What I would like to acheive is that f I select multiple ingredients in the slicer, it displays only those recipes which have ALL the selected ingredients. So for example, if I select Sugar and Honey, all the recipes shows must have BOTH ingredients in them. 

 

These examples are representative and the actual data set would have over 100 ingredients that can be chosen, and no matter how many I choose, the resulting recipes should have all the ingredients as a part of it.

 

Would this be possible? Thanks in advance!

  • Here is one way to do it with no relationship between your two tables.  You can use this measure in the table visual or use it as a visual level filter with a condition of = 1.

     

     

    ShowRecipe =
    VAR vSelIngredients =
        DISTINCT ( Ingredients[Ingredients] )
    VAR vNumSel =
        COUNTROWS ( vSelIngredients )
    VAR vThisRecipe =
        MIN ( Recipes[Recipe_Ingredients] )
    VAR vNumFound =
        COUNTROWS (
            FILTER (
                vSelIngredients,
                SEARCH ( Ingredients[Ingredients], vThisRecipe,, 0 ) > 0
            )
        )
    RETURN
        IF ( vNumFound = vNumSel1 )

     

    Pat

     

3 Replies

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    Here is one way to do it with no relationship between your two tables.  You can use this measure in the table visual or use it as a visual level filter with a condition of = 1.

     

     

    ShowRecipe =
    VAR vSelIngredients =
        DISTINCT ( Ingredients[Ingredients] )
    VAR vNumSel =
        COUNTROWS ( vSelIngredients )
    VAR vThisRecipe =
        MIN ( Recipes[Recipe_Ingredients] )
    VAR vNumFound =
        COUNTROWS (
            FILTER (
                vSelIngredients,
                SEARCH ( Ingredients[Ingredients], vThisRecipe,, 0 ) > 0
            )
        )
    RETURN
        IF ( vNumFound = vNumSel1 )

     

    Pat

     

    • MI04's avatar
      MI04
      New Member

      This worked perfectly, thanks a lot!