Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Chris1300
Helper II
Helper II

Search for multiple keywords across table

I have 2 independent tables, not related in powerBI:

 

Keyword List
apple
bird
red
tree
John
bike

 

Sentence
John has a red bike.

The tree is green.

The bird eats apple.

 

I would like to create calculated columns next to "Sentence" table, with following columns: # of Keywords found, Keyword1, Keyword2, Keyword3, Keyword4. So the final table looks like this:

 

Sentence# keywords foundKeyword1Keyword2Keyword3Keyword4
John has a red bike.3Johnredbike 
The tree is green.1tree   
The bird eats apple.2birdapple  

 

I only need to find 4 keywords. What formula can I use in DAX or power query to create these additional columns? Which one would be fastest, I do have many many rows. I think power query would be.

 

Thanks!

1 ACCEPTED SOLUTION

DAX is not created for addressing this although it's powerful enough to cover it.

CNENFRNL_0-1665903589515.png

 

For fun only, a showcase of powerful Excel worksheet formula,

CNENFRNL_1-1665904156164.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

7 REPLIES 7
CNENFRNL
Community Champion
Community Champion

let
    Keyword = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSiwoyElVitWJVkrKLEoBM4pSIXRJUSpExis/Iw+qJBsoEgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Keyword List" = _t]),
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPISCxWSFQoSk1RSMrMTtVTitWJVgrJSFUoKUpNVcgsVkgH0nkI4aTMohSF1MQSoKaCghyQ+lgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Sentence = _t]),
    Matches = let kw = Keyword[Keyword List] in Table.AddColumn(Source, "match", each let pos = List.PositionOf(kw, [Sentence], 2, (x,y)=>Text.Contains(y,x)) in {List.Count(pos)} & List.Transform(pos, each kw{_}))
in
    Matches

CNENFRNL_0-1665854535010.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Thanks for the solution in Power Query, but its really slow for my overall tables. Is this possible to do in DAX? I want to compare.

DAX is not created for addressing this although it's powerful enough to cover it.

CNENFRNL_0-1665903589515.png

 

For fun only, a showcase of powerful Excel worksheet formula,

CNENFRNL_1-1665904156164.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

@CNENFRNL Cool. I didn't know how to use that 4th argument of List.PostitionOf.

 

Personally, I prefer not to use positional functions (except for first/last sort of thing) given how often I make indexing errors, so I'd probably do it more like this:

let
    KeywordList = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSiwoyElVitWJVkrKLEoBM4pSIXRJUSpExis/Iw+qJBsoEgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Keyword = _t]),
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TYs7DoAgEAWv8kJtPIi1HaGAsAp+WLKg8fhup9UkM+9ZayZOBck3eAhFhLzTaNxgzZwIXYiQG1Zl+XTIEkG+66nW47df+MF2nbWBbxJ0VZFX7e4F", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Sentence = _t]),
    MatchList = Table.AddColumn(Source, "Matches", (r) => List.Select(KeywordList[Keyword], each Text.Contains(r[Sentence], _))),
    KeywordColumns =
        let MaxMatches = List.Max(List.Transform(MatchList[Matches], each List.Count(_)))
        in {"# keywords found"} & List.Transform({1..MaxMatches}, each "Keyword" & Number.ToText(_)),
    ToRecords = Table.TransformColumns(MatchList, {{"Matches", each let m = List.Count(_) in Record.FromList({m} & _, List.FirstN(KeywordColumns, m + 1)), type record}}),
    ExpandRecords = Table.ExpandRecordColumn(ToRecords, "Matches", KeywordColumns, KeywordColumns)
in
    ExpandRecords
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks for the solution in Power Query, but its really slow for my overall tables. Is this possible to do in DAX? I want to compare.

AbbasG
Memorable Member
Memorable Member

@Chris1300 I think you can achieve this by splitting the column by space as below. 

 

Steps:
1. Go to power query
2. Duplicate the sentence column
3. Split a column by a custom delimiter (Space)
4. Rename the column

 

AbbasG_0-1665806195639.png

I hope it helps you to achieve the desired output.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.