The 2018 edition of Commonwealth games are on for a week now. Both of my homes – India and New Zealand have done so well. Naturally, I wanted to gather games data and make something fun and creative from it. Here is my attempt to amuse you on this Friday.

Looks interesting? Want to know how to make something like this on your own? Then read on…
1. Gather data thru live connection to gc2018.com
We want to set up a refreshable visualization. So the data will be fetched thru PowerQuery. All we need to know about medal standings, medalists and country participation data is available at gc2018.com website.
Latest Medal Standings: This is available at https://results.gc2018.com/en/all-sports/medal-standings.htm page as an HTML table (the first table on the page).
So we can get the data using below M:
= Web.Page(Web.Contents("https://results.gc2018.com/en/all-sports/medal-standings.htm")){0}[Data]
This is extracting data column of first row of the Web.Page.
Number of participants by country: There is no one page where this information is available. Instead you need to visit each country’s page on gc2018.com to get the data from participants table. For example, the page for Bermuda (available at https://results.gc2018.com/en/all-sports/entries-bermuda.htm) looks like this:

Fortunately, all URLs follow the same pattern. https://results.gc2018.com/en/all-sports/entries-<country name>.htm
So given a URL in column [URL], we can use this custom column formula to get the total number of participants.
=Table.SelectRows(Web.Page(Web.Contents([URL])){0}[Data], each ([Discipline] = "Total")))
This is extracting the first table on URL and then filtering it for Total Row.
Arranging everything in one table:
We can use a bit of built-in operations in Power Query to arrange all the necessary data in one tidy table. I am not going to explain all steps, but here is the final output. Try to come up with this on your own.

Now that our medal data is in Excel, in a table named medal_standings, let’s go ahead to next step.
2. Calculations to show medal standings by any criteria
The calculation engine for our little medal standings has few key things:
- Fetching and sorting by a column
- Slicer selection for sort options (Gold, Silver, Bronze, Total Medals, M/P)
Let’s go thru them:
Fetching and sorting by a column:
We would like to see countries by Gold, Silver, Bronze, Total medals and Medals per participant.
As per our medal_standings table, these are 3,4,5,6 and 9 respectively.
Assuming the column we want to sort is given by a named range – sort.option.num, we can use INDEX formula to fetch values, like this:
=IFERROR(INDEX(medal_standings,<row number>,sort.option.num),-10)
The -10 ensures that if we poll for a row that doesn’t exit, we get a negative value rather than 0. As some countries have 0 medals, having negative ensures that when sorting such rows are always at bottom.
Once we fetch a column, you can use LARGE() to re-order them top to bottom.
As there will be ties (few countries getting same number of medals), we can use de-duplication logic. This is when you add a very small unique fraction to each row before calling LARGE(). It is an elegant way to deal with ties and overcome Excel’s lookup formula limitations of returning only first match. See this decade old post by Robert discussing deduplication technique.
After this, just re-arrange original data (only columns needed for output) using another set of INDEX formulas.
A slicer to allow user to pick sort option
Now let’s just link up sort.option.num to a slicer so user can tell calculation engine what the sort order should be.
Start by making a pivot from a range like this:

But when you add a slicer on sort option, you realize the folly of your plan. The slicer buttons are out of order.

Technically, they are in order – alphabetical. But that is not what we want. We want them in the order – Gold, Silver, Bronze, Total Medals and M/P.
So what now?
Simple, we can ask Jackie Chan to karate chop the slicer and re-arrange it.
Alas, my summonJackie() macro was subscript out of ranging. So we need something else.
So we cheat Excel. We can pre-fix empty spaces – CHAR(129) to the slicer items. Since these are empty spaces, we just add 1 space for Gold, 2 for Silver etc. and make a slicer from these new values.
Note: In Power BI, you can simply order the sort option column by index number and that will fix the slicer problem. In fact, we wouldn’t bother with a slicer as Power BI tables are sortable by clicking on header.
That is better. Now simply style it and give it a buzz cut and you get this.

Related: Comprehensive guide and tutorial on all things Slicer – MUST READ
3. Preparing the viz
Now that everything we need is ready, simply bring calculated table to a blank worksheet (using Copy, Paste links) and arrange it in a neat table. Add Conditional formatting > Databars on medal and M/P columns. Position slicer neatly where these columns headers should be and you are gold.
Every now and then press Ctrl+Alt+F5 and go make a cuppa. When you are back, the medal table would be updated. Of course, come 16th of April 2018, there is no need to refresh it as the games would have ended.
Download the Commonwealth 2018 games medal tracker
Click here to download the Excel file. Play with it to learn more. Examine the query definition, control sheet and viz sheet to understand how it is put together. Make changes to the query (but duplicate it first, otherwise you will break the calculations) to fetch other data and make your own charts.
Ways to enhance this – adding past performance etc.
You can use the data from https://thecgf.com/ (Commonwealth Games Federation) to see historical performance and contestant data. They do not yet have 2018 values (as the games are ongoing) but you can see how countries have done in 2014 or 2010. Or you could combine this with performance in Olympics. How about combining this with demographic and well-being data (Gini scores or HDI ranks)? There are several ways you can mash-up this.
Love Games and Excel? Check out these visualizations
Me too. I like sport and I like data. Guess what, I build a lot of charts and cool visualizations on sport. Check out below and have fun.
- Roger Federer’s wimbledon win – Visualized
- Sachin Tendulkar’s cricketing awesomeness in one chart
- MLB Pitching Stats Dashboard in Excel+VBA
- Excel steeplechase keyboard shortcuts game
Want to learn how to build awesome worksheets – Check out 50 ways to analyze data course
If you liked this, you are going to love our 50 ways to analyze data online class. This program helps you analyze and visualize business data in myriad ways. Learn all about statistical analysis, financial analysis, analytical modeling, data sciency stuff (clustering, outlier detection, optimization etc.) from the comfort of your office or home. Next batch enrollments will begin soon.
Visit 50 ways course page to know more and join the waiting list.














19 Responses to “How to Distribute Players Between Teams – Evenly”
An excellent solution, especially for large data sets.
Another solution without using solver would be to assign the player with the highest score to Team 1, the 2nd to team 2, 3rd to team 3, 4th to team 3, 5th to team 2, 6th to team 1, 7th to team 1 and it continues. This method would end up with a Std Dev of 0.001247219. This works best with a distribution with lower Std Dev for the dataset.
Full Disclosure: this is not my idea, remember reading something a few years ago. Think it may have been Ozgrid
thinking back I now remember why I read about it. About 10 years back I had to distribute around 300 team members into 25-30 odd teams. Used this method based on their performance scores. I used the method I described to do this and the distribution was pretty fair.
Solver would have saved me a ton of time though 🙂
I think the issue with you first Solver approach was that you took the absolute value of the sum of team deviations (which should always be zero except for rounding) instead of the sum of the absolute values (which is a reasonable measure of how unbalanced the teams are).
Here's another simple algorithm you could use: you start from the top (with players sorted from high to low), and at each step allocate the next player to whichever team has the smallest total so far. You can implement it dynamically with some formulas so it will update automatically when the data changes.
If the scores were more widely distributed (so that this might end up with not all teams the same size), you could add a constraint to only pick among the teams which currently have fewest players at each step, or just stop adding to any team when it hits its quota.
When I tried it on the sample, I got the three teams below, with a STDEV of 0.000942809 (i.e. about half of what Solver got to).
Team 1: John, Hugo, Tom, Josh, Eric, Zane, Charles, Andrew
Team 2: Barry, Michael, Kenny, Joe, Xavier, Patrick, Oliver, William
Team 3: Henry, Steven, Ben, Frank, Kyle, Edward, Cameron, Lachlan
Thanks for sharing!
Hi,
I was looking at all the solutions and this is closest to what I intended to do. I am dividing a bunch of players into 3 soccer teams. Players availability is also a factor while deciding the teams.
So the steps the excel needs to do is as follows:
1) In availability column if "yes" go to next
2) Equally divide 'Goalkeepers', 'Strikers', 'Defenders' basis their quality
So the end result gives each 3 teams a balance of players playing at different positions.
Can this be done on Google spreadsheet with only availability as an input from the user and rest calculates by itself.
Sorry for asking such a pointed question, but I have been struggling to find a solution for it for sometime now!
Hi Ishaan,
I am working on a similar problem at the moment, so I am wondering if you ever found a solution and if you are willing to share what you did.
Hi everyone, this is a variation of the famous Knapsack Problem https://en.wikipedia.org/wiki/Knapsack_problem.
I had to use a VBA implementation recently as part of a problem, where we ar trying to allocate teams of an organization into different locations (we are a large company with many different team). The goal was to optimally allocate teams to individual buildings without putting too many teams into one building and not splitting teams apart.
As we had around 400 teams of different sizes, solver couldn't handle it anymore. Luckily there is a Knapsack algorithm implementation in VBA readily available on the internet :).
I also went with a heuristic approach first!
An interesting mathematical solution but what if Eric and Xavier can't stand each other or Patrick is best friends with Steven - the real life problems that effect "even" teams.
@Joe
You can add more criteria like
If Eric and Xavier can't stand each other
=OR(AND(E15=1,E16=1),AND(F15=1,F16=1),AND(G15=1,G16=1))
It must be False
If Patrick is best friends with Steven
=OR(AND(E5=1,E17=1),AND(F5=1,F17=1),AND(G5=1,G17=1))
It must be True
Note that the 2 formulas above are exactly the same
except for the ranges
One must be True = Friends
One must be False = Not Friends
Nice Post!
Just one question What if number of players are not even or equally divisible.
Nice post Hui!
I download your workbook and just try to change in options the Precision Restriction from 10E-6 to 10-8 and the Convergence from 10E-4 to 10E-10. The process take almost the same time, but the results was great.
The standard deviation I got was 0,000471.
Team 1: John, Tom, Kenny, Frank, Eric, Xavier, Edward, Zane
Team 2: Steven, Hugo, Ben, Joe, Josh, Oliver, Cameron, William
Team 3: Barry, Henry, Michael, Kyle, Patrick, Charles, Andrew, Lachlan
Great application of Solver! Thanks for the link!
Great explanation. Well done... However, I tried with 6 teams of 4 players and solver never did finish.
How about vba code for the same data set.
I have 3 column A B C wherein A has text and B has number Wherein C is blank. And in C1 been the header C2 where I want the name to come evenly distributed the number which is in Column B.
My Lastcolumn is 1000.
Sorry if I'm being slow here, but how is 'Team Score' calculated? I've gone through the explanation several times but it seems to just appear.
@Hrmft
This process uses the Solver Excel addin
Solver is effectively taking the model and trying different solutions until it gets a solution that meets all the criteria
Then solver puts the solution into the cell and moves to the next cell
So yes it appears to "just appear"
Hi ! Thank you so much ! Works great 🙂
I cannot get the fourth Equation to work in my excel spreadsheet
You have =($E$2:$G$25=0)+($E$2:$G$25=1)=1 as a SUMIF solution, I have, =($F$2:$H$13=0)+($F$2:$H$13=1)=1 as my solution but it does not work. The only thing I changed is the ranges. Any suggestions?
Thank you.
Jim
I cannot get the fourth Equation of TURE or FALSE statements to work in my excel spreadsheet You have =($E$2:$G$25=0)+($E$2:$G$25=1)=1 as a SUMIF solution, I have, =($F$2:$H$13=0)+($F$2:$H$13=1)=1 as my solution but it does not work. The only thing I changed is the ranges. Any suggestions?
Sorry I left some of it out in the previous question,
Thank you. Jim