Forum Discussion
Month MAX record
- 9 years ago
An alternative in - advanced - Power Query would be:
let Source = TransactionTable, Grouped = Table.Group(Source, {"Tran_DT"}, {{"AllData", each Table.MaxN(_,"TransactionCount",1), Value.Type(Source)}},null, (x,y) => let Sort1 = Value.Compare(Date.Year(x[Tran_DT]),Date.Year(y[Tran_DT])), Sort2 = if Sort1 <> 0 then Sort1 else Value.Compare(Date.Month(x[Tran_DT]),Date.Month(y[Tran_DT])) in Sort2), Removed = Table.RemoveColumns(Grouped,{"Tran_DT"}), Expanded = Table.ExpandTableColumn(Removed, "AllData", {"Tran_DT", "TransactionCount"}, {"Tran_DT", "TransactionCount"}) in ExpandedBasically it is "Group By" Tran_DT, but taking full advantage of Table.Group capacities;
Syntax of Table.Group:
Table.Group(table as table, key as any, aggregatedColumns as list, optional groupKind as nullable number, optional comparer as function) as table
A custom comparer function is provided to group on year/month (the inner "let .. in" part).
Parameter aggregatedColumns is adjusted so a table is returned with 1 record (the maximum TransactionCount) and with the same columntypes as table Source (this is the part Value.Type(Source)).
Note that the column "Tran_DT" after grouping does not contain the date on which the maximum was reached, so this column is removed.
The nested tables in column "AllData" contain exactly the information you require, so after expanding the nested tables, you have your result.
Above is the screen shot for your reference. Yellow highlighted part is coming in result and red circled is what is desired.
Thanks,
Anupam
An alternative in - advanced - Power Query would be:
let
Source = TransactionTable,
Grouped = Table.Group(Source, {"Tran_DT"}, {{"AllData", each Table.MaxN(_,"TransactionCount",1), Value.Type(Source)}},null,
(x,y) =>
let
Sort1 = Value.Compare(Date.Year(x[Tran_DT]),Date.Year(y[Tran_DT])),
Sort2 = if Sort1 <> 0 then Sort1 else Value.Compare(Date.Month(x[Tran_DT]),Date.Month(y[Tran_DT]))
in
Sort2),
Removed = Table.RemoveColumns(Grouped,{"Tran_DT"}),
Expanded = Table.ExpandTableColumn(Removed, "AllData", {"Tran_DT", "TransactionCount"}, {"Tran_DT", "TransactionCount"})
in
ExpandedBasically it is "Group By" Tran_DT, but taking full advantage of Table.Group capacities;
Syntax of Table.Group:
Table.Group(table as table, key as any, aggregatedColumns as list, optional groupKind as nullable number, optional comparer as function) as table
A custom comparer function is provided to group on year/month (the inner "let .. in" part).
Parameter aggregatedColumns is adjusted so a table is returned with 1 record (the maximum TransactionCount) and with the same columntypes as table Source (this is the part Value.Type(Source)).
Note that the column "Tran_DT" after grouping does not contain the date on which the maximum was reached, so this column is removed.
The nested tables in column "AllData" contain exactly the information you require, so after expanding the nested tables, you have your result.