Power Query check if string contains strings from a list











up vote
2
down vote

favorite












Is there a way to check a text field to see if it contains any of the strings from a list?



Example Strings to Check:



The raisin is green
The pear is red
The apple is yellow


List Example to Validate Against



red
blue
green


The result would be



either:



green
red
null


or:



TRUE
TRUE
FALSE









share|improve this question




























    up vote
    2
    down vote

    favorite












    Is there a way to check a text field to see if it contains any of the strings from a list?



    Example Strings to Check:



    The raisin is green
    The pear is red
    The apple is yellow


    List Example to Validate Against



    red
    blue
    green


    The result would be



    either:



    green
    red
    null


    or:



    TRUE
    TRUE
    FALSE









    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Is there a way to check a text field to see if it contains any of the strings from a list?



      Example Strings to Check:



      The raisin is green
      The pear is red
      The apple is yellow


      List Example to Validate Against



      red
      blue
      green


      The result would be



      either:



      green
      red
      null


      or:



      TRUE
      TRUE
      FALSE









      share|improve this question















      Is there a way to check a text field to see if it contains any of the strings from a list?



      Example Strings to Check:



      The raisin is green
      The pear is red
      The apple is yellow


      List Example to Validate Against



      red
      blue
      green


      The result would be



      either:



      green
      red
      null


      or:



      TRUE
      TRUE
      FALSE






      list contains powerquery m






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 23:02









      Alexis Olson

      11.6k21633




      11.6k21633










      asked Nov 7 at 20:49









      Always Learning

      112




      112
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          Daniel has a decent solution, but it won't work if the example strings aren't space-separated. For example, The brick is reddish would detect red as a substring.



          You can create a custom column with this formula instead:



          (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _)))


          This takes the list Words = {"red","blue","green"} and checks if each of the colors in the list is contained in the [Texts] column for that row. If any are, then it returns TRUE otherwise FALSE.



          The whole query looks like this:



          let
          TextList = {"The raisin is green","The pear is red","The apple is yellow"},
          Texts = Table.FromList(TextList, Splitter.SplitByNothing(), {"Texts"}, null, ExtraValues.Error),
          Words = {"red","blue","green"},
          #"Added Custom" = Table.AddColumn(Texts, "Check", (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _))))
          in
          #"Added Custom"





          share|improve this answer




























            up vote
            0
            down vote













            This will make the trick, it's PowerQuery ("M") code:



            let
            Texts = {"The raisin is green","The pear is red","The apple is yellow"},
            Words = {"red","blue","green"},
            TextsLists = List.Transform(Texts, each Text.Split(_," ")),
            Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)
            in
            Output


            There are two lists: the sentences (Texts) and the words to check (Words). The first thing to do is to convert the sentences in lists of words splitting the strings using " " as the delimiter.



            TextsLists = List.Transform(Texts, each Text.Split(_," ")),


            Then you "cross" the new lists with the list of Words. The result are lists of elements (strings) that appears in both lists (TextLists and Words). Now you count these new lists and check if the result is bigger than cero.



            Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)


            Output is a new list {True, True, False).



            Alternatively, you can change the Output line by this one:



            Output = List.Transform(TextsLists, each List.Intersect({_,Words}){0}?)


            This will return a list of the first coincidence or null if there's no coincidence. In the example: {"green", "red", "null"}



            Hope this helps you.






            share|improve this answer























            • If you're transforming to a list, you can use the List.ContainsAny function.
              – Alexis Olson
              Nov 7 at 23:11











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197587%2fpower-query-check-if-string-contains-strings-from-a-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Daniel has a decent solution, but it won't work if the example strings aren't space-separated. For example, The brick is reddish would detect red as a substring.



            You can create a custom column with this formula instead:



            (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _)))


            This takes the list Words = {"red","blue","green"} and checks if each of the colors in the list is contained in the [Texts] column for that row. If any are, then it returns TRUE otherwise FALSE.



            The whole query looks like this:



            let
            TextList = {"The raisin is green","The pear is red","The apple is yellow"},
            Texts = Table.FromList(TextList, Splitter.SplitByNothing(), {"Texts"}, null, ExtraValues.Error),
            Words = {"red","blue","green"},
            #"Added Custom" = Table.AddColumn(Texts, "Check", (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _))))
            in
            #"Added Custom"





            share|improve this answer

























              up vote
              1
              down vote













              Daniel has a decent solution, but it won't work if the example strings aren't space-separated. For example, The brick is reddish would detect red as a substring.



              You can create a custom column with this formula instead:



              (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _)))


              This takes the list Words = {"red","blue","green"} and checks if each of the colors in the list is contained in the [Texts] column for that row. If any are, then it returns TRUE otherwise FALSE.



              The whole query looks like this:



              let
              TextList = {"The raisin is green","The pear is red","The apple is yellow"},
              Texts = Table.FromList(TextList, Splitter.SplitByNothing(), {"Texts"}, null, ExtraValues.Error),
              Words = {"red","blue","green"},
              #"Added Custom" = Table.AddColumn(Texts, "Check", (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _))))
              in
              #"Added Custom"





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                Daniel has a decent solution, but it won't work if the example strings aren't space-separated. For example, The brick is reddish would detect red as a substring.



                You can create a custom column with this formula instead:



                (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _)))


                This takes the list Words = {"red","blue","green"} and checks if each of the colors in the list is contained in the [Texts] column for that row. If any are, then it returns TRUE otherwise FALSE.



                The whole query looks like this:



                let
                TextList = {"The raisin is green","The pear is red","The apple is yellow"},
                Texts = Table.FromList(TextList, Splitter.SplitByNothing(), {"Texts"}, null, ExtraValues.Error),
                Words = {"red","blue","green"},
                #"Added Custom" = Table.AddColumn(Texts, "Check", (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _))))
                in
                #"Added Custom"





                share|improve this answer












                Daniel has a decent solution, but it won't work if the example strings aren't space-separated. For example, The brick is reddish would detect red as a substring.



                You can create a custom column with this formula instead:



                (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _)))


                This takes the list Words = {"red","blue","green"} and checks if each of the colors in the list is contained in the [Texts] column for that row. If any are, then it returns TRUE otherwise FALSE.



                The whole query looks like this:



                let
                TextList = {"The raisin is green","The pear is red","The apple is yellow"},
                Texts = Table.FromList(TextList, Splitter.SplitByNothing(), {"Texts"}, null, ExtraValues.Error),
                Words = {"red","blue","green"},
                #"Added Custom" = Table.AddColumn(Texts, "Check", (C) => List.AnyTrue(List.Transform(Words, each Text.Contains(C[Texts], _))))
                in
                #"Added Custom"






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 2:33









                Alexis Olson

                11.6k21633




                11.6k21633
























                    up vote
                    0
                    down vote













                    This will make the trick, it's PowerQuery ("M") code:



                    let
                    Texts = {"The raisin is green","The pear is red","The apple is yellow"},
                    Words = {"red","blue","green"},
                    TextsLists = List.Transform(Texts, each Text.Split(_," ")),
                    Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)
                    in
                    Output


                    There are two lists: the sentences (Texts) and the words to check (Words). The first thing to do is to convert the sentences in lists of words splitting the strings using " " as the delimiter.



                    TextsLists = List.Transform(Texts, each Text.Split(_," ")),


                    Then you "cross" the new lists with the list of Words. The result are lists of elements (strings) that appears in both lists (TextLists and Words). Now you count these new lists and check if the result is bigger than cero.



                    Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)


                    Output is a new list {True, True, False).



                    Alternatively, you can change the Output line by this one:



                    Output = List.Transform(TextsLists, each List.Intersect({_,Words}){0}?)


                    This will return a list of the first coincidence or null if there's no coincidence. In the example: {"green", "red", "null"}



                    Hope this helps you.






                    share|improve this answer























                    • If you're transforming to a list, you can use the List.ContainsAny function.
                      – Alexis Olson
                      Nov 7 at 23:11















                    up vote
                    0
                    down vote













                    This will make the trick, it's PowerQuery ("M") code:



                    let
                    Texts = {"The raisin is green","The pear is red","The apple is yellow"},
                    Words = {"red","blue","green"},
                    TextsLists = List.Transform(Texts, each Text.Split(_," ")),
                    Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)
                    in
                    Output


                    There are two lists: the sentences (Texts) and the words to check (Words). The first thing to do is to convert the sentences in lists of words splitting the strings using " " as the delimiter.



                    TextsLists = List.Transform(Texts, each Text.Split(_," ")),


                    Then you "cross" the new lists with the list of Words. The result are lists of elements (strings) that appears in both lists (TextLists and Words). Now you count these new lists and check if the result is bigger than cero.



                    Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)


                    Output is a new list {True, True, False).



                    Alternatively, you can change the Output line by this one:



                    Output = List.Transform(TextsLists, each List.Intersect({_,Words}){0}?)


                    This will return a list of the first coincidence or null if there's no coincidence. In the example: {"green", "red", "null"}



                    Hope this helps you.






                    share|improve this answer























                    • If you're transforming to a list, you can use the List.ContainsAny function.
                      – Alexis Olson
                      Nov 7 at 23:11













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    This will make the trick, it's PowerQuery ("M") code:



                    let
                    Texts = {"The raisin is green","The pear is red","The apple is yellow"},
                    Words = {"red","blue","green"},
                    TextsLists = List.Transform(Texts, each Text.Split(_," ")),
                    Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)
                    in
                    Output


                    There are two lists: the sentences (Texts) and the words to check (Words). The first thing to do is to convert the sentences in lists of words splitting the strings using " " as the delimiter.



                    TextsLists = List.Transform(Texts, each Text.Split(_," ")),


                    Then you "cross" the new lists with the list of Words. The result are lists of elements (strings) that appears in both lists (TextLists and Words). Now you count these new lists and check if the result is bigger than cero.



                    Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)


                    Output is a new list {True, True, False).



                    Alternatively, you can change the Output line by this one:



                    Output = List.Transform(TextsLists, each List.Intersect({_,Words}){0}?)


                    This will return a list of the first coincidence or null if there's no coincidence. In the example: {"green", "red", "null"}



                    Hope this helps you.






                    share|improve this answer














                    This will make the trick, it's PowerQuery ("M") code:



                    let
                    Texts = {"The raisin is green","The pear is red","The apple is yellow"},
                    Words = {"red","blue","green"},
                    TextsLists = List.Transform(Texts, each Text.Split(_," ")),
                    Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)
                    in
                    Output


                    There are two lists: the sentences (Texts) and the words to check (Words). The first thing to do is to convert the sentences in lists of words splitting the strings using " " as the delimiter.



                    TextsLists = List.Transform(Texts, each Text.Split(_," ")),


                    Then you "cross" the new lists with the list of Words. The result are lists of elements (strings) that appears in both lists (TextLists and Words). Now you count these new lists and check if the result is bigger than cero.



                    Output = List.Transform(TextsLists, each List.Count(List.Intersect({_,Words}))>0)


                    Output is a new list {True, True, False).



                    Alternatively, you can change the Output line by this one:



                    Output = List.Transform(TextsLists, each List.Intersect({_,Words}){0}?)


                    This will return a list of the first coincidence or null if there's no coincidence. In the example: {"green", "red", "null"}



                    Hope this helps you.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 13 at 10:55

























                    answered Nov 7 at 22:02









                    Daniel Herce

                    213




                    213












                    • If you're transforming to a list, you can use the List.ContainsAny function.
                      – Alexis Olson
                      Nov 7 at 23:11


















                    • If you're transforming to a list, you can use the List.ContainsAny function.
                      – Alexis Olson
                      Nov 7 at 23:11
















                    If you're transforming to a list, you can use the List.ContainsAny function.
                    – Alexis Olson
                    Nov 7 at 23:11




                    If you're transforming to a list, you can use the List.ContainsAny function.
                    – Alexis Olson
                    Nov 7 at 23:11


















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197587%2fpower-query-check-if-string-contains-strings-from-a-list%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings