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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
stchln
Resolver I
Resolver I

Counting number of words in a list found in a phrase

Hello

 

I am trying to count number of times one of the words in a list is found in a phrase

Unfortunately, the M-code I tried to make do not get the correct result

 

Capture0.PNG

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8jZU0lHKz0tVKCnPB7KKM/JLc1IU0jLzUhSMFDLzFEBS+UVg2VgdoHIjoCJMpYYgpXA1xiA1GUWpqWiqDJRiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Words = _t, Phrase = _t]),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Words", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Words"),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"ID", type text}, {"Words", type text}, {"Phrase", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Words"}, {{"AllData", each _, type table [ID=text, Words=text, Phrase=text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "WordsList", each [AllData][Words]),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "PhraseAsList", each [AllData][Phrase]),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Counting", each fNwordsInPhrase(#"Added Custom1"[PhraseAsList],#"Added Custom1"[WordsList]))
in
    #"Added Custom2"

 

 

 

where the function fNwordsInPhrase is defined as

 

 

= (List1 as list, List2 as list) =>
let
    MyPhrase = List1,
    MyWords = List2,
    MyResult = List.Count(List.Difference(MyPhrase, MyWords))
in
    MyResult

 

 

 

And what I get is Counting is always equal to 3

Capture.PNG

 

I cannot find my error in function or in the list of operations before the function is called to transform strings into two lists WordsList and PhraseAsList. Also, there is maybe simpler way for getting same result in DAX

 

Thanks for your help!

 

2 REPLIES 2
AllisonKennedy
Super User
Super User

@stchln  How much volume of data do you have? One problem here is with the List.Difference function, which will always give you the number of rows in that first list, and another problem is that you haven't turned the phrase into a list. 

 

This will work, but might not be the most efficient: 

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8jZU0lHKz0tVKCnPB7KKM/JLc1IU0jLzUhSMFDLzFEBS+UVg2VgdoHIjoCJMpYYgpXA1xiA1GUWpqWiqDJRiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Words = _t, Phrase = _t]),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Words", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Words"),
#"Split Column by Delimiter1" = Table.ExpandListColumn(Table.TransformColumns(#"Split Column by Delimiter", {{"Phrase", Splitter.SplitTextByDelimiter(" ", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Phrase"),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"ID", type text}, {"Words", type text}, {"Phrase", type text}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type", "Contains", each if [Words] = [Phrase] then 1 else 0),
#"Grouped Rows1" = Table.Group(#"Added Conditional Column", {"ID"}, {{"Count", each List.Sum([Contains]), type number}, {"AllData", each _, type table [ID=text, Words=text, Phrase=text]}})
in
#"Grouped Rows1"

 

Has this post solved your problem? Please mark it as a solution so that others can find it quickly and to let the community know your problem has been solved. 

 

If you found this post helpful, please give Kudos.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Thanks a lot Allison, what a speed!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.