1 Formulas: Index, Match, and Indirect Hello and welcome to our next lesson in this module on formulas, lookup functions, and calculations, and this time around we're going to be extending what we talked about last time with lookup functions, and looking at sort of a different variation on lookup functions. Last time around we've looked at VLOOKUP and HLOOKUP, and saw how you could basically look up the value, and match it to something in the left-most column of a table or the top-most row, and then go down a certain number of rows or to the right a certain number of columns, and then find something. And this time around we're going to be focusing on two other related functions called INDEX and MATCH. Now the difference between these and the other lookup functions is that you do not need to know the position of the row or column in advance. You also don't need to go from left to right or from top to bottom, so in general they're a lot more flexible then VLOOKUP than HLOOKUP. [01:00] The tradeoff is that they're a bit more difficult and time-consuming to write. But really all things considered, they have a lot of advantages, not just this. Arguably they're faster in some cases, because excel doesn't recalculate them as frequently, when you're manipulating spreadsheets. So if you're working with a massive data set, index and match tend to be the way to go. Now in addition, we're also going to take a look at INDIRECT and ADDRESS. These are two functions that a lot of people don't know about, and don't know how to use very well. But if you can use them effectively, they're going to give you a massive time-savings and advantage, especially when you're manipulating large datasets. So that's what we're going to focus on here. Now I'm going to start by just showing you some very basic applications of INDEX and MATCH. Once that's set, then we're going to go in and start rewriting some of these functions that we looked at previously. Like the monthly sales by sales by sales rep, year, and month. We're going to go in and fix that. I've already taken the liberty of fixing this one by region, because this one is really just adding to the SUMIF formula here, so there isn't too much to say.
2 [02:00] But we are going to fix this one, and I'm going to show you a couple different ways to rewrite it and get the equivalent result. Once that's done then we re going to go through an exercise, which will be quite challenging with an analysis of a REIT, actually several REITs, real estate investment trusts, and you can see the assignment right here. Basically, we're trying to compare the performance of different REITs, and the challenge here will be that the data we need is spread across many different worksheets. And to make it even more fun, the columns are in different orders. The geographies, the cities here they re not all the same, they're different between each different dataset. And so, if you try to use VLOOKUP up in this case, it just would not work. You'd have to make such a complicated formula and it would be very different for each different file. But we'll see how when we use INDIRECT, INDEX, and MATCH we can actually do this with a single formula. So let's go back. I'm going to show you some very simple examples of these formulas first. Let's start with INDEX, so INDEX just returns the value of an array at a specific row and column number. So what does this really mean? Well, let's just take a simple example here with our summary table. [03:00] Let's say I want to get the value in the second or third row, I ll say the second row 2. And then for the column number, let s also say 2, so you want the value in 2-2 here, and sure enough that gets us the $3,195,073 right there. So if I want too I can go in and fix this formatting, so you can see it. And you can play around with this, you can try entering other numbers, but that's basically what it does, and it's giving me a #REF! error there, because I entered one to many columns. Now by itself if you just use INDEX function like this, you might think it s useless. The real way that you use INDEX function is you use it in conjunction with this other function called MATCH. What does MATCH do? Well, you enter MATCH, and it basically allows you to look up a value and then find where it is in a row or column of data. [04:00]
3 So here s what we can do. Let s say we want to look up the value of $2,017, and then find the associated sales or order size associated with that. So let's enter $2,017 and then for the lookup array we want to enter just this column that I ll actually be in, and I can highlight that and then press F4 or CMD + T to anchor it. With the match type, let s just go with an exact match. Okay actually, the real problem is that this is a date field right here, so this is entered as 1/1/2017, so let me try fixing this, and now this is actually good, because it illustrates some of the disadvantages, and things you have to be careful of with these LOOKUP functions. [05:00] It's still not working, so let's try date value, and finally that worked. So you can see that sometimes you need to change around a few things, and manipulate these numbers and change them between dates and texts, and numbers and texts, and so on. But bottom line is you just have to make sure that if you're looking up values in a column or row of dates, that you're also using a date value in Excel as we just did here. So that's how you can use MATCH function, and likewise let's say you want to find something at the top, such as order size in this case. What you could do here is you could say something like MATCH, and then order size, so that's the value we're looking up. And then the lookup array is simply this top row of this table, and then, once again, we re going to say exact match here. And we can see it tells us it's in column 3. So you can now imagine how these functions could be very useful together, because now what we can do is use the INDEX function, and combine what we have here. So now we can say INDEX, and let s take this entire area right here to be safe. [06:00] Anchor it with F4 or CMD + T, and then for the row number I'm just going to enter 1/1 for now, because what I actually want to do is press F2 or CTRL + U to go in here, and I want to get the row number from this MATCH function. So let s pull it in from there. So we have that, and now let's enter this function for the column part of this. So now what its saying is basically, we want to get the value at the intersection of January 1st 2017, and then whatever column corresponds to order size, whatever row corresponds to the
4 $2017, we want that number. So let's press enter and sure enough we don't actually get what we want here. So why is this happening? Well, it's because we've made a mistake, which I've done sort of intentionally, but also to show you one of the things that you have to be careful of with this function, which is this. If you look at how we're setting this up, we're going from B8 to B17. [07:00] But in all of these other ones we're going from B7 to G7 or B7 to G17. We've forgotten this row at the top, and so what we need to do is change this, and change this to B7 to make sure that whenever we use INDEX and MATCH, the rows and columns that we're searching for are directly in this table. We want to make sure we re not coming up too short, or including an extra row, or extra column. So now we get the correct value, the $151,536 right here. So those are a few things you have to be careful of when using these functions, and hopefully you re more aware of these points after seeing some of the mistakes I made, even after using these functions and Excel and being quite familiar with Excel for a long time. So that's how you use INDEX and MATCH function, and we're going to go into some other examples and rewrite this formula using these functions. But one other thing I want to show you, actually two other things I want to show you first are how to use INDIRECT and ADDRESS. So INDIRECT, what does this do? [08:00] This is a little bit complicated to explain, but basically what it does is the following. I'm going to enter INDIRECT, and then I'm going to enter reference text here. And what this does is allow you to actually refer to a specific worksheet and cell name from within this very function. So I could enter something like the following right here. I could say INDIRECT and then orders to correspond to our orders tab over here, and then close the parenthesis, ampersand, and then exclamation point, because we're going to another worksheet. And then I could just say in double quotes G6, and that gives us Rhode Island, the state abbreviation, which is actually right here, and so it's literally pulling cell G6 from right here.
5 So why would this be useful? In what cases would this be useful? We have one of these cases in this very worksheet, which is the following. You can see how all these tabs with the monthly data are labeled 2020 monthly, 2019 monthly, 2018 monthly. [09:00] Now if we wanted to go and pull all of this data ourselves we'd have to write some very manual functions that require us to change the name of the worksheet in each function. But what we can do with INDIRECT is the following. In a function like this we can simply look up the year that we've picked up here, and then change the worksheet name. Actually change the name of the worksheet that we're pulling the data from, and pull it from monthly, 2019 monthly, 2018 monthly, and use the INDIRECT function to automate that whole process of swapping in different worksheet names. Now I realize you may not completely understand it, based on my description right now. So here's another example of how to use it, and maybe this will make it more clear. So let's say for example that we wanted to pull in data from 2020 monthly. So I m going to leave it at that, and okay this is giving us a rough error. And we do have a cell G6 here, so let s see what s going wrong. [10:00] Okay, so if we look at this function more closely, it looks like the problem here is most likely that we've forgotten single quotes around this, so if you press F2 or CTRL + U on the Mac to go ahead and enter it, let s add single quotes around the 2020 monthly, and now we can see hopefully that it gives you the correct value. It does, $1,338,174, so we have that. And you can see how with this formula, essentially what we could do is swap in this 2020, and replace it with anything we want really and as long as it's correctly formatted and everything, then we're going to move on to the next spread sheet, in each case. Now a related function here is called ADDRESS. What you can do here is the following. You can just enter a row number and a column number and then get a cell reference. So I can just enter 2 and 5 here and then it gives me cell E2. So how is this useful?
6 [11:00] Well, in some cases it may not always be obvious, which row or column you need to be in, but maybe you can get the row number or column number you need to be in from some other type of dates or other dataset that you're looking at. So ADDRESS is probably not quite as useful as these others, but depending on your data could still be very helpful. I just wanted to bring it up here, because it is something you might see, especially in advanced data manipulation files. So let's go to our initial task here, and try to rewrite this monthly sales figure, using INDIRECT and INDEX and MATCH. So basically, what we have to do is make sure that we incorporate the year this time around. So let's just double check the formatting. It's a custom format, but it looks like it s following a date format, overall right here. So rather than using SUMPRODUCT here, let s just delete this. And let's just start with a very basic INDEX and MATCH function, just for the 2020 monthly data, and then we can try to expand it and make it cover all of the different years. [12:00] So I'm going to type INDEX here, and we're going to go over to the array and question. So sales rep ID and take all of this data right here, and then for the row number, what do we want? Let's type the MATCH function, and then for the value that we're looking up, remember we're trying to match the sales rep ID here, so we want this value. And then the lookup array, we want to take this column right here, which I've already named, and then the match type we want an exact match. So we have that. And then for the column, let's enter a MATCH function, once again. And for the lookup value for the column, we want to enter the month right here. For the lookup array we want to enter the list of months here at the top. Make sure to include the sales rep ID, that field in the beginning, otherwise it's not going to work correctly. We want an exact match, so I ll say zero, and we actually have everything we need for INDEX. So let's close those parentheses and press enter, and see what happens. [13:00]
7 So this actually works correctly, and you can test it out yourself. If you look at this number here we have June 2011, actually just June in this case, because we're ignoring the year. So we have sales rep ID of 1 and month of June. Let's go over and take a look at that, and its $251, $239. And you can change this, play around with it yourself, but this works correctly, because our ranges here for the MATCH functions are both exactly on the edge of this table, or at least within this table. They don't go beyond it in terms of rows or columns and they don't go below it. They re exactly in line with the table, which is what you want. So how can we rewrite this to make it more flexible? The first thing we have to do is realize that unfortunately, we're going to have to get rid of all of these named cells here. So let's just start with something completely different, and let's just write a simple function here. Let's just say INDEX, and then for the array, to access this now, we're going to have to use the INDIRECT function. [14:00] So let's say INDIRECT and let s select the year. And then we want to use an ampersand and then monthly. Now for the range here, let's go take a look and make sure we're getting the right thing. So let's just go over, and I entered another ampersand, let s just go over to the 2011 monthly, and see what the range here might be. So we could do this in a couple of different ways. But probably the safest thing to do would be to take this whole range, B3 down all the way over to N12, and use that as our range. Now of course, we don't want this 2011 monthly, because we want it to be flexible and handle all different cases, so let's just get rid of that. And what we can do here is also get rid of that ampersand, and so we have that range. And now instead of entering anything right away, I m just going to leave that range here. And I'm just going to try one, let's just try 2 and 2 just to see if this actually works correctly. [15:00] And no it does not. We get a rough error so something has gone wrong. Okay, so what might be going wrong here? Well, one helpful thing to do in Excel is to go to show calculation step, when
8 you see an error like this. And you can see here the problem is that it's not looking it up in the right work sheet, because it's defaulting to instead of the date So why is this happening? Well, because we're using the date here, but in here we're using text. So what we have to do is use the TEXT function here, and use format text, and change this to YYY year. So let's see what's going on here. So a couple of things could be happening. We fixed the problem with the text and the dates, but with INDIRECT we need the single quote in the beginning, and then at the end we also need a single quote. It looks like right now, we have two single quotes so let me change that. And now we can at least verify that our INDEX and INDIRECT function works. [16:00] Again, you should see from my own circle is getting this to work correctly that it can be tricky, and you can make a lot of silly mistakes here, and Excel can be very picky what it accepts, so again, just one of the downsides to be aware of with this function. Now what else do we need to do here? The first thing we want to do is also make sure that these ranges are anchored, so that the N12 and then the B3 here, these should be anchored, because we're going to end up using them again and again, for the row and column names here. So let's start with the row number. How can we do this? For the row number we want to go, that's going to be the one that corresponds to the sales rep. So the lookup value is going to be the sales rep ID, the lookup array is going to be for This box over here and the match type is going to be zero. Actually I did that wrong. [17:00] It should be B3 to B12, and then match type of zero. So how do we translate this function into this INDIRECT formula? Actually/ it's not too complicated, because what we can do is take this whole formula, which I'm just going to press CTRL + X on to copy. We could put it in this row part. So we copied and pasted this function in, and now we just have to swap out the 2011 part.
9 So you have to be very careful when manipulating functions that are this complicated. Basically, we want to copy the INDIRECT part right here. And what I'm going to do is just delete this whole part to make sure we don't mess up anything when we are copying and pasting this in. So press CTRL + V to paste that in, and then we want to change this N12 to a B12, because we only want the column there, not the whole area. So that's still working, that's good news. And then for the final thing, we need to get the row. [18:00]] So thinking about the formula for that is just MATCH, June is the lookup value, and then the array if we go over to 2011, this is just this top part here, and we want zero for the exact match. So now essentially, we just have to transplant that function into this huge formula now. So let's take this two at the end. We have that and we have this newer formula in place. And now really what we really need to do here is take this part, with the direct reference to the worksheet itself, we need to swap in the INDIRECT function there instead, and still change it around, so we have that INDIRECT function. CTRL + C and then paste it in here, and time what I want to do is instead of changing the B3 part we're going to keep it at three, because we want row at the top. And there are a couple of ways to do it, but the easiest is to look up here and realize that N is the last column in all of these. Then let's press enter, and look it actually works, isn't that amazing? [19:00] Now as you can see this is an extremely complicated formula, and I'm not saying it's necessarily a good idea to use a formula like this unless you really have to. As it turns out, in this case we actually don't need to do anything this complicated, and there s actually a much simpler way to do this, using some other built-in functions that we ll get to in the next lesson. This is really just to illustrate more to you the power of INDIRECT, INDEX, and MATCH, and some of the more complex operations that you can perform with them. Now let's say I go in and change some of these. So let s say I change the year to 2017 and then change the date to September 1 st, and you can see that we can get data based on the sales rep by year, by month.
10 And if you want you can go in and check it yourself and verify that we did this correctly and everything. But it works and we've tried it out in a number of different cases now. What I always recommend doing, when you enter a function this complicated is exactly what we just did. [20:00] Go through the process of breaking apart each part of the formula into simpler steps, and check each of those. That ensures that you ll get the syntax right, and that you avoid any silly errors like the one that we encountered. So that's how you can rewrite this to make it truly flexible to incorporate the year, month, and the sales rep ID, and look up the monthly sales by all of that criteria. If you go over here, go to December for sales rep , just to check. Of course, that matches up. Now as I said there are simpler ways to do this. We'll be getting to them soon, but for now what I want to do is turn to our exercise. So let's pull this up. This INDEX MATCH and DIRECT REIT data file. So here's what you're going to have to do. You can see a description at the bottom, which I went through briefly at the beginning of the video. What we have to do here is take data for all of these different real estate geographies, so Boston, New York, New Jersey, Washington metro area, Maryland, Norfolk, and so on and so forth. [21:00] And we have to write formulas that pull in all of the data, on apartment homes, the rental rates, and these are average rental rates per apartment home, per unit really; the rental increase from one year to the next year, the occupancy rate, the rental revenue, and then the increase in that. Now for Avalon Bay, that's what the AVB here stands for, that's a residential REIT in the US. It's pretty easy. But for Equity Residential it gets harder, because the data is in a different order. So if we just tried to write a VLOOKUP function it wouldn't work, because our columns are arranged in a different order.
11 And if you look at these companies filings, if you pulled the PDF file, you ll see it really is a real problem, the data really is arranged differently in all of these, and I ve modified it a little bit, but this a very real problem, whenever you re working with large datasets like this. So essentially what we want to do is put all of these real estate investment trusts side to side, and see how they actually compare in each of these geographies, and if any of them have a certain advantage in one of these geographies. So pause this video right now and try to write this function using INDIRECT, MATCH, and INDEX. [22:00] You're going to be using INDIRECT, because you have to use the REIT abbreviation, the ticker here to refer to the worksheet, just like we did previously. And you're going to have to use INDEX and MATCH; INDEX to get the actual value, and then MATCH to match it up with the title here at the top, and also the city over here on the side. So pause this video now. Give it a shot yourself, then when you're done, or if you get stuck unpause it, and then we ll go through it together. Okay, good. So let's start breaking this down and start with some very simple examples, and then do what we did before, and break it into simpler components, and then combine the formula at the end. So let's get the INDIRECT part right first, because we're going to end up using that throughout this. So I'm going to type INDEX and then INDIRECT. And then in this case, we want to be referring to the REIT ticker abbreviation, so I m going to press F4 a few times, or CMD + T on the Mac to anchor that. [23:00] And then I'm going to enter ampersand and then underscore port, because that's how all of these are named, with underscore port at the bottom. Exclamation mark and then for the range, we want to go from the very top left corner, to however far out it extends. Now the range is different in each of these files, but to be safe we can make this 26 columns across, and 100 rows down and that should cover us, because here we can see that actually we only have around 94 rows.
12 We could be even more aggressive, and have 1,000 rows or 1,000 columns or something like that. But in this case I'm just going to say A1, have another dollar sign there to Z100, so we have that. And then we need to enter a row number and a column number. Now before we start entering these MATCH functions, let's just verify that we have the syntax here correct first. So I m going to say two and three and let s just see if this works. So did this work? Well, it's hard to tell in this case. Let s see if I can enter different numbers, and see if that actually returns something, $2,080. [24:00] So it's clearly working, it's returning this value right here, the Q4-12 rental rate. So this works, and now what we have to do is incorporate MATCH into this process. How do we do this? Well, let's think about the individual components first, before we start making this complicated. So for MATCH, let's think about the row part first. What do we want to do for the row part? We want to say basically MATCH and lookup this value, Boston, Massachusetts. We want to anchor that a few times, so that the column doesn t shift around. And then for the range we want to use that same range that we're using in the INDIRECT function right here. So that's what we're going to use for the lookup array. And then of course, match type is always zero. So let's actually go back into this other function and copy what we had here for the INDIRECT part. So we have that. And then for the MATCH part, let's type MATCH, and then lookup value is going to be Boston. Press F4 a few times to anchor the column. [25:00] And then lookup array is exactly what I have there. We're going to be using that same function for all of these. And then match type I m just going to say zero. And the reason this came up, N/A, which is another error that I just made, which is good for you to know; is the problem is that I didn't enter this correctly; I entered A1 through Z100. But remember this has to be a column not an entire area for the MATCH function. So I'm going to change this to A100 instead, and now this works correctly. So that part is there, now let's enter this. F2 or CTRL + U to edit the cell, CTRL + X to cut this out, and then let's put it in here for the row part. Let's press enter and hope this works, and it does work.
13 And then finally, for the column part let's think about this. And once again, we're going to have to use the INDIRECT function and change it around slightly. So let's just take the first one, because that's closer to what we need. So let's take this INDIRECT function, and then let's say MATCH, and what we want to do here is take the title at the top, and that's what we're going to be trying to match up to get the appropriate column. [26:00] Remember these are in different orders and at different intervals in the other spreadsheets, so it's not as if we can just assign it to a constant column. Instead we have to do this. And I'm going to anchor the row part here, the one so it doesn't shift around. For the lookup array, we're going to use INDIRECT again, except this time instead of A1 to Z100, I m going to say A1 to Z1, so that it goes all the way across the columns, the columns go from A to Z. So we have that. And for the match type I m going to enter zero, so we have that. F2 or CTRL + U on the Mac to get all of this, CTRL + X to cut it out and then let s put it right here. Highlight this, and then CTRL + V or CMD + V on the Mac. So we have that. Now does this actually work? We have this long complicated formula, although it is somewhat easier to understand than the other one we went over. [27:00] Does this work? Is it going to hold up properly? Well, let's copy and paste it across. This is the moment of truth. Let's see, CTRL + C, and then ALT + ESF after highlighting this whole area, or CMD + CTRL + V, CMD + F on the Mac. And okay, so it almost works but not quite, I misaligned this top part. So this should actually be F1 instead. So let's try this again. CTRL + C and then ALT + ESF and it looks like this is actually working properly, and so what we can now do is copy and paste this around for everything else. I'm going to zoom out by using CTRL + scroll wheel on the mouse, just to make it a little bit easier to copy and paste this down. So let's do this for all of them. And you might wonder if there's an easier or more automated way doing this. The short answer is actually, if we wanted to take the easy way out, we could just copy this, and just paste it all the way down this entire area.
14 [28:00] Because what will happen is that for anything it can't find, it's essentially just going to return a blank value. Let s do that ALT + ESF. So let's take a look at this. We do have some rough errors it looks like. Most of these are because we are looking for something that doesn't exist. Let's delete these. And then we do have some N/A errors too, but those are probably going to be easier to solve. So I'm going through and deleting these rough errors. And these are because these items literally don't exist in the spreadsheets that we're looking for them on. So we have that. And then these other errors, basically they re happening, because if you look at this, if you look at the data for Avalon Bay, AVB, they don't have anything for other markets. [29:00] So sometimes it's giving us N/A, sometimes it s giving a rough error depending on how much is actually there. There are a couple of ways you could deal with this, but probably the easiest way is to use the IFERROR function. We haven't gone through error checking yet, but you can just learn this now as a bonus. We can say IFERROR and enclose this whole thing in that function and then say just have a blank there, just double quotes if there's an error. So now let's zoom out again, and paste this around, and now this will handle all of these different cases, and also work in case we run into an error message, or in case the data is not there in the first place. And there you go. So you can go through and look at this yourself. Double check everything if you want, but that's how you can fix this. One other note, IFERROR only works in Excel 2007 and onwards, so if you still somehow still have been using 2003, it's not going to work. But I'm assuming if you're watching this it probably is not you. So what does this allow us to do? Well, it lets us see at a glance really, what types of rental increases, different REITs in the same area have had. [30:00] So if we look at this data we can see right here that there s a big variation. Equity Residential, their rates have almost gone up 10%, but Avalon Bay and AMECO/AV have only gone up by around 4.0% or 5.0%. So that might be interesting, and worth looking at more if that explains that in the Boston area.
15 And then you can go down and then do the same thing for really almost everything here; the occupancy rates, the total revenue increase, the rental rates, everything like that; those are some of the key variables for real estate. And this is pretty typical of what you might actually see in real life. This type of exercise is common to get on modeling tests, if they give you one in interviews, as well. And any type of Excel or formula test they give you, so just good to know. We have been through a lot in this lesson. I realize it's a lot to take in. but these are some very powerful functions, and I just wanted to spend time on them, to teach you how to use them properly. So that's it for our lesson on how to use INDIRECT, INDEX, and MATCH. Hopefully you have a better idea of the sheer power of these functions now. [31:00] To recap, we went through the basic functionality at first. I showed you how they worked. Then we rewrote some of the functions on here by using the INDEX, INDIRECT, and MATCH. This one's very messy. There's a better way to do it coming up soon. And then we learned how to use it for the real estate data, when we have columns that are out of order and all over the place. Now as far as limitations, well again, you do have to specify a row and column for MATCH, so it's not as flexible as you might think it is. You still have to specify something there. And then INDIRECT again, you still need to specify a cell range as we did, and you saw how that caused some issues before, so these functions are not perfect. They do require more upfront effort, but ultimately they are going to be a lot more flexible and arguably more efficient than the LOOKUP functions as well. And as I say here, you still can't aggregate them easily. They're still looking up single cell values with all of these, but that is sometimes all you need. So that's it for this lesson. Coming up next we're going to jump into database functions. And then we're going to go into array functions, and learn how to use generic array functions, as well as a very common one called TRANSPOSE.