Forum Discussion
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
Community 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 - laganlee
Helper II
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..
- AntrikshSharma
Community Champion
laganlee Yup, I am always amazed by how many functions there are to solve almost anything, if you want to get better in using M then follow this page on LinkedIn https://www.linkedin.com/in/excelbi/recent-activity/shares/ The owner posts PQ challenges every day and they are good for learning from others as well as practicising M.