transcript

excelisfun - Excel Magic Trick 1117: Calculate Average For Each Sample: AVERAGEIFS, PivotTable, or OFFSET

Thumbnail of the video “excelisfun - Excel Magic Trick 1117: Calculate Average For Each Sample: AVERAGEIFS, PivotTable, or OFFSET”
Download Markdown
Community upload. AI output has not been independently verified.

Welcome to Excel Magic Trick number 1,117. If you want to download this workbook, click on the link below the video. In this video we have some numbers and some samples, and we want to calculate an average for each one of the samples. It turns out that each one of these samples has three numbers in it. We actually did a video with this same data set back in 116 and saw some cool tricks, but here we simply want to average, and there are some really easy built-in methods for doing this.

The first one is the pivot table. As long as we have field names at the top, records in rows, meaning sample number and number, we click in a single cell and insert a pivot table, or we can use the keyboard: in 2013, Alt, N, V; in 2010 and 2007, it's Alt, N, V, T. All right, so we're going to do it and say on this existing sheet, and we're going to place it right in between a lot of other stuff. This is dangerous, why? Because pivot tables can pivot and wipe out other data, but I'm going to keep it contained right in this area right here, so I'm going to click OK. Now I'm going to drag the sample number down to Rows, and instantly I have a unique list of all the samples. When I drag the number field down to the Values area, it will default to Sum, but no problem. Right-click, and I can go to Value Field Settings, which is the one at the bottom of the shortcut menu. Now we want to summarize values by, and there's a bunch of functions. Average. I'm actually going to go to Value Field Settings, and we can change the name, but it will change automatically. Watch this: when I click the function, it changes the name up here. Now I'm going to click on Number Formatting, because I want to show only one decimal. Notice our data set has, looks like, three decimals. Number Formatting, Number, one decimal. We don't need to use the comma. Click OK. This is only number formatting; it actually won't change the source numbers. There may still be some decimals hidden underneath, but when I click OK, instantly there are the averages.

If I want to do a slightly different method, in 2007 and later, use AVERAGEIFS, especially if the sample numbers are sequential, it's easy to put in numbers. Now we have our criteria from this column that will help us pick our numbers from this column and average them. Equals AVERAGEIFS. I'm going to use the Excel 2007 and later function, because I like the AVERAGEIFS, COUNTIFS, and SUMIFS, since their ScreenTips argument names are a little easier to understand. The average range is those numbers: click on the top cell, Control+Shift+Down Arrow to get all the way down to the bottom, F4 to lock it because we're copying and we need the dollar signs, then comma to get to the next argument. Criteria range: same trick, Control+Shift+Down Arrow, F4 to lock it, and finally comma. The criteria means: from what's in this column, do you want to decide which numbers we're going to use? That's a relative cell reference, the sample number. Close parentheses, Control+Enter, double-click and send it down. We get the same answers, and we can hide the decimals here. In fact, if you really wanted to use the source numbers with only a single digit, we could chop off all these extra decimals, because when you use number formatting they're still there, and they might mess up a calculation. I'm going to hit F2 on that active cell to put the formula in edit mode, and I'm going to use the ROUND function. The ROUND function allows us to round to a particular digit. Yes, we have a big formula there. I come to the end, and when I type comma, I can see the ScreenTip saying, what number of digits do you want to round to? We want to round to just a single decimal. The number of digits you count from the decimal, this way: 1, 2, 3, and that tells you what position you want to round to. We want the tenth position, so we put a 1, close parentheses, and now when I Control+Enter, it takes this edited formula and populates it all the way down.

So there we go. The advantage of the pivot table is that it is easy and fast. Actually, the "Row Labels" header is really annoying, so let's click inside our pivot table, go to Design, Report Layout, Show in Tabular Form. That lists a much better label at the top, so instead of "Row Labels" it shows the column header name, or the field name, so "Sample" is much better. The pivot table is quick and easy, but formulas will update if I change this or if the data changes. If I change this to 55, as soon as I hit Enter the formula updates, but the pivot table does not. No problem, it's easy to update: you simply right-click and Refresh, and there we have it. Now I'm going to Control+Z and Control+Z to undo both the refresh and that first number for the first sample.

Now what if we didn't have this sample column here? Notice the criteria: the pivot table and AVERAGEIFS needed the number 1 to determine where the ones are, to pick out the numbers. If you didn't have this column over here, how in the world would you do this? Well, if it's really every third item, we can do that. Back in 116 we saw some great tricks for what to do when you don't have this extra sample column, and we're going to use one of them right here. We need the number for the first number, position 1, and down here we need position 4. When we get down to three, we need position 7, and so on. So if there's a way with a formula to create 1, 4, 7 as we copy down, then we could use the OFFSET function to create a dynamic range. We're going to use our old favorite, the ROW function. I'm sitting in J5, so I'm going to type ROW, then J, dollar sign, 5, which is J5. The ROW function counts rows. Right now it says how many rows are there from 5 to 5: there's one row. Notice that 5 is locked and this one is not, so Control+Enter, and when I copy down, that's an expandable range. You can see 5 is locked but now the 8 is not. Now 1, 2, 3, 4, 5, that's not going to do it. I'm going to have to get a little tricky here, so I'm going to put parentheses around this and subtract one, because I need to start at zero for this trick. Double-click and send it down. Now I'm going to F2, and I can multiply times the increment. Remember, it's every three items, so I'm going to multiply by three. What's 0 times 3? 0. What's 1 times 3? 3. What's 2 times 3? 6. Control+Enter to populate that all the way down. We're almost there. We just need a starting position, so we simply F2 and add one, and now instead of 0, 3, 6 we'll get 1, 4, 7. Control+Enter to populate that all the way down. That's the hardest part of this.

Now we have to look at OFFSET. In essence, this tells the OFFSET function, which will create a dynamic range, which relative position in this column of numbers we want to start from. We're going to start our range at one and always go down three, and then the next one will start at relative position four and go down three, and then seven and go down three. Are you ready for the magic of the OFFSET function? OFFSET. When I hit Tab here, watch what happens: the parenthesis is already there and is used in this formula, and the function steals that parenthesis, so you're going to have to type an extra one. The way OFFSET works is you need to give it a reference, which is a starting point. I'm going to start with the field name right above the first item, and press F4 to lock it because we're going to copy this down. Comma, and then the rows argument says from that starting position, how far do you want to go down? 1, 4, 7. Notice OFFSET is going to go down that many rows from the reference, the starting position, and that rows value gives the new starting position. Now I'm going to come to the end: comma, columns, from that starting position. We don't need to move right or left any columns, so we leave that empty, comma, to skip over it. The height of this dynamic range is always going to be three. The width we don't need, because it will assume the width of the reference, which is one column. That's all we need. Now OFFSET creates a range. If I entered this now and copied it down, it would give me a #VALUE! error or some number from implicit intersection. But check this out: we can go to any particular cell here, highlight it, and hit the F9 key, the evaluation key, to see what it actually got. One, two, three. Are those the right numbers? They sure are: one, two, three. That is magic. OFFSET has created a dynamic range, and as we copy it down, it's always getting the first three, then the next three, then the next three. Now I can simply put this into the AVERAGE function, and guess what? The AVERAGE function will have no problem with the values that OFFSET delivers. OFFSET is delivering a range of three values. I'm simply going to Control+Enter, double-click and send it down, and I get the same exact values without having that sample column.

There are other ways to do this. If you want to look at it, OFFSET is a volatile function, which means if you have many formulas, huge data sets, or lots of ranges in your formulas, for example if you had 500 samples, OFFSET recalculates all the time and might slow down your workbook. If that's the case, you might have to switch over to creating dynamic ranges with INDEX. I'm not going to do that in this particular video, but there it is. So if you have a data set with sample numbers and some numbers, pivot tables and AVERAGEIFS are the way to go. If you don't have that sample column and you need every three items to calculate an average, then you can use OFFSET inside of AVERAGE. All right, we'll see you in the next video.