Forum Discussion

laganlee's avatar
laganlee
Icon for Helper II rankHelper II
3 years ago
Solved

Select earliest and latest dates between two columns that have the same ID in another column.

Hi.

For each ID

1) I need to find the earliest ArrivalDateTime and use the ArrivalCountry for that record.

2) Find the latest DepartureDateTime

3) Put this new record in a new table

 

Flight table:

ID            DepartureCountry   ArrivalCountry        DepartureDateTime         ArrivalDateTime

744                  UK                        Portugal                 17th Jan 2022  09:00     17th Jan 2022  12:00

744                 Portugal                  Denmark               20th Jan 2022  09:00     20th Jan 2022  14:00

744              Denmark                   UK                         20th Jan 2022  16:00      20th Jan 2022  20:00   

853                 etc.....

 

New table:

ID              ArrivalCountry        DepartureDateTime          ArrivalDateTime

744                Portugal              20th Jan 2022  16:00       17th Jan 2022  12:00

844...........etc

 

Many thanks for looking!   ğŸ¤—

  • laganlee You can use this, paste this complete code in the Advanced Editor:

    let
        Source = 
            Table.FromRows (
                Json.Document (
                    Binary.Decompress (
                        Binary.FromText (
                            "i45WMjcxUdJRCvUGEgH5RSWl6Yk5QKaRgZGRroGhrqE5KidWB6YDSbFLal5uYlE2kkojA1QOQhtCLdhKnBpiAQ==",
                            BinaryEncoding.Base64
                        ),
                        Compression.Deflate
                    )
                ),
                let
                    _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
                in
                    type table [
                        ID = _t,
                        DepartureCountry = _t,
                        ArrivalCountry = _t,
                        DepartureDateTime = _t,
                        ArrivalDateTime = _t
                    ]
            ),
        ChangedType = 
            Table.TransformColumnTypes (
                Source,
                {
                    { "ID", Int64.Type },
                    { "DepartureCountry", type text },
                    { "ArrivalCountry", type text },
                    { "DepartureDateTime", type datetime },
                    { "ArrivalDateTime", type datetime }
                }
            ),
        GroupedRows = 
            Table.Group (
                ChangedType,
                { "ID" },
                {
                    {
                        "Result",
                        ( CurrentGroup ) =>
                            let
                                FirstArrival = Table.Min ( CurrentGroup, "ArrivalDateTime" ),
                                FirstArrivalDate = FirstArrival[ArrivalDateTime],
                                FirstArrivalCountry = FirstArrival[ArrivalCountry],
                                LastDepartureDate = Table.Max ( CurrentGroup, "DepartureDateTime" )[DepartureDateTime],
                                Result = Table.FromRecords (
                                    {
                                        [
                                            ArrivalCountry    = FirstArrivalCountry,
                                            DepartureDateTime = LastDepartureDate,
                                            ArrivalDateTime   = FirstArrivalDate
                                        ]
                                    }
                                )
                            in
                                Result,
                        type table [
                            ArrivalCountry = text,
                            DepartureDateTime = datetime,
                            ArrivalDateTime = datetime
                        ]
                    }
                }
            ),
        ExpandedResult = 
            Table.ExpandTableColumn (
                GroupedRows,
                "Result",
                { "ArrivalCountry", "DepartureDateTime", "ArrivalDateTime" },
                { "ArrivalCountry", "DepartureDateTime", "ArrivalDateTime" }
            )
    in
        ExpandedResult

3 Replies

  • AntrikshSharma's avatar
    AntrikshSharma
    Icon for Community Champion rankCommunity Champion

    laganlee You can use this, paste this complete code in the Advanced Editor:

    let
        Source = 
            Table.FromRows (
                Json.Document (
                    Binary.Decompress (
                        Binary.FromText (
                            "i45WMjcxUdJRCvUGEgH5RSWl6Yk5QKaRgZGRroGhrqE5KidWB6YDSbFLal5uYlE2kkojA1QOQhtCLdhKnBpiAQ==",
                            BinaryEncoding.Base64
                        ),
                        Compression.Deflate
                    )
                ),
                let
                    _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
                in
                    type table [
                        ID = _t,
                        DepartureCountry = _t,
                        ArrivalCountry = _t,
                        DepartureDateTime = _t,
                        ArrivalDateTime = _t
                    ]
            ),
        ChangedType = 
            Table.TransformColumnTypes (
                Source,
                {
                    { "ID", Int64.Type },
                    { "DepartureCountry", type text },
                    { "ArrivalCountry", type text },
                    { "DepartureDateTime", type datetime },
                    { "ArrivalDateTime", type datetime }
                }
            ),
        GroupedRows = 
            Table.Group (
                ChangedType,
                { "ID" },
                {
                    {
                        "Result",
                        ( CurrentGroup ) =>
                            let
                                FirstArrival = Table.Min ( CurrentGroup, "ArrivalDateTime" ),
                                FirstArrivalDate = FirstArrival[ArrivalDateTime],
                                FirstArrivalCountry = FirstArrival[ArrivalCountry],
                                LastDepartureDate = Table.Max ( CurrentGroup, "DepartureDateTime" )[DepartureDateTime],
                                Result = Table.FromRecords (
                                    {
                                        [
                                            ArrivalCountry    = FirstArrivalCountry,
                                            DepartureDateTime = LastDepartureDate,
                                            ArrivalDateTime   = FirstArrivalDate
                                        ]
                                    }
                                )
                            in
                                Result,
                        type table [
                            ArrivalCountry = text,
                            DepartureDateTime = datetime,
                            ArrivalDateTime = datetime
                        ]
                    }
                }
            ),
        ExpandedResult = 
            Table.ExpandTableColumn (
                GroupedRows,
                "Result",
                { "ArrivalCountry", "DepartureDateTime", "ArrivalDateTime" },
                { "ArrivalCountry", "DepartureDateTime", "ArrivalDateTime" }
            )
    in
        ExpandedResult
  • That's absolutely brilliant Antriksh!  Saved the day 🙂  

    I'll be learning all about Table functions now; didn't know how powerful M code was..