March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hi all,
I didn't find the text.containsany function in power query, and I don't find a way to make that, do you have any method about it?
Example:
Column name is Name and 3 rows
Name |
Research |
Army |
Hospital |
and what I want is to search the Name column value, if the value include Res or Arm, then true else false.
I don't want to do it using text.contains twice in power query, because in my actually data, the search string has more than 10 values.
So do you have any method to do that?
Thanks.
Aiolos Zhao
Solved! Go to Solution.
Hello,
Try this in Power Query as custom column:
let
SearchString = {"res", "arm"}
in
List.Count(Splitter.SplitTextByAnyDelimiter(SearchString) (Text.Lower([Data]))) >= 1
Replace the contents of {" "} with your actual search strings and [Data] with your actual column data.
Proud to be a Super User!
Hey Overhear,
it's like danextian mentioned, you must use Comparer.OrdinalIgnoreCase instead:
List.AnyTrue(
List.Transform(
{"Res", "Arm"},
(listItem) => Text.Contains(
[Name],
listItem,
Comparer.OrdinalIgnoreCase
)
)
)
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
I love this solution!
@Anonymous for a big table the peformance will take a hit but this is another way
let
Source = Web.BrowserContents("https://community.powerbi.com/t5/Desktop/How-to-do-the-text-containsany-in-power-query/m-p/932611"),
#"Extracted Table From Html" = Html.Table(Source, {{"Column1", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(7) > TR > :nth-child(1), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(7) > * > TR > :nth-child(1)"}}, [RowSelector="DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(7) > TR, DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(7) > * > TR"]),
#"Changed Type" = Table.TransformColumnTypes(#"Extracted Table From Html",{{"Column1", type text}}),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
#"Added Custom1" = Table.AddColumn(#"Promoted Headers", "Keywords", each {"Res","Arm"}),
#"Expanded Keywords" = Table.ExpandListColumn(#"Added Custom1", "Keywords"),
#"Grouped Rows" = Table.Group(#"Expanded Keywords", {"Name"}, {{"ad", each _, type table [Name=text, Keywords=text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let
X=[ad],
Y = Table.AddColumn(X, "Custom", each Text.Contains([Name],[Keywords]))
in Y),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Name", "Keywords", "Custom"}, {"Name", "Keywords", "Custom.1"})
in
#"Expanded Custom"
Hello,
Try this in Power Query as custom column:
let
SearchString = {"res", "arm"}
in
List.Count(Splitter.SplitTextByAnyDelimiter(SearchString) (Text.Lower([Data]))) >= 1
Replace the contents of {" "} with your actual search strings and [Data] with your actual column data.
Proud to be a Super User!
Hello! How can I get this to work if my values have identical first words. E.g. I have{ "blue umbrella" , "blue hat", "blue scarf"} I only want the result to give me blue hat but it seems that the logic is yielding to "true" for all those with blue
@danextian super cool method !!!
Hi @danextian ,
nice code, I think it should be > 1 instead of >=1 at the end of your formula.
My take would be to add a column with this formula:
List.AnyTrue(
List.Transform(
{"Res", "Arm"},
(listItem) => Text.Contains(
[Name],
listItem
)
)
)
Where the optional 3rd argument of the Text.Contains-function could hold "Ordinal.IgnoreCase" if a case insensitive match would be required.
This doesn't require any transformation of the original colum value. But not sure if this actually makes a difference in performance at the end.
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
Thanks for providing this answer, @ImkeF. Would you mind providing an example that includes the Ordinal.IgnoreCase specification?
hi @ImkeF ,
I wanted to List.Select to the code but it slipped my mind. So, in the formula I posted, should have been >1 instead of >=1.
That is also a pretty nice code and I like how the lines are indented. By the way, I think it should be Comparer.OrdinalIgnoreCase 😊
Proud to be a Super User!
Hi @danextian ,
Thanks a lot! That's really cool method.
Could you please help to explain the expression?
It's my first time to use function in a function,
so you create a function using the
Splitter.SplitTextByAnyDelimiter
But you change the parameter to a list
then
(Text.Lower([Data]))
the function works in the above part and returns a list
finally, you count the list
am I right?
Aiolos Zhao
Hi @Anonymous ,
The function splits a text string based on a defined list of delimiters. "Research is split into "" and "earch" and are returned as a list. If none of the delimiters is in the string, the function still returns a list but just a single row containing the original text string. So if List.Count > 1, the cell contains the string being searched.
Proud to be a Super User!
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
133 | |
91 | |
88 | |
64 | |
58 |
User | Count |
---|---|
201 | |
137 | |
107 | |
73 | |
68 |