let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Cols = let x = Table.ColumnNames(Source) in {x} & {List.InsertRange(x,3,{"Futur Contrat", "Statut"})},
#"Grouped Rows" = Table.Group(Source, List.Transform({0,1,2}, each Cols{0}{_}), {{"Custom", each
let x = Table.SelectRows(_, (y)=> Record.Field(y, Cols{0}{5}) > DateTime.LocalNow()) in
if Table.RowCount(x) > 1 then
List.InsertRange(Table.ToRows(Table.SelectRows(x, (z)=> Record.Field(z, Cols{0}{5}) = List.Min(Table.Column(x, Cols{0}{5})))){0},3,{"*","actif"})
else
List.InsertRange(Table.ToRows(x){0},3,{null,"actif"})
}})[Custom],
Result = Table.Combine(List.Transform(#"Grouped Rows", each Table.FromRows({_},Cols{1})))
in
Result