Forum Discussion

c_laurenti's avatar
c_laurenti
Frequent Visitor
3 years ago
Solved

Find flight itinerary from flight legs

A flight with destination from A to B is composed by one or more legs. It has 2 legs if it has to stop to a intermediate destination C, going A -> C -> B. Suppose I have a table of flight legs, with ...
  • AlienSx's avatar
    3 years ago

    custom sorting

    let
        Source = your_table,
        comparer = (x, y) => 
            if x[Date] > y[Date] then 1 else if x[Date] < y[Date] then -1 else 
            if x[Origin] = y[Destination] then 1 else 
            if x[Destination] = y[Origin] then - 1 else 0,
        g = Table.Group(Source, "Id", {{"legs", each Table.Sort(_, comparer)}}),
        iti = Table.AddColumn(g, "itinerary", each Text.Combine([legs][Origin] & {List.Last([legs][Destination])}, "/")),
        legs = Table.TransformColumns(iti, {"legs", Table.RowCount})
    in
        legs