Forum Discussion

Topaz_Installer's avatar
Topaz_Installer
Regular Visitor
3 years ago
Solved

Nearest match from TABLE1 to Table2

I have two tables and both contains time. I would like match time couln from table1 to time column in the table2. Table1 is fixed and no no need change anything. If I find the same record or match in...
  • m_dekorte's avatar
    3 years ago

    Hi Topaz_Installer,

     

    You can copy this into a new blank query

     

    let
        Tbl1 = Table.TransformColumnTypes(Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjSwMgAhpVgdOMcUiWOILGMIlIkFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Table1 = _t]),{{"Table1", type time}}),
        Tbl2 = Table.TransformColumnTypes(Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjSwMgAhpVgdOMcQmWOGxDFEVmYIlIkFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Table2 = _t]),{{"Table2", type time}}),
        AddCustom = Table.AddColumn(Tbl1, "Approx Match", each List.First( Table.SelectRows( Tbl2, (x)=> x[Table2] >= [Table1])[Table2] ), type time)
    in
        AddCustom

     

     

    Give this a go for an approx match

    Table.AddColumn( Tbl1, "Approx Match", each List.First( Table.SelectRows( Tbl2, (x)=> x[Table2] >= [Table1])[Table2] ), type time)

     

    with this result

     

    Ps. If this helps solve your query please mark this post as Solution, thanks!

  • ThxAlot's avatar
    3 years ago

     

    let
        Table2 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjSwMgAhpVgdOMcMiWOILGMIlIkFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Timestamp = _t]),
        #"Changed Type Table2" = Table.TransformColumnTypes(Table2,{{"Timestamp", type time}}),
        Table1 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjSwMgAhpVgdOMcUiWOILGMIlIkFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Timestamp = _t]),
        #"Changed Type Table1" = Table.TransformColumnTypes(Table1,{{"Timestamp", type time}}),
    
        #"Added Match" =
            let l = #"Changed Type Table2"[Timestamp]
            in Table.AddColumn(
                #"Changed Type Table1",
                "Match",
                each let dev = List.Transform(l, (ts) => Number.Abs(Number.From(ts - [Timestamp]))), pos = List.PositionOf(dev, List.Min(dev))
                    in l{pos}
            )
    in
        #"Added Match"

     

     

    A showcase of powerful Excel worksheet formulas,

     

    =INDEX(Table2[Timestamp];LET(dev;ABS(Table2[Timestamp]-[@Timestamp]);MATCH(MIN(dev);dev;)))