Pages

Thursday 26 July 2012

Pivot tables - more exciting than the 100m sprint. Really.

Off the top of your head, right now, what are the top 5 burning questions you have on Track & Field performance in the last 3 Olympic summer games?

While you ponder that, here are mine....

1) How many medals are there to be gained in these categories?
2) Which countries have won the most Gold medals in each of the last 3 years?
3) Is there a country that is always a bridesmaid (ie - always the silver, never the gold)?
4)How do women fare by country? Is there a significant change in the top 3?
5)Which year had the highest number of countries winning medals?

Your first question should now be, "How are you going to find all that out?". And my answer shall be, "With the most effective data analyser Excel has to offer - the pivot table".

I know a lot of people who would prefer to wash (by hand) the USA's Olympic team's training kit, than use a pivot table. Trust me, a basic understanding of pivot tables is all that is needed to make you ditch the sweaty socks, and embrace the beauty of the table. So here it is...a taster course in basic pivot tables.

In brief, a pivot table looks at a big clump of data, and acts like Tom Cruise in Minority Report. You know that scene where he stands in front of the big clear screen and pulls images from left and right onto the screen, to come up the bigger picture? Well, that's what a pivot table does. You select how you want to look at your data, which parts you want summarised, if you want it added / counted / averaged, and you press ok. You'll be rewarded with a better overview of your data. Just try look less tense than Tom Cruise.

That's the important part, to not be tense. Because pivot tables were made to be played with. Don't be scared to break it, you can't really. You'll just change the look of it. You can change it back or start again, it is not going to affect the underlying data.

Speaking of which, here is our data (not all of it, I don't have the space!). I downloaded some stats from www.databaseolympics.com, and manipulated them to come up with this table:











This continues down to row 302, and covers 2008, 2004 and 2000.  Something to note - I have a heading over each column. This is essential, otherwise your pivot table is not going to work.

So, to create a basic pivot table, you select your data (including headings) and go to Insert - Pivot Table or press ALT D, P. A dialog box will pop up to take you through creating your pivot table.
 - In Step 1, select Microsoft Excel list or database, and PivotTable.
 - In Step 2, just confirm you have the right data
 - In Step 3, select where you want your pivot table, and click finish.

You will now get a little blank image on your sheet with pivot table written in it, and a Field List box on your right. If this Field List box doesn't appear when you click on the pivot table image, go to PivotTable Tools - Options - Field List, and make sure it's highlighted.

To create a simple summary of medals won by year, event and gender, you need to click and drag the following items in the Field List to the areas below as shown here:

Note that under Values, we have Sum of Medal. If you wanted to change that to count or average or Minimum, we could click on the downward arrow, select Field Value Settings, and select how we wanted to treat the data.

Also, you may want to change the look and design of your table. Go to the PivotTable Tools - Design tab. From here you can change your table layout (Report Layout - I always prefer Tabular), the look of your table (PivotTable Styles - just scroll down the choices until you find one you like) and whether you want sub totals and totals. Excel will automatically add them.

So here is what our pivot table looks like:

That answers our first question - how many medals per category? But how do we see which country has won the most Gold medals, and if there is always a runner up? We act like Tom Cruise...go to the Field List box, and drag and drop to your hearts content to manipulate the table into various formats.

Here is our answer for questions 2 and 3. After the USA, Russia is always chasing those Gold medals. I created this table by:
 1) Adding the medals and countries as row labels, and years as a column label.
 2) I then clicked into the Grand Total column, right clicked, and selected Sort - Sort Largest to Smallest
 3) Then I narrowed the field down to the top 3 by right clicking on the Country header, selecting Filter - Top 10, and changed it to the Top 3
4) And I selected only the Gold & Silver Medal types from the drop down box.

To answer question four, we are going to drag Gender into the Report Filter area and remove the Medal item from the field list by just dragging it out. Once you have done that, select Women from the drop down box in the Report Filter:

And look at that...Russia has taken the lead.

To answer question five, we actually get rid of the Medals won item completely, and drag the Country item into the Value field. As this is a text field, Excel automatically counts it. Here is the answer:


Now to show you a really cool trick...What happens if you want all the underlying data that comprises the 99 total for 2008? Just double click on the 99, and Excel will create a full table of that data in a new worksheet for you. It will look something like this (again, I haven't included the full table):

This is incredibly handy if you need to zero in on some specific result from your table, and provide the back up data.
As you can see, there are so many ways to use pivot tables, and I have only scratched the surface here. But don't be afraid to give them a bash... they really are fun and so useful once you get the hang of them.

A new tool which Microsoft has added in Excel 2010 is the Slicer, which is brilliant if you want to create an interactive pivot table. I'll be covering this in my newsletter, so if you want to learn more, sign up here.

Enjoy the Olympics!

Thursday 19 July 2012

Global Warming? Could we have some of the warmth please?

I have been trying my very hardest to ignore the fact that it is July. Because I live in Ireland. And I used to live in South Africa. And I am pretty darn sure that their winter is probably a little bit warmer than our summer this year. Now don't get me wrong, Ireland has a lot of good things going for it. Lovely people. Lovely beaches. Lovely scenery. Just not so lovely bbq weather.

So I was wondering today whether I am being a bit thick-skulled. I mean, I have lived here for nigh on eight years. I should be used to the non-event that is summer. My husband talks fondly of his hot childhood summers in Cork. Even I remember a couple of good weeks of sunshine when I first moved here. So am I just being silly, wanting a few days of sun?

I don't like thinking I may be losing my mind, so I decided to do what all good analysts should. I graphed the data. This data is all from www.met.ie. So, from 2000 to 2012, the total rainfall and mean temperatures recorded in June at Cork Airport have been:

If we start by just doing a simple bar graph of the temperatures, it looks like this:
I have added a trendline, and just look at that. Things are definitely slipping. To add a trendline, double click on one of the bars to select your series, right click and select Add Trendline. You can change the look of the line through the options in the dialog box that pops up.

Now, we add in the rainfall, just for the laugh. If we maintain the graph as is, it will look like this:

Not so helpful. What we need to do is plot the rainfall on the secondary vertical axis. You need to follow a couple of steps to do this:
1) Select the rainfall series by either double clicking on one of the bars, or going to your Chart Tools tab on your menu ribbon and going to Format. At the top left hand side you will see a drop down box. Select the Rainfall series, like so:


2) Once the series is highlighted, right click and select Change Series Chart Type. Select a basic line graph.
3) Right click again and select Format Data Series. The following Dialog box will pop up:
Under Series Options, click on the Secondary Axis and you're sorted. Add another trendline for the rainfall, make your lines pretty, and your graph will look like this spectacular specimen below:

Thankfully, this graph proved I wasn't completely losing my mind. Temperatures are going down and rainfall is going up. Not a good recipe for planning bbq parties.

However, I would be a very bad analyst if I didn't point out that the ridiculous amount of rainfall in June 2012 is definitely skewing the trendline. So I re-did the graph up to 2011:


Looks kinda different, doesn't it? Rainfall is still on the way up, but the downward trend on temperature needed another year of low temperature to materialise. No wonder I expected a little bit of heat this year.

Now, unless you work at Met Eireann, these graphs may seem a bit useless. But using the secondary axis is one of my favourite ways of comparing sets of data that have very different number ranges. You can even use percentages. It sometimes really brings home the point you are trying to make with data, when you show not one, but two trends, telling the same story.

This week my newsletter is going to cover something completely different and a little bit fun...how to format your cell comments to make them a little bit more exciting than the drab yellow box. Change the shape, add in pictures. Interested? Sign up here.

Hope you have some sunshine in your spreadsheets this week!

Thursday 12 July 2012

Millionaires and their assets..can you link it up?

Close your eyes and imagine you're a multi-millionaire. Think of all those houses, holiday homes, cars, private jets.... When I do this, I just think that it must be an absolute nightmare to keep track of the asset values. (Ok, I may fantasise about a beach house in the Maldives...but I'd still worry about keeping my asset values under control!)

Back to your daydreams....now picture that your beloved wife blindsides you with a surprise divorce (who could I be referring to ?). Besides maybe needing a good friend on speed dial, you would also need to have those asset values to hand, and pretty darn quick. Imagine, too, that you don't mind having a large staff, and you don't want any one of your accountants having access to too many of your assets. To provide adequate segregation, you have an accountant deal with each of your asset groups, and each of them report to you directly with a summary of assets, values, and the current month's running cost.

* Note - this is a simple illustration, so these figures are completely and utterly fictional, as I have eschewed buying luxury houses or cars in lieu of a simple life. Also, I would assume any multi-millionaire worth his salt would want to know a whole lot more about his assets, but we're keeping this simple....

So how can I help speed up this little part of your working life? Well, imagine each asset accountant has a template which he updates each month:






You, sitting in your plush office, add these figures every month to an asset overview sheet you use to keep track of your investments. This admittedly wouldn't take long but, to make it even easier, you could link your workbooks so that when your accountants update their templates each month, yours would automatically update too.

So, how do we link workbooks? Easy really. There are 3 ways to choose from, which I will take you through below, but what you want is for your summary to have formulae that look like this:

All this formula is saying is, instead of looking for the total on the summary sheet, go to a different workbook (City Apartments.xlsx) and in that workbook, look for a sheet called "City Apartments", and then find cell C10 on that sheet and bring its contents back to the summary sheet.

The official syntax is [WorkbookName]SheetName!CellAddress

So, how do get the Linking Formula into your workbook?

1) You can type it in.
This may be an easier option if you don't have the other file open, but some things to note on typing in a file name:

 a) 100% accuracy is required otherwise it won't work;

 b) If the file is closed, you will need to type in the full address, which may look something like this; ='C\Data\Assets\Monthly Updates\[City Apartments.xlsx]City Apartments'!$C$10 . Not so easy to get 100% right (see point a);

 c) If your workbook or worksheet has spaces, it will need the little ' at the beginning and end of the name.

2) You can point and click
This really is as easy as it sounds. Firstly, open your Summary Sheet and City Apartments sheet. In the Summary Sheet, click on Cell C4 and press =. Then go to your City Apartments sheet and click on cell C10. Press enter and your formula is there, perfectly spelt and looking impressive.

You can use this for all sorts of formulae that require an external link, not just picking up a number. You just type your formula and when you need the external data, just include the external cell references as I described above.

This, in my mind, is the best and easiest way of setting up links. It allows you the most flexibility, and is always accurate.

3) You can paste a link

If you simply wanted to transfer data from one cell into your workbook, as we did in our example, you can just paste as a link. So, using our example, to transfer the total value from the Cars workbook, you would click on cell C11 in the Cars workbook and copy it. Then go to cell C6 in the summary sheet, and Paste Special - Paste Link (Or Home - Clipboard - Paste - Paste Link). This creates the same linked formula you would get by pointing and clicking.

With points 2 and 3, when you close the external workbook, the full name is then shown in your formula.

With all of the above steps, if the external file is updated, when you open the summary file, the data will update automatically.

Also note that in Excel 2010, new security feautures have been added. So the first time you re-open your Summary sheet, you will see a ribbon appearing at the top of your page asking whether you want to enable links. Click Yes, and Excel shouldn't ask you again.

Now, I hate to bring you back down to earth, but time to stop imagining you're a multi-millionaire, and start assessing whether there are any workbooks you can link, and therefore save you time.

Our newsletter this week will provide some more tips on link, and some troubleshooting hints. If you haven't already, why not sign up here?

Happy Spreadsheeting!

A PS here (thanks to reader Martin for pointing out that I forgot to mention this) - if you change the source file (ie - insert or delete rows or columns) , when your final file is closed, the final file will not be adjusted, and will pick up the wrong data. So either always have both files open when making adjustments, or use a vlookup to pick up your data.

Thursday 5 July 2012

Micro Mass, Body Mass and Macros.....

Well, the 4th of July 2012 was significant. Not only did the United States' celebrate their 235th day of Independance, but the good scientists in CERN announced that they are pretty much certain they have discovered the Higgs Boson. The "what?", you may splutter into your coffee. I am certainly no expert in particle physics, so here is an easy explanation. In short, it is the tiny little particle that gives everything mass.

This led me to think about my own body mass (not something I like contemplating for very long!), and the fact that I hate having to go online to use a BMI calculator, when it would be really easy to create my own, and track my progress. Which then led me to thinking about Macros. And how they can make life easy. And how they can also make life very very frustrating.

So I am going to show you how to record a VERY simple macro. This one is really very simple. But it will give you an idea of whether there is anything you could speed up if you just recorded a simple little macro to save you from doing repetitive and boring tasks.

However, it comes with a warning. It is my experience that if you want to play around with Macros, and experiment with improving your work life, you want to have someone close by who knows quite a bit about Macros. Because they are finicky little blighters, and they can come up with errors that are as elusive as the Higgs Boson itself. That said, don't let me put you off, because they can save time.

Another thing to note is that you can of course write Macros in VBA from scratch. You don't need to record them at all. Its just easier, when you are starting out, to record them.

So, the first step in recording a good Macro is planning. Make sure you have everything you need set up before you press record. So I have created a (very fictitious) set of numbers for our test subject, Fred. Fred is 1.85 metres tall and got a lecture in December from his doctor for having a BMI of 36.52, which is classified as clinically obese. Fred's New Year's resolution was to get his BMI down to 28 by the end of that year. Looking at our data, you can see he is doing well so far:


So it is now June, and Fred has dropped down to a BMI of 33.02. The Macro we are going to record is simply going to copy the BMI each month when he recalculates it, and add it to the graph data, to update the graph. Like I said, really simple. You may want to try replicate this to see if you can get the Macro to work. If you want to, the formula for the BMI is: =C6/(C5*C5)

I have the graph data set up on the same sheet, like this:


 So we are nearly ready. We just need to insert Fred's weight for July into cell C6. Lets say he is now weighing 110kgs, which gives him a BMI of 32.14.

Now, lets record. I'll show you how to start recording, and then talk you through each step. So go to the Developer tab on your menu ribbon, and click on Record Macro. You will get a box like this:


Choose a name for your Macro, but note that it won't accept spaces in names. You can also assign a shortcut key of your choice. I also use the letter in capitals as it is less likely to already be assigned a shortcut role.

If your Macro is specific to a particular workbook, then choose the option shown above. However, if it is something you would like to use in all your spreadsheets, store it in Personal Macro Workbooks, and it will be available whenever you open Excel.

Also note that Macro needs to be saved in a Macro Enabled Workbook, so make sure that is what your workbook is saved as.

Click Ok and remember that everything you do now is recorded. You will get a little square in the bottom left hand corner of your workbook which you need to press to stop recording the macro.

Then you just do the following:

Click on Cell E6, and press Ctrl C to copy
Click on Cell N3, and press Ctrl + down arrow (this should take you to June's BMI figure)
Click on the down arrow once more (to take you to July's space) and Paste Special - Values
Press Esc
Press Ctrl S to save.
Click on the little square at the bottom left hand side of your spreadsheet, or go to Developer, Stop recording, and you're done. Your table data is updated, your graph is updated. Life is good.

This looks easy, no? Well, hate to break it to you, but there is already an issue with this macro. If you go to your developer tab, click on Macro, select your Macro and click the Edit button, this screen will appear:


This is your VBA code. Cool, isn't it? See that line I have highlighted? That's the problem. Because when you select Cell N3, and then press Ctrl + Down Arrow, it takes you to the last cell with data in it. You then press down once more, and the Macro recording selects the absolute cell reference of N10. Which is fine for July, but in August, when you run the Macro, you will find it saves the BMI into the July cell. How do we fix this?

We change the code to say "Go to N3, then go to the last cell with data in it, then go one row down." How? Easy - instead of having the code as highlighted above, you replace that one line with :

ActiveCell.Offset(1,0).Select

This will ensure you always paste in the next empty cell in column N. The offset function is great for Relative referencing, as you can move around by choosing how many rows and columns to move by, instead of selecting a specific cell.

Whenever you make changes to your code, always remember to press the 'Reset' button. Its the little square highlighted below:



Then you can close out your VBA screen and you should be ready to run your Macro. How do you do this? Either by pressing Ctrl + whichever shortcut key you selected, or go to Developer-Macro-Run Macro. Your Macro will now add in the new data every month, and the graph will update automatically.

Another way to do this is to assign a button to a Macro. My newsletter this week will describe how to do this. Not subscribed yet? Do so now, here.

If you want to play around with Macros in your own time, I can recommend purchasing a textbook to help you through the minefield.My favourite author on all Excel related matters is John Walkenbach, but there are loads out there.

Enjoy, and hope I haven't scared you off Macros for life. They are great little timesavers, once you get over the set up!

Happy Spreadsheeting!