Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Group by the last two dates

Greetings to all!   I need help grouping information from a database I have here in power query.   Briefly, I have a table of nominations where the availability of a position is recorded: Pos...
  • WanderingBI's avatar
    2 years ago

    Hi,

     

    this will sort the data by [Position ID] and [Date] and only keep the last two dates for each position id:

     

     

     

    let
      Source = Table.FromRows(
        Json.Document(
          Binary.Decompress(
            Binary.FromText(
              "nZJNC4JAEIb/yuI5Y2b2K+fatTpUdBEPgVIdTJAi+veNZpAG4nZbFh7ed56ZNI0OSwLCGIF0NIvkbWJwMaFCx9Yywhzkf1sc86faVI8om30xrseAQmByDEnL7M5VfVP7oi77kPeDIGRYfIJW1fU0jZF+BltmXeSXezmR0mxMAIW+ocizduFUwvhHQ6FssECRQZMFvutpy3a69I4xrH2ICKfQMlk2ozch1zeIosY5jW+qT3UiRKAfGeqXAbZa5gpI6rYryt0wKXsB", 
              BinaryEncoding.Base64
            ), 
            Compression.Deflate
          )
        ), 
        let
          _t = ((type nullable text) meta [Serialized.Text = true])
        in
          type table [#"Position ID" = _t, Date = _t, Availability = _t]
      ), 
      inputTable = Table.TransformColumnTypes(Source, {{"Position ID", type text}, {"Date", type datetime}, {"Availability", type text}}), 
      inputTableSorted = Table.Sort(inputTable, {{"Position ID", Order.Descending}, {"Date", Order.Descending}}), 
      #"Added Custom" = Table.AddColumn(
        inputTableSorted, 
        "RowsForPositionID", 
        (outerRow) => Table.SelectRows(inputTableSorted, (innerRow) => innerRow[Position ID] = outerRow[Position ID])
      ), 
      #"Added Custom1" = Table.AddColumn(#"Added Custom", "Max2Dates", each List.MaxN([RowsForPositionID][Date], 2)), 
      #"Added Custom2" = Table.AddColumn(#"Added Custom1", "IsInLastTwoDates", each if List.Contains([Max2Dates], [Date]) then true else false), 
      #"Filtered Rows" = Table.SelectRows(#"Added Custom2", each ([IsInLastTwoDates] = true)), 
      #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows", {"RowsForPositionID", "Max2Dates", "IsInLastTwoDates"}), 
      tableSorted = Table.Sort(#"Removed Columns", {{"Position ID", Order.Descending}, {"Date", Order.Descending}})
    in
      tableSorted

     

     

     

    Or a more elegant solution with using Table.MaxN:

     

    let
      Source = Table.FromRows(
        Json.Document(
          Binary.Decompress(
            Binary.FromText(
              "nZJNC4JAEIb/yuI5Y2b2K+fatTpUdBEPgVIdTJAi+veNZpAG4nZbFh7ed56ZNI0OSwLCGIF0NIvkbWJwMaFCx9Yywhzkf1sc86faVI8om30xrseAQmByDEnL7M5VfVP7oi77kPeDIGRYfIJW1fU0jZF+BltmXeSXezmR0mxMAIW+ocizduFUwvhHQ6FssECRQZMFvutpy3a69I4xrH2ICKfQMlk2ozch1zeIosY5jW+qT3UiRKAfGeqXAbZa5gpI6rYryt0wKXsB", 
              BinaryEncoding.Base64
            ), 
            Compression.Deflate
          )
        ), 
        let
          _t = ((type nullable text) meta [Serialized.Text = true])
        in
          type table [#"Position ID" = _t, Date = _t, Availability = _t]
      ), 
      inputTable = Table.TransformColumnTypes(Source, {{"Position ID", type text}, {"Date", type datetime}, {"Availability", type text}}), 
      inputTableSorted = Table.Sort(inputTable, {{"Position ID", Order.Descending}, {"Date", Order.Descending}}),
        #"Grouped Rows" = Table.Group(inputTableSorted, {"Position ID"}, {{"Filtered", each Table.MaxN(_,"Date",2)}}),
        #"Expanded Filtered" = Table.ExpandTableColumn(#"Grouped Rows", "Filtered", {"Date", "Availability"}, {"Filtered.Date", "Filtered.Availability"}),
        #"Renamed Columns" = Table.RenameColumns(#"Expanded Filtered",{{"Filtered.Date", "Date"}, {"Filtered.Availability", "Availability"}}),
        tableSorted = Table.Sort(#"Renamed Columns", {{"Position ID", Order.Descending}, {"Date", Order.Descending}})
    in
        tableSorted