Forum Discussion

maalsan's avatar
maalsan
Advocate I
4 years ago
Solved

Find multiple substring DAX CONTAINSSTRING

Hello everyone,

 

I need to find if the substrings "house", "home", "dewelling" appear in a long string column. Using CONTAINSSTRING I am able to find each word at a time- house= CONTAINSSTRING(table1[column1], "house"). Is there a way to look for more than one substring? Perhaps something like house= CONTAINSSTRING(table1[column1], "house" or "home" or "dewelling").

 

Thank you for any help!

  • maalsan's avatar
    maalsan
    4 years ago

    That's weird. It still gives me an error message. 

    I found the solution to be simply:

    house= CONTAINSSTRING(table1[column1], "house" ) || CONTAINSSTRING(table1[column1], "home" ) || CONTAINSSTRING(table1[column1], "dewelling" )

     Without the need of IF statements. 

     

    Thanks!

14 Replies

  • diegovelez's avatar
    diegovelez
    Frequent Visitor

    For anyone on this forum, Copilot gave me this solution which works quite well:

    ContainsAnyValue =
    VAR StringList = {"Value1", "Value2", "Value3"} -- Step 1: Define the list of string values
    RETURN
    IF (
    COUNTROWS (
    FILTER (
    StringList,
    CONTAINSSTRING ( [YourColumnName], [Value] )
    )
    ) > 0,
    TRUE(),
    FALSE()
    )

  • Hi maalsan,

     

    Assuming that you have table called "String" where Description is your column header; You have another table called Keyword, that contains a column header Keyword.

     

    You can load both the tables to PowerBI and then for the String Table, you can create a calculated column as below:

     

     

    Is this providing a solution for your query? If "yes" kindly confirm.

     

    Request other experienced users to add value to my suggestion.

     

    Best Regards,

     

    C.S.N. Raja

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      This is a great solution that works fine, however, I struggle to wrap my head around what exactly going on in here.

      The part where I'm stuck is the FIRSTNONBLANK () function, and how it manages to "loop through" the column of keywords. When I isolate the expression FIRSTNONBLANK(Table[Column],1) it just returns the first line of the column, but in this solution it obviously checks against all values in the column

      If you could explain in plain English, that would be hugely appreciated,
      thanks

    • Alexandernoclue's avatar
      Alexandernoclue
      Frequent Visitor

      Hi Raja, im fascinated by your solution. Nevertheless I cant recreate it for myself.
      The Search Process works fine, but my Result (when found) gives always ALL values from the Keyword list back.

      The rootcause seems to be the "ResultwhenTrue" of the If statement.


      EDIT: My Colleague found the problem. You cant have a active relation between the two tables. -.- 

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    maalsan Try:

    house = 
      IF(
        CONTAINSSTRING(table1[column1], "house") ||
        CONTAINSSTRING(table1[column1], "home") ||
        CONTAINSSTRING(table1[column1], "dwelling"),
        TRUE(),
        FALSE()
      )
    • maalsan's avatar
      maalsan
      Advocate I

      Thanks for the reply. However, it seems that containsstring function only allowes for 2 arguments max. I got the following error message: "Too many arguments were passed to the CONTAINSSTRING function. The maximum argument count for the function is 2".