@Cambo,
Do you really want to create a column that reverses the order of every second row? Or, alternatively, do you want to respect the season, venue, date, time and round of the game and identify the home and away teams (and quarter and final scores and win/loss) of each?
You can do this a few different ways.
One way would break the steps into separate queries (but reading the file contents only once) like:
Base
let
Source = Excel.Workbook(File.Contents("path\to\file\file.xlsx"), true, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Sheet1_Sheet,{{"Teams", type text}, {"Round", Int64.Type}, {"Home/Away", type text}, {"Quarter Scores", type text}, {"Final Score", Int64.Type}, {"Date", type date}, {"Time", type time}, {"Venue", type text}, {"Crowd", Int64.Type}, {"Season", Int64.Type}, {"Win/Loss", type text}})
in
#"Changed Type"
Main
let
Source = Base,
#"Removed Other Columns" = Table.SelectColumns(Source,{"Round", "Date", "Time", "Venue", "Crowd", "Season"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"),
#"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Index", 1, 1, Int64.Type)
in
#"Added Index"
Home
let
#"Merged Queries" = Table.NestedJoin(Base, {"Season", "Venue", "Date", "Time", "Round"}, Main, {"Season", "Venue", "Date", "Time", "Round"}, "Main", JoinKind.LeftOuter),
#"Expanded Main" = Table.ExpandTableColumn(#"Merged Queries", "Main", {"Index"}, {"Index"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Main",{"Round", "Date", "Time", "Venue", "Crowd", "Season"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([#"Home/Away"] = "Home"))
in
#"Filtered Rows"
Away
let
#"Merged Queries" = Table.NestedJoin(Base, {"Season", "Venue", "Date", "Time", "Round"}, Main, {"Season", "Venue", "Date", "Time", "Round"}, "Main", JoinKind.LeftOuter),
#"Expanded Main" = Table.ExpandTableColumn(#"Merged Queries", "Main", {"Index"}, {"Index"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Main",{"Round", "Date", "Time", "Venue", "Crowd", "Season"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([#"Home/Away"] = "Away"))
in
#"Filtered Rows"
Merged
let
Source = Table.NestedJoin(Main, {"Index"}, Home, {"Index"}, "Home", JoinKind.LeftOuter),
#"Merged Queries" = Table.NestedJoin(Source, {"Index"}, Away, {"Index"}, "Away", JoinKind.LeftOuter),
#"Expanded Home" = Table.ExpandTableColumn(#"Merged Queries", "Home", {"Teams", "Quarter Scores", "Final Score", "Win/Loss"}, {"Home.Teams", "Home.Quarter Scores", "Home.Final Score", "Home.Win/Loss"}),
#"Expanded Away" = Table.ExpandTableColumn(#"Expanded Home", "Away", {"Teams", "Quarter Scores", "Final Score", "Win/Loss"}, {"Away.Teams", "Away.Quarter Scores", "Away.Final Score", "Away.Win/Loss"})
in
#"Expanded Away"
Alternatively, you could do this in one query:
OneQuery
let
Source = Excel.Workbook(File.Contents("path\to\file\path.xlsx"), null, true),
Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet, [PromoteAllScalars=true]),
#"Renamed Columns" = Table.RenameColumns(#"Promoted Headers", {{"Home/Away", "HomeAway"}, {"Teams", "Team_Home"}, {"Win/Loss", "WinLoss"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns", {
{"Team_Home", type text},
{"Round", Int64.Type},
{"HomeAway", type text},
{"Quarter Scores", type text},
{"Final Score", Int64.Type},
{"Date", type date},
{"Time", type time},
{"Venue", type text},
{"Crowd", Int64.Type},
{"Season", Int64.Type},
{"WinLoss", type text}
}),
HomeTeams = Table.SelectRows(#"Changed Type", each [HomeAway] = "Home"),
AwayTeams = Table.SelectRows(#"Changed Type", each [HomeAway] = "Away"),
HomeTeamsWithIndex = Table.AddIndexColumn(HomeTeams, "Index", 1, 1, Int64.Type),
AwayTeamsWithIndex = Table.AddIndexColumn(AwayTeams, "Index", 1, 1, Int64.Type),
MergedTable = Table.NestedJoin(HomeTeamsWithIndex, {"Round", "Index"}, AwayTeamsWithIndex, {"Round", "Index"}, "AwayTeam", JoinKind.LeftOuter),
ExpandedTable = Table.ExpandTableColumn(MergedTable, "AwayTeam", {"Team_Home", "Quarter Scores", "Final Score", "WinLoss"}, {"Team_Opposition_Away", "Quarter_Scores_Away_Merged", "Score_Away", "Win_Loss_Away_Merged"}),
MergedTableAway = Table.NestedJoin(AwayTeamsWithIndex, {"Round", "Index"}, HomeTeamsWithIndex, {"Round", "Index"}, "HomeTeam", JoinKind.LeftOuter),
ExpandedTableAway = Table.ExpandTableColumn(MergedTableAway, "HomeTeam", {"Team_Home", "Quarter Scores", "Final Score", "WinLoss"}, {"Team_Opposition_Home", "Quarter_Scores_Home_Merged", "Score_Home", "Win_Loss_Home_Merged"}),
CombinedTable = Table.Combine({ExpandedTable, ExpandedTableAway}),
CombinedTableWithFinalScores1 = Table.AddColumn(CombinedTable, "Final_Score_Home", each if [HomeAway] = "Home" then [Final Score] else [Score_Home]),
CombinedTableWithFinalScores2 = Table.AddColumn(CombinedTableWithFinalScores1, "Final_Score_Away", each if [HomeAway] = "Away" then [Final Score] else [Score_Away]),
CombinedTableWithQuarterScores1 = Table.AddColumn(CombinedTableWithFinalScores2, "Quarter_Scores_Home", each if [HomeAway] = "Home" then [Quarter Scores] else [Quarter_Scores_Home_Merged]),
CombinedTableWithQuarterScores2 = Table.AddColumn(CombinedTableWithQuarterScores1, "Quarter_Scores_Away", each if [HomeAway] = "Away" then [Quarter Scores] else [Quarter_Scores_Away_Merged]),
CombinedTableWithWinLoss1 = Table.AddColumn(CombinedTableWithQuarterScores2, "Win_Loss_Home", each if [HomeAway] = "Home" then [WinLoss] else [Win_Loss_Home_Merged]),
CombinedTableWithWinLoss2 = Table.AddColumn(CombinedTableWithWinLoss1, "Win_Loss_Away", each if [HomeAway] = "Away" then [WinLoss] else [Win_Loss_Away_Merged]),
SelectedColumns = Table.SelectColumns(CombinedTableWithWinLoss2, {"Team_Home", "Round", "HomeAway", "Date", "Time", "Venue", "Crowd", "Season", "Team_Opposition_Away", "Final_Score_Home", "Final_Score_Away", "Quarter_Scores_Home", "Quarter_Scores_Away", "Win_Loss_Home", "Win_Loss_Away"}),
FinalResult = Table.SelectRows(SelectedColumns, each [HomeAway] = "Home")
in
FinalResult
The approach is basically the same for each. Split the file into home and away then merge it back with itself.
Here’s a look at the data:
I’m sure others can provide better solutions.