Search Not Just Numbers

Loading...

Tuesday, 10 May 2016

Excel Tip: Adding up columns based on multiple criteria (the SUMIFS function)

Before getting into today's post I want to point you to an excellent free Webinar being offered (for a limited time) by Mynda Treacy, entitled "Creating Excel Dashboards". Mynda is a real expert on Excel Dashboards and her training materials are always excellent. You can register for the webinar here.

I realised the other day that I had never covered one of my most used functions on this blog - SUMIFS. I have covered its predecessor, SUMIF, as SUMIFS has only been available since Excel 2007.

Although SUMIF is still available in later versions of Excel for compatibility purposes, it is essentially redundant, as SUMIFS does the same thing, plus a lot more.


Let us look at an example of some sales data (see left).

Say we want to know how much Mary's sales were, or how much Sarah sold in the East Region, or even how much Ben sold in the North region in the month of January.

SUMIFS can do all of these.

The syntax for SUMIFS is as follows:

=SUMIFS(SumRange,CriteriaRange1,Criteria1,[CriteriaRange2],[Criteria2].....)

You can have as many pairs of CriteriaRange and Criteria as you need. The function works as follows:

SUM SumRange where CriteriaRange1 = Criteria1 and CriteriaRange2 = Criteria2 etc. for however many criteria you have.

For all of the examples above the SumRange will be D2:D21, as this is the range we want to sum, subject to our criteria. We will look at how we construct the rest of the formula for each of our examples above.

How much did Mary sell?
Here we only have one criteria:

CriteriaRange1 = C2:C21
Criteria1 = "Mary"

=SUMIFS(D2:D21,C2:C21,"Mary")

returns £16,853.

How much did Sarah sell in the East Region?
This time we have two criteria:

CriteriaRange1 = C2:C21
Criteria1 = "Sarah"

CriteriaRange2 = B2:B21
Criteria2 = "East"

=SUMIFS(D2:D21,C2:C21,"Sarah",B2:B21,"East")

returns £1,085.

How much did Ben sell in the North Region in the month of January?
This time we actually have four criteria:

CriteriaRange1 = C2:C21
Criteria1 = "Ben"

CriteriaRange2 = B2:B21
Criteria2 = "North"

CriteriaRange3 = A2:A21
Criteria3 = ">="&DATE(2016,1,1)

CriteriaRange4 = A2:A21
Criteria4 = "<="&DATE(2016,1,31)

There are two elements to these last two criteria that need further explanation.

The first is that if our criteria is anything other than equals, we need to include the criteria in inverted commas, for example ">23", or "<=15", to make it a string. If rather than 23, we wished to refer to a cell (say G5) we can use the ampersand (&) to join two strings together, e.g. ">"&G5.

The second is that if we wish to refer to a date directly, we need to refer its sequential number which we can calculate using the DATE function. The three arguments for the DATE function are Year, Month and Day, so to get the date sequence number for 1st January 2016, we can use DATE(2016,1,1). Note that if we entered 1/1/2016 in cell G5, we could just use ">="&G5 for Criteria3, as the cell value when you enter a date, is its date sequence value.

Our function is therefore:

=SUMIFS(D2:D21,C2:C21,"Ben",B2:B21,"North",A2:A21,">="&DATE(2016,1,1),A2:A21,"<="&DATE(2016,1,31))

which returns £4,007.

In most real situations we are likely to have all of the criteria in other cells, as we are usually doing more than one calculation.

With careful planning and smart use of dollar signs, you can structure your formula so that you only need to write it once. For example if we wish to populate the following grid from our data:

If we put the following function in cell J2, we  can copy it to all of the other cells:

=SUMIFS($D$2:$D$21,$C$2:$C$21,$I2,$B$2:$B$21,J$1)

We have used dollars to fix both the rows and columns of the references to the data table, as these should not change, no matter what cell that we are in.

For our criteria however, we want those to change between cells, so we have fixed the column of our name criteria ($I2), as we are always going to look to column I for the name, but want it to change as we change rows. Similarly, we have fixed the row of our region criteria (J$1), as we always want to look at row 1, but want it to change with the columns.

Hopefully this gives you an idea how flexible SUMIFS can be.


If you enjoyed this post, go to the top of the blog, where you can subscribe for regular updates and get two freebies "The 5 Excel features that you NEED to know" and "30 Chants for Better Charts".

Tuesday, 5 April 2016

ICAEW Spreadsheet Capability Framework - your opinions?

As you may be aware, I am a member of the Institute of Chartered Accountants in England and Wales (ICAEW) Excel Community Advisory Committee.

An important piece of work that the committee has been working on is what we are (currently) calling the Spreadsheet Capability Framework.

This document is intended to provide a reference point that spreadsheet users, employers and job applicants can use to discuss and assess competence in spreadsheet use - beyond glib phrases on a CV, such as "proficient with Excel".

Given the authorship of the framework, it is obviously primarily aimed at those in the finance field, so apologies to my non-accountant readers.

The framework has now reached the stage where we are actively seeking opinions outside the committee.

To this end my colleague, David Lyford-Smith has posted a copy of the document to the ICAEW's IT-Counts website, where your comments would be greatly appreciated.

If you would like to read the framework document and/or add your opinions to the debate, please use the following link:


http://www.ion.icaew.com/itcounts/post/Spreadsheet-Capability-Framework---exposure-draft-seeking-your-comments

Whereas comments on my blog are always welcome, and will be passed on, I would recommend commenting on the IT Counts post instead to contribute to the wider debate.

If you enjoyed this post, go to the top of the blog, where you can subscribe for regular updates and get two freebies "The 5 Excel features that you NEED to know" and "30 Chants for Better Charts".

Tuesday, 8 March 2016

Excel Tip: Using Index to calculate a cumulative sum (e.g. a Year to Date total)

Let me start with an apology. My current workload has meant that my posts have been a lot less frequent in recent weeks. This may continue for a while, but I will still be aiming to post at least once a month during this busy spell.

In this post I want to show you how to do something I have shown before using OFFSET, but this time using the more efficient INDEX.

Let's look at the example of a 12 month budget spreadsheet with the monthly sales figures in cells B9 to M9 (month 1 in B9, Month 2 in C9, etc.). And let's say that the current month number is entered in cell B6.

Using OFFSET we could return the current month's sales using:

=OFFSET($B9,0,$B$6-1)

and the cumulative sales using:

=SUM(OFFSET($B9,0,0,1,$B$6))

See this earlier post, if you don't understand why.

The problem with OFFSET though, is that it is what is known as a "volatile" function. This means that it always has to be recalculated when any cell in the spreadsheet changes - as it does not specify a range that it is dependent on, therefore does not know whether a change might affect its result.

INDEX however looks at a defined range, making it significantly more efficient, and in its simplest form can be used to replace both of these examples of OFFSET.

For a range the width of a single cell (as in our example), INDEX only needs two arguments to return the current month's sales:

=INDEX($B9:$M9,$B$6)

This returns the value of the cell in position B6 in the range B9:M9.

If $B$6 is 3, this will return the value in cell D9, being the third cell in the range B9:M9.

To do the cumulative calculation, we can use the same INDEX function to return the end of  a SUM range, while fixing the start:

=SUM($B9:INDEX($B9:$M9,$B$6))

Again, if B6 contains 3, then this returns the sum of the range B9:D9.

In both cases, these will only recalculate if a cell in the range B9:M9 is edited. In a complex spreadsheet with many calculations, this can make a huge difference to calculation times.





If you enjoyed this post, go to the top of the blog, where you can subscribe for regular updates and get two freebies "The 5 Excel features that you NEED to know" and "30 Chants for Better Charts".

Tuesday, 9 February 2016

Excel Tip: Controlling what is printed from a sheet

One of the challenges that many Excel users face, is ensuring that the printed version of a spreadsheet looks as good as it did on the screen.

There are a few simple things that you need to know to help to organise what actually gets printed and how it is spread across pages.

I will cover the first of these today, and revisit some of the others in future posts.

Today we will look at how to determine how much of the sheet is actually printed.

Left to its own devices, Excel will print all of the used area on the sheet, Specifically, it will print the whole area from cell A1 to the rightmost column and the last row that are not empty.

This may not be what is required, and it is easy to change. Simply highlight the area that you do want to print, then go to the Page Layout ribbon, click the Print Area icon, and select Set Print Area.

You can even select multiple ranges (holding down the Control key, while you select the subsequent ranges) which Excel will print on different pages.

For example, you may have a Profit & Loss and Balance Sheet alongside each other on one worksheet. Just highlight the area of the Profit and Loss (say A1:H100) and then hold down the Control key while selecting the Balance Sheet range (say J1:P80) and Set Print Area as described above.

Once a Print Area has been set in this way, you can see (and edit it) using the Name Manager on the Formulas ribbon. You will see all of the Print Areas in the workbook listed there and (scope to their particular worksheet). You can then edit these ranges like any other.

You can even make them a dynamic range using functions like OFFSET and INDEX, so that the area that gets printed is determined by values in the workbook.

A particularly useful technique is to combine this ability to set a dynamic Print Area with a formula to determine the last row of data. I explained a formula to do this in this earlier post.

Let's say we put this formula in a cell that we name LastRow.

If we want the print area to be columns A to J but the height of the print area to flex to the amount of rows occupied, we can enter the following as the Print Area (in the Names Manager):

=OFFSET($A$1,0,0,LastRow,10)

This will print a range starting at A1, with a height of LastRow, and a width of 10 (A to J is 10 columns).






Free Excel Dashboard Webinar
If you enjoyed this post, go to the top of the blog, where you can subscribe for regular updates and get two freebies "The 5 Excel features that you NEED to know" and "30 Chants for Better Charts".