Forum Discussion
Extracting text from string based on match from another text
- 4 years ago
Hi matout
Download sample PBIX file with the following code and examples
You can do what you ask by firstly setting up a list of the products you are looking for. This is stored in a list (the result of a query)
let ProductList = {"Vacuum", "Microwave", "Photo Printer", "Earbuds", "Sound Bar"} in ProductListA separate query then uses that list to check the Product Name column for those words/descriptions
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY1BDoIwEEWvMmFNtNSWA4DKQkmINbAAFrUtQkCaFKobD2/FkLiYxZ/3Z15ZejkX1j7gDXkcSxqSJpSUNC5H/D6oafJqv/TSThj94k/l9inDuyMlycXNAv8+YJRSwva0+N1DVQFGCBVL75xApkWvZshaPWvITDfOyqzsqkcF26Iz6qsF5pDScODmZuUE0cBFv1aZtqN0AgPsFOx9wBskWh+CxVR/AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product Name" = _t]), #"Added Custom" = Table.AddColumn(Source, "Product", each List.Accumulate ( ProductList, "", (state, current) => if Text.Contains([Product Name], current, Comparer.OrdinalIgnoreCase) then state & " " & current else state )) in #"Added Custom"Giving this result
You can add as many products as you like to the ProductList and the query will search for them.
This blog post explains the workings of the code
Create a List of Matching Words When Searching Text in Power Query • My Online Training Hub
Regards
Phil
Hi PhilipTreacy, this is a very elegant and helpful solution. Will resolve many of my issues, entailing a lot matching of text strings.
Had a question though : what is the logic behind concatening the state and the current in the code? should it not just return the current value from the list? also if the Text.contains, returns false, meaning the string from {ProductList} is not found in [ProductName] column, then should it not return null, instead of state?
Thanks in advance