let
fnOverallRecords = (Source) => let
ConvertedTable = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
ExpandedColumns = Table.ExpandRecordColumn(ConvertedTable, "Column1", {"wins", "losses", "ot"}),
MergedColumns = Table.CombineColumns(Table.TransformColumnTypes(ExpandedColumns, {{"wins", type text}, {"losses", type text}, {"ot", type text}}, "en-US"),{"wins", "losses", "ot"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Merged"),
TransposedTable = Table.Transpose(MergedColumns),
Result = Table.RenameColumns(TransposedTable,{{"Column1", "home"}, {"Column2", "away"}, {"Column3", "shootOuts"}, {"Column4", "lastTen"}})
in
Result,
Source = Json.Document(Web.Contents("https://statsapi.web.nhl.com/api/v1/standings?hydrate=record(overall),team&season=20222023&site=en_nhl")),
ConvertedTable = Table.FromList(Source[records], Splitter.SplitByNothing(), null, null, ExtraValues.Error),
ExpandedColumn1 = Table.ExpandRecordColumn(ConvertedTable, "Column1", {"standingsType", "league", "division", "conference", "season", "teamRecords"}),
RemovedOtherColumns = Table.SelectColumns(ExpandedColumn1,{"teamRecords"}),
ExpandedListColumn = Table.ExpandListColumn(RemovedOtherColumns, "teamRecords"),
ExpandedRecordColumn = Table.ExpandRecordColumn(ExpandedListColumn, "teamRecords", {"team", "gamesPlayed", "leagueRecord", "regulationWins", "goalsAgainst", "goalsScored", "points", "row", "streak", "pointsPercentage", "records"}),
ExpandedRecordColumnTeam = Table.ExpandRecordColumn(ExpandedRecordColumn, "team", {"shortName"}),
ExpandedRecordColumnLeague = Table.ExpandRecordColumn(ExpandedRecordColumnTeam, "leagueRecord", {"wins", "losses", "ot"}),
ExpandedRecordColumnTeamRecords = Table.ExpandRecordColumn(ExpandedRecordColumnLeague, "records", {"overallRecords"}),
ExpandedRecordColumnTeamStreak = Table.ExpandRecordColumn(ExpandedRecordColumnTeamRecords, "streak", {"streakCode"}),
AddColumnOverallRecords = Table.AddColumn(ExpandedRecordColumnTeamStreak, "Custom", each fnOverallRecords(_[overallRecords])),
ExpandedColumnOverallRecords = Table.ExpandTableColumn(AddColumnOverallRecords, "Custom", {"home", "away", "shootOuts", "lastTen"}),
RemovedColumns = Table.RemoveColumns(ExpandedColumnOverallRecords,{"overallRecords"}),
AddedColumnDiff = Table.AddColumn(RemovedColumns, "diff", each [goalsScored] - [goalsAgainst]),
ReorderedColumns = Table.ReorderColumns(AddedColumnDiff,{"shortName", "gamesPlayed", "wins", "losses", "ot", "points", "pointsPercentage", "regulationWins", "row", "goalsScored", "goalsAgainst", "diff", "home", "away", "shootOuts", "lastTen", "streakCode"}),
RenamedColumns = Table.RenameColumns(ReorderedColumns,{{"shortName", "National Hockey League"}, {"gamesPlayed", "GP"}, {"wins", "W"}, {"losses", "L"}, {"ot", "OT"}, {"points", "PTS"}, {"pointsPercentage", "P%"}, {"regulationWins", "RW"}, {"row", "ROW"}, {"goalsScored", "GF"}, {"goalsAgainst", "GA"}, {"diff", "DIFF"}, {"home", "HOME"}, {"away", "AWAY"}, {"shootOuts", "S/O"}, {"lastTen", "L10K"}, {"streakCode", "STRK"}}),
ChangedColumnTypes = Table.TransformColumnTypes(RenamedColumns,{{"National Hockey League", type text}, {"GP", Int64.Type}, {"W", Int64.Type}, {"L", Int64.Type}, {"OT", Int64.Type}, {"PTS", Int64.Type}, {"P%", type number}, {"RW", Int64.Type}, {"ROW", Int64.Type}, {"GF", Int64.Type}, {"GA", Int64.Type}, {"DIFF", Int64.Type}, {"HOME", type text}, {"AWAY", type text}, {"S/O", type text}, {"L10K", type text}, {"STRK", type text}}),
SortedRows = Table.Sort(ChangedColumnTypes,{{"PTS", Order.Descending}, {"GP", Order.Ascending}, {"RW", Order.Descending}, {"ROW", Order.Descending}, {"W", Order.Descending}, {"DIFF", Order.Descending}, {"GF", Order.Descending}}),
Result= SortedRows
in
Result