Forum Discussion

kkarol's avatar
kkarol
Frequent Visitor
1 year ago
Solved

comparing datetime and choosing the nearest one

Hi, I am looking for a solution to find a nearest datetime in one table after a specific datetime stamp in another. So I have two tables: Table1 ID Start End IX34572 2024-11-05 12:37:48 ...
  • ronrsnfld's avatar
    1 year ago

    Given your data, the following should work:

    let
    
    //Note references to Table_1 and Table_2
    //You can either put them in sepearate queries, or set them directly in this query.
        Source = Table_1,
    
        #"New End" = Table.ReplaceValue(
            Source,
            each [Start],
            each [ID],
            (x,y,z)as nullable datetime=>if x <> null then x
                else List.Min(List.Select(Table.SelectRows(Table_2, each [ID]=z)[End], (li)=>li>y)),
            {"End"})
    in
        #"New End"