Search code examples
excelpowerquerym

How to merge 2 tables in PowerQuery using substrings?


I have a sold items table. In this list, in addition to the name of the product, there are other expressions required by law - all on a single line.

In the second table I have the name of the products.

I need to merge the product name from the second table with the items from the first table. However, as the items in the first table are in the middle of a sentence, something like "product" == "%product%" would be needed.

How to do this merge in PowerQuery?

Example tables

+------------------------------+
|Sold items                    |
+------------------------------+
|Yellow bananas - consume soon |
|A box of small apples         |
|Blue and yellow pencil set    |
|A box of oranges              |
+------------------------------+

+---------+
|Products |
+---------+
|Bananas  |
|Apple    |
|Juice    |
|Pencil   |
+---------+

Expected outcome:

+------------------------------+----------+
|Sold items                    | Products |
+------------------------------+----------+
|Yellow bananas - consume soon | Bananas  |
|A box of small apples         | Apple    |
|Blue and yellow pencil set    | Pencil   |
|A box of oranges              | (null)   |
+------------------------------+----------+

Solution

  • Table 1

    enter image description here

    Table 2

    enter image description here

    Result

    enter image description here

    Add a column:

    enter image description here

    Table.SelectRows(Table2,  (x)=> Text.Contains ([Sold items], x[Products], Comparer.OrdinalIgnoreCase) )
    

    Then expand.

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sold items", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table.SelectRows(Table2,  (x)=> Text.Contains ([Sold items], x[Products], Comparer.OrdinalIgnoreCase) )),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Products"}, {"Products"})
    in
        #"Expanded Custom"