Created
November 26, 2022 07:39
-
-
Save bjulius/19c299a55644f74cee6dda158cf5dcdf to your computer and use it in GitHub Desktop.
Excel BI Power Query Challenge 29 – Brian Julius Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let | |
| Source = RepeatsRaw, | |
| Unpivot = Table.TransformColumnTypes( Table.RenameColumns( Table.RemoveColumns( Table.UnpivotOtherColumns(Source, {"Year"}, "Attribute", "Value"), "Attribute"), {"Value", "Team"}), {"Year", Int64.Type}), | |
| Sort = Table.Sort(Unpivot,{{"Team", Order.Ascending}, {"Year", Order.Ascending}}), | |
| Group = Table.Group(Sort, {"Team"}, {{"All", each _, type table [Year=nullable number, Team=text]}}), | |
| AddIdx0 = Table.RemoveColumns( Table.AddColumn(Group, "Idx0", each Table.AddIndexColumn([All], "Index0", 0, 1)), "All"), | |
| Expand0 = Table.ExpandTableColumn(AddIdx0, "Idx0", {"Year", "Index0"}, {"Year", "Index0"}), | |
| AddIdx1 = Expand0, | |
| Expand1 = Table.RenameColumns( Table.TransformColumns(AddIdx1, {{"Index0", each _ + 1, type number}}), {{"Index0", "Index1"},{"Year", "YearPrev"},{"Team", "Team1"}}), | |
| Join = Table.Join( Expand0, {"Team", "Index0"}, Expand1, {"Team1", "Index1"}, JoinKind.LeftOuter), | |
| Choose = Table.SelectColumns(Join,{"Team", "Year", "Team1", "YearPrev"}), | |
| Gap = Table.AddColumn(Choose, "Gap", each [Year] - [YearPrev], type number), | |
| MaxGap = Table.Group(Gap, {"Team"}, {{"All", each _, type table [Team=text, Year=number, Team1=nullable text, YearPrev=nullable number, Gap=number]}, {"MaxGap", each List.Max([Gap]), type number}}), | |
| FilterNulls = Table.SelectRows(MaxGap, each ([MaxGap] <> null)), | |
| Expand = Table.SelectRows( Table.ExpandTableColumn(FilterNulls, "All", {"Year", "YearPrev", "Gap"}, {"Year", "YearPrev", "Gap"}), each [Gap] = [MaxGap]), | |
| Concat = Table.AddColumn(Expand, "Concat", each Text.From( [YearPrev] ) &"-"& Text.From( [Year] )), | |
| Choose2 = Table.SelectColumns(Concat,{"Team", "MaxGap", "Concat"}), | |
| Group2 = Table.Group(Choose2, {"Team", "MaxGap"}, {{"All", each _, type table [Team=text, MaxGap=number, Concat=text]}}), | |
| ConcatYears = Table.RemoveColumns( Table.AddColumn(Group2, "Years", each Text.Combine( [All][Concat], ", ")), "All"), | |
| Sort2 = Table.Sort(ConcatYears,{{"MaxGap", Order.Descending}, {"Team", Order.Ascending}}) | |
| in | |
| Sort2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment