Forum Discussion

speedramps's avatar
speedramps
Super User
9 months ago
Solved

Power Query Merge with LIKE % type logic

A question for Power Query experts.     No AI replies please.  Thank you.   I know that Power Query Merge has a Fuzzy logic function, but is there any method to do table joins using LIKE % wild...
  • danextian's avatar
    9 months ago

    Hi speedramps 

    Try the following:

    let
        _likecode = Lookup[Likecode],
        _fruit = Lookup[Fruit],
        _code = [Code],
    
        // helper function that evaluates a single LIKE pattern
        MatchesLike = (pattern as text, code as text) as logical =>
            let
                startsWithPct = Text.StartsWith(pattern, "%"),
                endsWithPct = Text.EndsWith(pattern, "%"),
                inner =
                    if startsWithPct and endsWithPct then
                        Text.Middle(pattern, 1, Text.Length(pattern) - 2)
                    else if startsWithPct then
                        Text.End(pattern, Text.Length(pattern) - 1)
                    else if endsWithPct then
                        Text.Start(pattern, Text.Length(pattern) - 1)
                    else
                        pattern,
    
                result =
                    if startsWithPct and endsWithPct then
                        Text.Contains(code, inner)
                    else if startsWithPct then
                        Text.EndsWith(code, inner)
                    else if endsWithPct then
                        Text.StartsWith(code, inner)
                    else
                        code = inner
            in
                result,
    
        // find all matching positions (0-based)
        _returnedPositions =
            List.Select(
                List.Positions(_likecode),
                each MatchesLike(_likecode{_}, _code)
            ),
    
        // get corresponding fruits for those positions
        _fruitMatches =
            List.Transform(_returnedPositions, each _fruit{_})
    in
        _fruitMatches

     

     

    Note: I used AI to figure this out.