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
list contains powerquery m
add a comment |
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
list contains powerquery m
add a comment |
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
list contains powerquery m
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
list contains powerquery m
edited Nov 7 at 23:02
Alexis Olson
11.6k21633
11.6k21633
asked Nov 7 at 20:49
Always Learning
112
112
add a comment |
add a comment |
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"
add a comment |
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.
If you're transforming to a list, you can use theList.ContainsAnyfunction.
– Alexis Olson
Nov 7 at 23:11
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
answered Nov 8 at 2:33
Alexis Olson
11.6k21633
11.6k21633
add a comment |
add a comment |
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.
If you're transforming to a list, you can use theList.ContainsAnyfunction.
– Alexis Olson
Nov 7 at 23:11
add a comment |
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.
If you're transforming to a list, you can use theList.ContainsAnyfunction.
– Alexis Olson
Nov 7 at 23:11
add a comment |
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.
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.
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 theList.ContainsAnyfunction.
– Alexis Olson
Nov 7 at 23:11
add a comment |
If you're transforming to a list, you can use theList.ContainsAnyfunction.
– 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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