cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper I
Helper 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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Champion
Community Champion

Re: Counting number of words in a list found in a phrase

@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

 


______________


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

View solution in original post

2 REPLIES 2
Highlighted
Community Champion
Community Champion

Re: Counting number of words in a list found in a phrase

@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

 


______________


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

View solution in original post

Highlighted
Helper I
Helper I

Re: Counting number of words in a list found in a phrase

Thanks a lot Allison, what a speed!

Helpful resources

Announcements
August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Upcoming Events

Upcoming Events

Wondering what events you could join or have an event to promote yourself? Check out our Upcoming Events.

Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

We are thrilled to announce we will begin running a monthly webinar series named Power BI Dev Camp.

Top Solution Authors
Top Kudoed Authors