Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Compare two Columns with placehoder ? or *

Hi all,   I will compare two Columns. The ID Column can have placeholders (like ? or *)  If there is some ? there must be one digit. If there is some * there can be zero, one or more digits. At...
  • AlienSx's avatar
    2 years ago

    Anonymous 

    let
        your_table = Table.FromRecords(
            {[ID = "1NA*W*", Search = "1NA2345W78"],
            [ID = "1NA????W*", Search = "1NA1234W78"],
            [ID = "1NA????W*", Search = "1NA12W78"]}
        ),
        wildcards = [#"?" = "0123456789", #"*" = "0123456789"],
        comparer = (x as list, y as list) => 
            [x_first = List.First(x),
            a = Record.FieldOrDefault(wildcards, x_first, x_first),
            star = x_first = "*",
            b = List.First(y),
            par = Text.Contains(a, b),
            skip_y = par, 
            skip_x = not List.AllTrue({star, par}),
            next = 
                if List.IsEmpty(y) 
                    then "Yes" 
                    else 
                        if List.IsEmpty(x) or List.AllTrue({not par, not star}) 
                            then "No" 
                            else @comparer(
                                List.Skip(x, Number.From(skip_x)),
                                List.Skip(y, Number.From(skip_y))
                            )][next],
        mapping = Table.AddColumn(your_table, "FindMapping", each comparer(Text.ToList([ID]), Text.ToList([Search])))
    in
        mapping