Welcome to Excel Magic Trick number 1,311. If you want to download this Excel workbook, Excel Magic Trick 1,37 to 1,311, and follow along, click on the link below the video. We have a great video here. We want to be able to select a particular item number. If I select this one here, instantly I want to see all of the items associated with that item number and the total. So you can see right now we have Fast Catch listed there, and then the next one Quad listed there. So all of them, and then the total for all of those items from this column.
I'm going to click on the sheet 1311. Here's the same data set, and we want to get all the items and then the total sales number for this item number. The first thing I need to do is use COUNTIFS to count from the item number column, comma, how many of these item numbers we have. When I hit Enter, we have four of them. Now that we know there are four of these item numbers, that means we need Sunshine, Bellon, Trifly, Quad, and then the numbers. The problem is we're looking up something, and anytime we have duplicates like this, the lookup functions are just not built to do this. So what we really need is the INDEX function. We need to know that Sunshine is in a relative position, Bellon's in the fifth, Trifly is in whatever that one is, and Quad is in the very last one. So in the formula we actually have to generate these relative positions.
We're going to start with that formula element. I'm going to use the ROW function and then the reference argument. I'm just going to highlight every single item, which is a function argument array operation. Of course, if I highlight ROW and press F9 to evaluate this, it gives me the actual row numbers 2 to 17. That's not what I want. I actually want 1 to 16. Ctrl+Z. Now I'm going to subtract a single cell, and I actually learned this from Bill Jelen. If we have a proper data set, we can simply click on the field name at the top. All this does for this row right here is say I'm in row one. So 2 minus 1, of course, is 1. If I highlight this whole little bit right here and press F9, that's an array of relative positions.
Now we need to filter out. We're only interested in 2, 5, and the other two that represent our items. So I'm going to close the parentheses and divide by another array calculation that asks, how many of you in here are equal to this? I actually forgot to lock all of these because we're going to copy them down, so I'm going to highlight them and press F4. Watch this. I'm going to highlight all of these and press F4. That gives me a series of TRUEs and FALSEs in the denominator. When I evaluate this entire thing with F9, dividing by zero will filter out all the relative positions we don't want. There are our 2, our 5, our 13, and our 16.
As I copy this formula down, I need to pull out the first smallest, second smallest, third smallest, and so on. Ctrl+Z. I'm going to use the AGGREGATE function, and in the very first argument I'm going to use SMALL, so function number 15, comma. I need to ignore the divide-by-zero errors, so I use 6 in the options argument, comma, then the array, which is that whole thing with our relative positions, comma. For K, I need a formula element that gives 1, 2, 3, 4 as I copy down. So I use ROWS, and I'm sitting in F8, so I'm going to say F$8:F8. This is an expandable range. How many rows are there from 8 to 8? One. The first cell is locked, but the second one is not, so as it goes down it will expand, and ROWS will give us 2, 3, 4, and so on. Now I can close the parentheses, and the ROWS for K will do its job. Ctrl+Enter. It looks like I have number formatting there, but I won't worry about that. I'm going to copy this down. There it is: relative positions 2, 5, 13, and 16.
Now we can throw that into the INDEX function. I'll go to the beginning of INDEX and the array, which are the items we're trying to look up. I'll press F4 to lock it, then comma. The row number is the output from AGGREGATE. Close the parenthesis, Ctrl+Enter, double-click, and send it down.
Now we need to do two things. We need to get rid of the #NUM! errors, and we need to add the total row, which is 4 + 1. I'm going to press F2 and build two nested IFs. I already have part of what I want, which is the ROWS piece. In the IF logical test, I'll say when the number incrementor is less than or equal to four, and I'll press F4 on the count. If that's true, then the value if true is our lookup formula. I go to the end and add a comma, since there are still two conditions left, so I need another IF. In that IF, I'll check whether the ROWS value is exactly equal to the count of four, F4, plus one. If so, what do I want? I want SUMIFS, with the sum range being all of the sales values, F4, comma, the criteria range being all of the item numbers, F4, comma, and finally the criteria being this item number, F4 locked. There's our SUMIFS. I close it off, and it sits in the value-if-true slot, which will work fine. Otherwise, for the #NUM! error, we want an empty text string, which is a zero-length text string that shows nothing. Close the parenthesis.
By the way, this array formula doesn't require Ctrl+Shift+Enter, because although there are a bunch of array calculations inside AGGREGATE, the AGGREGATE array argument is built to handle them without Ctrl+Shift+Enter. I go to the end and add one more parenthesis, press Ctrl+Enter, double-click, and send it down. That is an amazing formula. Now, if I change the item number, there it is: all of the items and the total. We'll see you next trick.
