Skip to content

Instantly share code, notes, and snippets.

@bjulius
Created July 29, 2023 18:28
Show Gist options
  • Save bjulius/2b0b25cfaaf12e80ff3213ecec0a437e to your computer and use it in GitHub Desktop.
Save bjulius/2b0b25cfaaf12e80ff3213ecec0a437e to your computer and use it in GitHub Desktop.
Excel BI Power Query 99 Challenge - Brian Julius solution
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Type = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Amount", Int64.Type}}),
AddShortMon = Table.AddColumn(Type, "Month", each Text.Start( Date.MonthName( [Date] ), 3)),
Group = Table.Group(AddShortMon, {"Month"}, {{"Min", each List.Min([Amount]), type nullable number}, {"Max", each List.Max([Amount]), type nullable number}, {"All", each _, type table [Date=nullable date, Amount=nullable number, Month=text]}}),
Expand = Table.ExpandTableColumn(Group, "All", {"Date", "Amount"}, {"Date", "Amount"}),
AddMinDate = Table.AddColumn(Expand, "MinDate", each if [Amount] = [Min] then [Date] else null, type text),
AddMaxDate = Table.AddColumn(AddMinDate, "MaxDate", each if [Amount] = [Max] then [Date] else null, type text),
Regroup = Table.Group(AddMaxDate, {"Month", "Min", "Max", "Amount"}, {{"MinDate2", each Text.Combine(List.Transform([MinDate], Text.From ), ", "), type nullable text},{"MaxDate2", each Text.Combine(List.Transform([MaxDate], Text.From ), ", "), type nullable text}}),
ReReGroup = Table.Group(Regroup, {"Month", "Min", "Max"}, {{"Min Date", each List.Max([MinDate2]), type nullable text}, {"Max Date", each List.Max([MaxDate2]), type nullable text}}),
Reorder = Table.ReorderColumns(ReReGroup,{"Month", "Min", "Min Date", "Max", "Max Date"})
in
Reorder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment