Tournament Results

jimrward

Well-known Member
Joined
Feb 24, 2003
Messages
1,878
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
This is part 3 of my quest to learn how to use power tools
I have called this generically Tournament results so that it might be useful in other scenarios
Ok here we go Power Tools gurus

Over a month a competitor can complete up to 3 rounds for a competition which has a few categories for the results, overall, Ladies, Vets, Juniors, Junior Ladies
The data is a rolling log by date, I can group by name and sum the scores for a total to give me Name and total, but I want to include the round scores on each line as opposed to vertically
Here is some sample raw data, the set is incomplete butr hopefully enough data for a solution
Book1
ABCDEF
1DateNameCat1Cat2RoundScore
201/04/2023JimVet15
301/04/2023BillVet17
401/04/2023Bob14
501/04/2023FredJunior18
602/04/2023FredJunior28
703/04/2023Jim26
803/04/2023Alan111
903/04/2023DebsLadies111
1004/04/2023KirstyLadiesJunior112
1104/04/2023KimLadies15
1204/04/2023JimVet32
1304/04/2023Bob27
1404/04/2023BillVet210
Sheet1


This is what I would like to achieve, overall might be easier as it doesnt need the category logic, I havent decided on sorting yet when there are rounds not completed, looking for the method first
Book1
HIJKLM
1OverallNameR1R2R3Total
2Jim56213
3Bill71017
4Bob4711
5Fred8816
6Alan1111
7Debs1111
8Kirsty1212
9Kim55
Sheet1


And now by a single category, I have listed all sections but the logic will be the same with an edit for the category
Book1
HIJKLM
11Cat
12VetNameR1R2R3Total
13Jim56213
14Bill71017
15
16Cat
17LadiesNameR1R2R3Total
18Debs1111
19Kirsty1212
20Kim55
21
22Cat
23JuniorNameR1R2R3Total
24Fred8816
25Kirsty1212
Sheet1


Finally using 2 categories for Junior Ladies
Book1
HIJKLM
27Cat
28JuniorNameR1R2R3Total
29LadiesKirsty1212
Sheet1
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
My approach by using M code directly, which I believe is the most powerful tool in Power Tools.
To me, Power Query with interface-only usage is similar to using Excel with functions only. While it is still powerful with the interface, M makes everything more doable in Power Query, just like how it feels good in Excel with VBA.

Assumptions:
R1, R2, and R3 rounds for corresponding 1, 2, and 3 round values.
If R3 exists for a name then it means R1 and R2 must also exist for the same name.
Source table name is "Table1"

Step names in the provided M codes shortly explain what's going on. So, you can see what's happening by simply clicking on the steps after copying & pasting these codes into the blank queries in the Advanced View in the Power Query interface.

Source table as it is provided in the question:
1235916
ABCDEF
1DateNameCat1Cat2RoundScore
24/1/2023JimVet15
34/1/2023BillVet17
44/1/2023Bob14
54/1/2023FredJunior18
64/2/2023FredJunior28
74/3/2023Jim26
84/3/2023Alan111
94/3/2023DebsLadies111
104/4/2023KirstyLadiesJunior112
114/4/2023KimLadies15
124/4/2023JimVet32
134/4/2023Bob27
144/4/2023BillVet210
Data


Overall:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeTypes = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Round", Int64.Type}, {"Score", Int64.Type}}),
    SetRoundNames = Table.AddColumn(ChangeTypes, "RoundName", each "R" & Text.From([Round])),
    RemoveUnnecessaryColumns = Table.RemoveColumns(SetRoundNames,{"Date", "Cat1", "Cat2", "Round"}),
    PivotRoundColumn = Table.Pivot(RemoveUnnecessaryColumns, List.Distinct(RemoveUnnecessaryColumns[RoundName]), "RoundName", "Score", List.Sum),
    RoundCount = Table.ColumnCount(PivotRoundColumn) - 1,

    AddMissingRounds = if RoundCount < 3 then
        let
            addR3 = Table.AddColumn(PivotRoundColumn, "R3", each 0),
            addR2 = if RoundCount < 2 then Table.AddColumn(addR3, "R2", each 0) else addR3,
            Result = addR2
        in
            Result
        else PivotRoundColumn,
    ReorderColumns = Table.ReorderColumns(AddMissingRounds,{"Name", "R1", "R2", "R3"}),
    ReplaceNullRounds = Table.ReplaceValue(ReorderColumns,null,0,Replacer.ReplaceValue, {"R1", "R2", "R3"}),
    AddTotal = Table.AddColumn(ReplaceNullRounds, "Total", each [R1]+[R2]+[R3])
in
    AddTotal
1235916
ABCDE
1NameR1R2R3Total
2Alan110011
3Bill710017
4Bob47011
5Debs110011
6Fred88016
7Jim56213
8Kim5005
9Kirsty120012
Overall


By single category:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeTypes = Table.TransformColumnTypes(Source,{{"Name", type text},  {"Round", Int64.Type}, {"Score", Int64.Type}}),
    UnpivotCategories = Table.Unpivot(ChangeTypes, {"Cat1", "Cat2"}, "Attribute", "Value"),
    RenameCategoryColumn = Table.RenameColumns(UnpivotCategories,{{"Value", "Category"}}),
    SetRoundNames = Table.AddColumn(RenameCategoryColumn, "RoundName", each "R" & Text.From([Round])),
    RemoveUnnecessaryColumns = Table.RemoveColumns(SetRoundNames,{"Date", "Round", "Attribute"}),
    PivotRoundColumn = Table.Pivot(RemoveUnnecessaryColumns, List.Distinct(RemoveUnnecessaryColumns[RoundName]), "RoundName", "Score", List.Sum),
    RoundCount = Table.ColumnCount(PivotRoundColumn) - 2,

    AddMissingRounds = if RoundCount < 3 then
        let
            addR3 = Table.AddColumn(PivotRoundColumn, "R3", each 0),
            addR2 = if RoundCount < 2 then Table.AddColumn(addR3, "R2", each 0) else addR3,
            Result = addR2
        in
            Result
        else PivotRoundColumn,
    ReorderColumns = Table.ReorderColumns(AddMissingRounds,{"Name", "Category", "R1", "R2", "R3"}),
    ReplaceNullRounds = Table.ReplaceValue(ReorderColumns,null,0,Replacer.ReplaceValue, {"R1", "R2", "R3"}),
    AddTotal = Table.AddColumn(ReplaceNullRounds, "Total", each [R1]+[R2]+[R3]),
    GroupByCategory = Table.Group(AddTotal, {"Category"}, {{"GroupedByCategory", each _, type table [Name=nullable text, Category=text, R1=nullable number, R2=nullable number, R3=nullable number, Total=number]}}),
    RemoveBlanks = Table.SelectRows(GroupByCategory, each ([Category] <> "")),
    ExpandColumns = Table.ExpandTableColumn(RemoveBlanks, "GroupedByCategory", {"Name", "R1", "R2", "R3", "Total"})
in
    ExpandColumns
1235916
ABCDEF
1CategoryNameR1R2R3Total
2VetBill710017
3VetJim5027
4LadiesDebs110011
5LadiesKim5005
6LadiesKirsty120012
7JuniorFred88016
8JuniorKirsty120012
ByCategory


By multiple categories:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeTypes = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Round", Int64.Type}, {"Score", Int64.Type}}),
    SetRoundNames = Table.AddColumn(ChangeTypes, "RoundName", each "R" & Text.From([Round])),
    FilteredRowsByMultiCats = Table.SelectRows(SetRoundNames, each ([Cat1] <> null and [Cat2] <> null)),
    RemoveUnnecessaryColumns = Table.RemoveColumns(FilteredRowsByMultiCats,{"Date", "Cat1", "Cat2", "Round"}),
    PivotRoundColumn = Table.Pivot(RemoveUnnecessaryColumns, List.Distinct(RemoveUnnecessaryColumns[RoundName]), "RoundName", "Score", List.Sum),
    RoundCount = Table.ColumnCount(PivotRoundColumn) - 1,

    AddMissingRounds = if RoundCount < 3 then
        let
            addR3 = Table.AddColumn(PivotRoundColumn, "R3", each 0),
            addR2 = if RoundCount < 2 then Table.AddColumn(addR3, "R2", each 0) else addR3,
            Result = addR2
        in
            Result
        else PivotRoundColumn,
    ReorderColumns = Table.ReorderColumns(AddMissingRounds,{"Name", "R1", "R2", "R3"}),
    ReplaceNullRounds = Table.ReplaceValue(ReorderColumns,null,0,Replacer.ReplaceValue, {"R1", "R2", "R3"}),
    AddTotal = Table.AddColumn(ReplaceNullRounds, "Total", each [R1]+[R2]+[R3])
in
    AddTotal
1235916
ABCDE
1NameR1R2R3Total
2Kirsty120012
ByMultiCategory


I think these could help as a starting point. Many steps could be structured by using other M functions and methods that might be more effective or even the whole structure for each query could be redesigned by using alternative logic, for example, I would actually create a function query that could be reused for AddMissingRounds step and likely shorten each query. There is really not a single way to do things in Power Query. This is just my initial approach above.
 
Upvote 0
Solution
Wow awesome many thanks for that I will have a play over the weekend and see if I can get my head around it
 
Upvote 0

Forum statistics

Threads
1,215,984
Messages
6,128,110
Members
449,421
Latest member
AussieHobbo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top