Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Convert a String Expression into a Boolean Expression and "solve" it

Dear Friends,   Could you please help me to figure out how to automatically convert a string into a Boolean expression and “solve” it? Here’s an example: There are two tables: one shows options...
  • CNENFRNL's avatar
    3 years ago
    let
        Option = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8skviTdU0lHyLwDRsTqoIsZwESMMNTAREwwRU7iIMVTECEPETCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Lot = _t, Option = _t]),
        #"Grouped Option" = Table.Group(Option, "Lot", {"Opt", each [Option]}),
    
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8skviTdU0lHyLwDSCol5KQogljGYlZdfAuYZKcXqIFSicFCljZCljaDSJgr5RWBjTDGkjOAWmsHljIFyGhDXQPUZaYKVaUAcBhU00UTRgZsD84SxUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Lot = _t, Condition = _t]),
    
        #"Logical Expression" = Table.AddColumn(
            Source,
            "Logical",
            each let opt = #"Grouped Option"{[Lot=[Lot]]}[Opt],
                    cond = List.Select(List.Transform(Text.Split([Condition], " "),each Text.Remove(_ , {"(",")"})), each Text.StartsWith(_, "Opt", Comparer.OrdinalIgnoreCase)),
                    #"T/F" = List.Transform(cond, each Text.From(List.Contains(opt, _))),
                    expr = List.Accumulate({0..List.Count(cond)-1}, [Condition], (s,c) => Text.Replace(s, cond{c}, #"T/F"{c}))
                in if Text.Length([Condition]) = 0 then true else Expression.Evaluate(expr, [true=true, false=false])
        )
    in
        #"Logical Expression"