Search code examples
powerbidaxpowerquerym

Power BI join two tables on LIKE clause


How to join two tables in Power BI on LIKE clause? Suppose we have two tables:

+------------------+  +--------+
|    Messy_Name    |  |  Tag   |
+------------------+  +--------+
| red apple        |  | apple  |
| apple very tasty |  | banana |
| good apple green |  +--------+
| yellow banana    |            
| banana split     |            
+------------------+            

And we want to join them in PBI M or DAX (preferably both) imitating this SQL query:

select
  a.Messy_Name
 ,b.Tag
from FactTable a
outer apply
    (
    select top 1
     b.Tag
    from TagList b
    where a.Messy_Name like '%'+b.Tag+'%'
    order by b.Tag
    ) b

So desired results would be:

+------------------+--------+
|    Messy_Name    |  Tag   |
+------------------+--------+
| red apple        | apple  |
| apple very tasty | apple  |
| good apple green | apple  |
| yellow banana    | banana |
| banana split     | banana |
+------------------+--------+

So the query is supposed to return the first Tag from the TagList table where Messy_Name contains the Tag. Eventually, this is one to one relationship.


Solution

  • To do this in Power Query, create a custom column with this formula

    List.Max(
        Table.SelectRows(Tags,
           (T) => Text.Contains([Messy_Name], T[Tag]))[Tag])