Pages

Thursday 28 June 2012

Natural Disasters - what is the cost? (and can we use a SUMIF to find out?)

Hello all. Today my hometown was flooded. Badly. I live up on a hill, so avoided the floods, but was without water and electricity for most of the day. However, a lot of people will have lost their possesions and their business stock but thankfully not their lives. My heart has been with them.

I am also a geek, so after getting over the irritation of being inconvenienced by Mother Nature,  I started wondering about the cost of natural disasters. I found this helpful map, courtesy of The Economist and Munich Re, on the top 10 most costly natural disasters:




In table format, the economic stats look like this (please note that if there are 2 disasters combined, I have just split the costs equally):



Scary stuff.  But lets try analyse it a bit. So what if you wanted to see which disaster type costs the most, in total and on average? You would do this:



I have added in the formulas for the first row (Earthquakes) so that you can see how I came to the figures. Now, before you start praying for a natural disaster to sweep me and my formulas away, let me try explain them in English.

What a SUMIF does is look for stuff in a column within a table of data that matches a criteria you specify, and when it does find those items, it sums up all the corresponding figures.
So for the earthquake line, to add up all the costs of earthquakes, you are saying =SUMIF(look in the Disaster column, for all the Earthquakes, now add up the corresponding costs).

COUNTIF works pretty much the same, except it just counts the number of times the specific criteria appears. So to count the number of times Earthquakes appear on the list, you say =COUNTIF(look in the disaster column, count how many times Earthquake appears).

Easy as pie. The average calculation then just flows from the two answers, and indicates that after Earthquakes, Tsunamis are not be trifled with in terms of economic damage.

Now.....what if you wanted to check the total costs just in Japan, pre and post 2000?


A SUMIFS  formula follows the same concept as a single SUMIF, you can just add more criteria. Also, the order changes in the formula. So to sum all the costs in Japan pre 2000, you say: =SUMIFS(these are the costs to add up, when anything in the Year column, is less than 2000, and anything in the country column, says Japan).

It really will make sense when you get the hang of talking to yourself when you're working....!

So that's your brain bender for this week. In this week's newsletter I will be discussing some other useful Count functions. Sign up here if you would like to learn more....

Happy spreadsheeting, and stay safe and dry!



Thursday 21 June 2012

Square pegs into round holes and the wonderful dropdown list

Meet Matt. Matt is a 13 month old toddler, and his mom has given him a shape toy. You know the one I mean; you have to fit the squares into the square-shaped holes, triangles into the triangle-shaped holes…. ? Well, Matt is exploring this toy, and while he might sometimes, by pure luck, get the right shape into the right hole, most of the time he will get it wrong. And what happens then? Either pure frustration, followed shortly by a tantrum, or he’ll get bored and look for another toy.
But why do we give children these toys? Not to torment them, but to teach them shapes, spatial awareness, colours…all sorts of useful things to help them through later life.
What has all this to do with Excel? Well, have you ever received a spreadsheet, and when you try type in your data into a specific cell, you get a message like this:

This is Excel’s version of acting like your mom. It is saying, slowly and clearly, “Try again, little one, the data you input just doesn’t fit in this cell”. Now, while it may make you have a small tantrum and want to throw your laptop against the wall, the person who created the spreadsheet did this for a reason. They have stipulated, quite clearly, that only certain types of data may be entered into that cell. How did they do that? Well, you would tell a 13 month old its “Magic”. For Excel users, the answer is “Data Validation”.

This little gem (or mind bender, depending) can be found on the Data tab of your menu. It looks like this:

As you can see, there are a number of options you can select. With each option, you will be asked for more details. So for example, if you select whole numbers, you then need to select whether the number needs to be greater than, equal to, etc. a specific number. So let’s use this list of children as an example. We are entering the names of children attending preschool, so we would not expect a child to be older than 6. We would select all the cells in the 'Age' column and apply the following settings:


By stipulating this, if you had to then type in 7, you would get an error message. So what happens if you got the same list of children, with their ages already filled in? You need to check there is no-one over 6, so instead of manually checking each age, you can select the whole cell range that contains ages, and apply the same data validation as you did above. Nothing will happen. But if you then select “Circle invalid data” as below:

…all your invalid data will be circled, awaiting your correction.

To prevent future tantrums, you can assist users of your spreadsheet by supplying an Input Message which will appear if the cell is selected. To use this option, fill in the details like so:

This will result in a little pop up box giving the user your message. Likewise, by filling in the details on the Error Alert tab, if someone does fill in the wrong number or text, you can help them by detailing what they need to do to get it right:

If you’re feeling sweet, you might actually lead them in the right direction!

One of the most useful of these data validation tools in the “list” option. This is how you create a dropdown list. If you have a number of items in your list, you can create the list elsewhere in your workbook, and then reference the list co-ordinates in the source box, like so:



However, if your dropdown list is short, maybe it is only Boy or Girl, you can type them directly into the source box, separated by a comma. You would then have a drop down list in your column as shown below:
2 more points:
1)      You may have noticed there is a “Custom” option. This applies to formulas and warrants a blog all on its own. However, I will be giving a taster in my newsletter. Why not sign up?

2)      Data validation rules will be deleted if you copy or drag another cell into the cell with the data validation. So be careful! (Now I sound like your mom….hmmm...sorry about that!)

Happy Spreadsheeting!

Thursday 14 June 2012

Who's afraid of the big bad vlookup?

Little Red Riding Hood is lost. And it's not in a dark, scary wood...oh no, it's in the lot of a Hollywood Studio. You see, what with all the rehashing of classic fairytales at the moment, the casting directors are getting confused and sending the actors to the wrong studios. What they need is a good vlookup formula to get themselves on track (yes, seriously)......

So imagine these were there current projects:











What this table shows, besides the fact that handsome princes are in demand and fairies and stepmothers have got a bad name, is that there is a fair amount of opportunity to get confused with all the various details.

If each casting director were sent a lovely little lookup tool as shown below, they could select their fairytale from the dropdown list, and the answers would automatically flow into place, along with the correct studio number.
Now before you fall into a 100 year sleep, let me explain VLOOKUPs as simply as I can. The official syntax of a vlookup is:

=VLOOKUP(lookup_value,table_array,col_index_num,range_lookup).
Yup. Hardly surprising it sends most people running for their fairy godmothers. Instead of fleeing, if you are using a VLOOKUP formula, try saying this in your head as you type it out. It may help you make sense of the formula:

=VLOOKUP(look for this, in here, bring back stuff from that column, false). So much easier than the official set of syntax, no?

Here is what the formula means, in plain english:

lookup_value - this is the cell reference of the item you want to search for in the data table. So for our example above, the lookup_value is the Fairytale name.

table_array - this is the table of data that you are using to search for all your answers.
NOTE: for vlookups to work, the first column in the table of data needs to be the column containing your lookup_values. In our example, that means the 'Fairytale' column must come first.

col_index_number - this will depend on what data you want to bring back. In our example, to bring back the Leading Lady, you insert a 2, as this info is contained in the 2nd column of data if you start counting columns (left to right) from the column that contains your lookup_values.  For Good Guys, use 3, for Bad Guys, use 4 etc.

range_lookup - This is always either TRUE, FALSE or you can leave it out, which is the same as typing in TRUE. To get an exact match on your lookup_value, type in FALSE. If it can't find an exact replica of the item you are searching for, you will get an #N/A. If you type in TRUE, it will look for the nearest match and bring back that data. This has its uses (see below) but in our example, you would always use FALSE. In fact, whenever you want 'exact match or nothing', use FALSE.

Some points on VLOOKUPS:
1) If you have duplicates in your first column, the vlookup will always bring back the data from the first instance. So check for duplicates if you're not getting the answer you expect.
2) If you are going to be copying the formula into a number of cells, make sure your table data is 'locked' into the formula. So select your table co-ordinates and press F4 to get the little $ signs.
3) Spaces matter in vlookups. If you download or import data from a reporting system, it is quite likely there may be a space at the end of your data. This will prevent your vlookup from working. Delete the space, et voila.
4) If you are not quite comfortable typing in the formula freehand, use the formula function box to help you the first couple of times. After that, you will probably find it much easier just to type it yourself.

So, back to the issue of TRUE or FALSE. Using TRUE is really handy if you want to look up a number that falls within a range of numbers. So if you were grading papers and wanted to give students an A-F grading based on their % marks, this is a handy tool to use.

Similarly, if we wanted to give our characters a 'goodness' rating, we could do the following (I've added the formula in at the bottom again for explanation):


This formula works by first checking for an exact match. If it can't find it, it will look for the next LOWEST number. So your table always needs to be in ascending order. In our example, Mother Gothel has not yet reformed her ways enough to even be classified as Misguided (40%), so she goes straight into the Evil & Twisted basket.

So, that is your basic VLOOKUP. These formulas are normally used for extracting data from one table into another, but can be used for just about anything. I've kept it simple for ease of explanation, but you can add in all sorts of other functions to make these formulas truly perform magic.

In our newsletter this week, I will be explaining how an HLOOKUP works...why not subscribe?

Happy Spreadsheeting!


--

Thursday 7 June 2012

How do I sum thee? Let me count the ways.....

Do you know what I really love about Excel? The fact that everyone uses it differently. There are so many ways to reach the same answer, and people will use the techniques that they have been taught, or find the easiest.

Take the simple task of adding totals and subtotals to data, for example. I had a debate with my better half the other day about this very subject. He went for  the subtotal function. I went for pivots. It wasn't too heated (because hopefully we aren't that sad yet!), but it made me think about all the ways you could do it.

So here are some examples of how to get sub totals and totals for data with different categories (and bear in mind these are not all of the ways to do it... but we don't have all day!). Please note too that I am going to ignore the most simple and obvious choice of the sum function. Pure snobbery, but we have to have standards...

Our test data for today is riveting...unit sales on widgets, sprockets and gadgets:


Subtotal Formula Function
If we side with my better half, we can get subtotals for each product by doing the following:

 - Select all the data, and sort it by Product.
 - Insert lines between each product type
 - Add the sales for each product grouping by using the subtotal formula function as follows:


Things to note on this method:
 - When typing the Subtotal formula, note that the 9 (or you can use 109) refers to 'adding'. If you type in" =subtotal(" into your spreadsheet, you will notice a little drop down box appears giving you all sorts of lovely options, like average, max etc. The number you type in directly after the open brackets will determine what magic your formula will perfom.

 - The formula in the total line at the bottom will ignore all the subtotals above it. Handy.

Subtotals with grouping

An easier way to do this is click on the data, go to your data tab, and select the Subtotal button. You can then select what type of formula function you want, for which columns and hey presto, you have the same as above. You also get little brackets along the left side of your sheet that you can open and close for ease of viewing the totals, like below:



SUMIF Function

Has anyone seen the issue with this method yet? Well, one of the issues anyway...What happens if we want totals by colour too? You have to rearrange everything. Or, you can do this:


By using the SUMIF function, you can create sub totals by product or by colour, without having to touch the data. I've shown the formula above for clarity, but what it is basically saying is "Look in column B for any cells that match "Widget". Where they do, add up all the corresponding figures in column D". You can then drag this formula across to get the totals for all your products and colours.

The plus side of this is that you don't have to rearrange your data to get totals for different categories, and it is easier to add more data.

Pivot Tables

In my mind, this is still the cleanest option. By using a pivot table to get subtotals and totals, you can cut and slice your data however you choose. A basic pivot of the data would look like this:



By using the Pivot Table Field List, you can then choose whatever options you like for your pivot table by mixing it up.



For example, if you wanted these totalled by colour, simply swop the positions of Product and Colour in the Row Labels box.

This is a basic Pivot Table, because they deserve a whole blog on their own. But if you would like to see how I would mix this data up even more in a pivot, why not subscribe to our newsletter?

At the end of the day, you may say To-may-to and I may say To-mah-to, but we'll both be happy with our way of doing things. I just hope this has shown you a couple of other ways to try add it all up....

Thanks for reading, and happy spreadsheeting!