Skip to content

Instantly share code, notes, and snippets.

@bjulius
Created November 20, 2022 13:40
Show Gist options
  • Save bjulius/b46a023482505baa29a7fc26474ec187 to your computer and use it in GitHub Desktop.
Save bjulius/b46a023482505baa29a7fc26474ec187 to your computer and use it in GitHub Desktop.
Excel Power Query Challenge 28 - Brian Julius Solution
let
Source = Table.Group(GreekRaw, {"Greek"}, {{"All", each _, type table [Seq=nullable text, Greek=nullable text]}}),
CountRows = Table.RemoveColumns( Table.AddColumn(Source, "RowCount", each Table.AddIndexColumn( [All], "RowCount", 1, 1)), "All"),
ExpandRowCounter = Table.ExpandTableColumn(CountRows, "RowCount", {"Seq", "RowCount"}, {"Seq", "RowCounter"}),
SortIndex = Table.AddIndexColumn(ExpandRowCounter, "SortIndex", 1, 1, Int64.Type),
GlobalMax = List.Max( Table.Group(SortIndex, {"Greek"}, {{"Count", each Table.RowCount(_), Int64.Type}})[Count] ),
DistList = List.Distinct(SortIndex[Greek]),
RepeatList = Table.FromList(List.Repeat(DistList, GlobalMax), Splitter.SplitByNothing(), null, null, ExtraValues.Error),
Group = Table.Group(RepeatList, {"Column1"}, {{"All", each _, type table [Column1=text]}}),
RptIdx = Table.RemoveColumns( Table.AddColumn(Group, "RepeatIdx", each Table.AddIndexColumn( [All], "RptIdx", 1,1)), "All"),
ExpandMaxList = Table.RenameColumns( Table.ExpandTableColumn(RptIdx, "RepeatIdx", {"RptIdx"}, {"RptIdx"}), {"Column1", "Greek2"}),
Merged = Table.Join( ExpandMaxList, {"Greek2", "RptIdx"}, SortIndex, {"Greek","RowCounter"}, JoinKind.LeftOuter),
FillDown = Table.FillDown(Merged,{"SortIndex"}),
Sort = Table.Sort(FillDown,{{"SortIndex", Order.Ascending}, {"Greek2", Order.Ascending}, {"Seq", Order.Descending}}),
Reorder = Table.ReorderColumns(Sort,{"Seq", "Greek2", "RptIdx", "Greek", "RowCounter", "SortIndex"}),
Clean = Table.TransformColumnTypes( Table.RenameColumns( Table.SelectColumns(Reorder,{"Seq", "Greek2"}), {"Greek2", "Greek"}), {"Seq", Int64.Type})
in
Clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment