Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

How can I have an M Query Text.Contains function search for multiple possible words in a field?

I would like to have a Text.Contains that searches Column A for "Example" OR "Examples" OR "Examples2". I am sure it is staring me in the face but I cannot figure out how to do it.

  • Hi Anonymous ,

    Please try this:

    if Text.Contains([Column1],"Example") = true then [Column1] else null

    The function of Text.Contains will search out all the data which contains the key word. So you just need to write Text.Contains one time.

     

    If you want to filter data, you could try this:

    = Table.SelectRows(#"Changed Type", each Text.Contains([Column1], "Example"))

    Best Regards,

    Xue Ding

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

5 Replies

  • If you are trying to perform this on a table, you would like to use something like:

     

    #"Filtered Rows" = Table.SelectRows(table, each( Text.Contains([ColumnA], "Executive") or Text.Contains([ColumnA], "General") ))

     

     

    Regards

    • Anonymous's avatar
      Anonymous
      Not applicable

      JasonTX, thanks for the suggestion. I have been doing it that way, but I am hoping I can cut out the repetitious "Text.Contains".

      • v-xuding-msft's avatar
        v-xuding-msft
        Community Support

        Hi Anonymous ,

        Please try this:

        if Text.Contains([Column1],"Example") = true then [Column1] else null

        The function of Text.Contains will search out all the data which contains the key word. So you just need to write Text.Contains one time.

         

        If you want to filter data, you could try this:

        = Table.SelectRows(#"Changed Type", each Text.Contains([Column1], "Example"))

        Best Regards,

        Xue Ding

        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Hi,

     

    Put these Example(s) words you want to find in strings in lists and then use the List.MatchesAny function.

    Like in the following example:

    Define a table with your keywords... (Example,Examples,sample...)

    Transform_Keywords_list_in_list =
    Table.TransformColumns
    (
    Source
    ,{
    "Keywords list"
    ,each Text.Split(_, ",")
    }
    ),

    Main_Data =
    Table.AddColumn
    (
    Main_Data_Table
    ,"Returned_Text"
    ,(MAIN)=>
    let
    Buffered_Keywords_list_in_list = Table.Buffer(Keywords_list_in_list ),

    FilteredTable = Table.SelectRows
    (
    Buffered_Keywords_list_in_list
    ,(KEYWORDS)=>
    List.MatchesAny(KEYWORDS[Keywords list], each Text.Contains(MAIN[String_to_test], _, Comparer.OrdinalIgnoreCase))
    ),
    selectValue = Table.First(FilteredTable)[Value_to_return] // or you can return directly the table... selectValue = FilteredTable
    in
    selectValue, type text  // or if you need the table returned  selectValue, type table
    ),