Visualizing Commonwealth games performance – Interactive chart

Posted on April 13th, 2018 in Charts and Graphs , Power Query - 2 comments

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.

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.

 

2 Responses to “Visualizing Commonwealth games performance – Interactive chart”

  1. liu says:

    To control the order of slicer item, you may define a custom list in advance,

  2. Hey Chandoo , can you also show some of this awesomeness in PowerBi as well?

Leave a Reply