Forum Discussion

matout's avatar
matout
Helper I
4 years ago
Solved

Extracting text from string based on match from another text

Hello,   I am doing a data scraping project for a client, the data will come from various sources (website) with multiple naming standards.   the idea is that I want to standarise the data to be ...
  • PhilipTreacy's avatar
    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
        ProductList

     

    A 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