Forum Discussion

Adn1's avatar
Adn1
New Member
3 years ago
Solved

Calculate the highest date value for people with the same ID.

Hi All,

 

I want to create a new column in my table.

This column must calculate the highest date from the "END_DATE" column for lines of peoples that have the same "ID".


This is an example of how the table should work:

NAME       ID      END_DATE           New Column (Highest Date)
Ana 124/01/2023 24/01/2023
Gustav 101/01/2023 24/01/2023
Louis 3510/02/2022 10/02/2022
Gabe 3510/01/2022 10/02/2022



  • Hi Adn1 ,

     

    How about this:

     

    Before:

     

    After:

     

    Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough). The idea is to join the query from after the grouping (calculating MaxDate per ID) with the original query before the grouping:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsxLVNJRUjAEEkYm+gaG+kYGRsZKsTrRSu6lxSWJZTBJoAyypE9+aWYxSM7YFEgaGugbGIEkjSA6E5NSUeQMoXKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NAME = _t, ID = _t, END_DATE = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"NAME", type text}, {"ID", Int64.Type}, {"END_DATE", type date}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"MaxDate", each List.Max([END_DATE]), type nullable date}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ID"}, #"Grouped Rows", {"ID"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"MaxDate"}, {"MaxDate"})
    in
        #"Expanded Grouped Rows"

     

    Let me know if this helps 🙂

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/

2 Replies

  • tackytechtom's avatar
    tackytechtom
    Icon for Most Valuable Professional rankMost Valuable Professional

    Hi Adn1 ,

     

    How about this:

     

    Before:

     

    After:

     

    Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough). The idea is to join the query from after the grouping (calculating MaxDate per ID) with the original query before the grouping:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsxLVNJRUjAEEkYm+gaG+kYGRsZKsTrRSu6lxSWJZTBJoAyypE9+aWYxSM7YFEgaGugbGIEkjSA6E5NSUeQMoXKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NAME = _t, ID = _t, END_DATE = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"NAME", type text}, {"ID", Int64.Type}, {"END_DATE", type date}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"MaxDate", each List.Max([END_DATE]), type nullable date}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ID"}, #"Grouped Rows", {"ID"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"MaxDate"}, {"MaxDate"})
    in
        #"Expanded Grouped Rows"

     

    Let me know if this helps 🙂

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/