Forum Discussion

dolevh's avatar
dolevh
Helper II
4 years ago
Solved

M code with max date

Hi, 

 

I have a Customers table: 

 

DateYearMonthRecordIDCustom
1/1/20222022011231/20/2022
1/3/20222022011231/20/2022
1/9/20222022011231/20/2022
1/14/20222022011231/20/2022
1/20/20222022011231/20/2022

 

I want to add Customers[Custom] like if Year = Year and Month = Month and RecordID = RecordID so bring me the max date in this month for my example it: 1/20/2022 

 

thanks all!

  • See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJS0lGCUoYgbGSsFKsDkjXGK2uJV9bQBK+0kQEeaSN9IzRZsAojE6gsuruQZaF+MoTIGsJNNoVZjFXWDKbXAJd0LAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Year = _t, Month = _t, RecordID = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Year", Int64.Type}, {"Month", Int64.Type}, {"RecordID", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Year", "Month", "RecordID"}, {{"Custom", each List.Max([Date]), type nullable date}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Year", "Month", "RecordID"}, #"Grouped Rows", {"Year", "Month", "RecordID"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Custom"}, {"Grouped Rows.Custom"})
    in
        #"Expanded Grouped Rows"

     

1 Reply

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJS0lGCUoYgbGSsFKsDkjXGK2uJV9bQBK+0kQEeaSN9IzRZsAojE6gsuruQZaF+MoTIGsJNNoVZjFXWDKbXAJd0LAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Year = _t, Month = _t, RecordID = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Year", Int64.Type}, {"Month", Int64.Type}, {"RecordID", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Year", "Month", "RecordID"}, {{"Custom", each List.Max([Date]), type nullable date}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Year", "Month", "RecordID"}, #"Grouped Rows", {"Year", "Month", "RecordID"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Custom"}, {"Grouped Rows.Custom"})
    in
        #"Expanded Grouped Rows"