Skip to content

Instantly share code, notes, and snippets.

@bjulius
Created December 9, 2022 23:36
Show Gist options
  • Save bjulius/5a9655ce608800edd9802b94702f63df to your computer and use it in GitHub Desktop.
Save bjulius/5a9655ce608800edd9802b94702f63df to your computer and use it in GitHub Desktop.
Nested Ranking Example Using Table.AddRankColumn
let
Source = StandingsRaw,
Group = Table.Group(Source, {"Conference"}, {{"All", each _, type table [Team=nullable text, Wins=nullable number]}}),
RankCompetition = Table.SelectColumns( Table.AddColumn(Group, "NestRankComp", each Table.AddRankColumn( [All], "RankComp", { "Wins", Order.Descending}, [RankKind = RankKind.Competition ])), {"NestRankComp"}),
RankDense = Table.RemoveColumns( Table.AddColumn(RankCompetition, "RankCompDense", each Table.AddRankColumn( [NestRankComp], "RankDense", { "Wins", Order.Descending}, [RankKind = RankKind.Dense ])), "NestRankComp"),
RankOrdinal = Table.RemoveColumns( Table.AddColumn(RankDense, "RankCompDenseOrd", each Table.AddRankColumn( [RankCompDense], "RankOrdinal", { "Wins", Order.Descending}, [RankKind = RankKind.Ordinal])), "RankCompDense"),
Expand = Table.TransformColumnTypes( Table.ExpandTableColumn(RankOrdinal, "RankCompDenseOrd", {"Team", "Wins", "Conference", "Date", "RankComp", "RankDense", "RankOrdinal"}, {"Team", "Wins", "Conference", "Date", "RankComp", "RankDense", "RankOrdinal"}), {"Wins", Int64.Type}),
#"Changed Type" = Table.TransformColumnTypes(Expand,{{"Wins", Int64.Type}, {"RankComp", Int64.Type}, {"RankDense", Int64.Type}, {"RankOrdinal", Int64.Type}, {"Date", type date}, {"Conference", type text}, {"Team", type text}})
in
#"Changed Type"
@bjulius
Copy link
Author

bjulius commented Dec 9, 2022

To create the StandingsRaw source table, just drop the following code into a blank query:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdHBbsIwDAbgV5l65gBsgHZtNQ2NgRAg9VBx8ILVWc2SKk2KlKdfoVlXrFxy8JffsuyiSDKUlkSTTJL5tHveoLFoVHKeFEnqRHWD2YpBBi1IQnPHJcM1XPvUM4M9iBDhskMbra+WITBncIDa6jhtFIWpZ0xy8mAuUVoj2Fg9dVLeAq/8vzaqn5mvZk+N1SomWyhJdLUFqx+dGvaY46gTShLQ24LZuyHvuwtEcUOqHNY5hp0ry7DpF0aZpLoOy+a2hRbN31J5z5MBkk+pBD+67fjDB3gf65qDMaTjmRP9fKG5atnicK4H/3bqgiZGn1CFQaZMDlpU/zcby7F298jDBc6/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Team = _t, Wins = _t, Conference = _t]),
#"Added Custom" = Table.AddColumn(Source, "Date", each #date(2022, 12, 7)),
#"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Wins", Int64.Type}})
in
#"Changed Type"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment