Relative References in Excel Tables
Excel Tables have been around for a decade now (they are introduced in Excel 2007), and yet, very few people use them. They are versatile, easy and elegant. At Chandoo.org, we celebrate Tables all the time. If you have never used them, start with below tuts.
- Introduction to Excel tables
- How to use structured referencing
- Tables and Relationships in Excel
- Using lookups and other formulas with Excel tables
- Simple way to get absolute references in Tables
- Customizing table styles for awesome usability
While tables are super helpful, they do come with some limitations. Today let’s examine one such unique problem and learn about an elegant solution.
Table Relative Reference Problem
Imagine you are the machine supervisor at Mighty Machine City Inc. Although your machines are mighty, sometimes they do fail. To keep track of which machines are under repair, you maintain a repair log in Excel. Since you heard Tables are mighty, you thought,
‘Gee whiz, I might as well use tables to maintain the repair log. Chandoo says tables are sweet’
So, your Repair log looks like this:
After a few days of tracking the repairs, you wanted to know if same machines are failing successively. For example, in above picture, you notice that MACH-0038 failed twice in a row starting with 11th of March. Same goes for few other rows.
You are the kind of person who frowns upon manually highlighting yellow color in cells to flag such successive failures. So you want to write a formula.
So you add a new column called Same? and want to fill it up with a simple relative reference formula to check with Machine in row 1 matches machine in row 2.
Here is the formula you used:
=[@[Machine ID]] = B6
Note: Your table starts with Row 5.
Excel automatically filled down the formula for all rows of the table, because tables are awesome like that.
You whistled your way to home that night.
Next day morning, as usual Homer messed up something and you had a new repair to log. So you went to the bottom of fail table and inserted a new row to add the failure details.
And you notice something unusual.
The formula for Same? column is WRONG!!!
As soon as you inserted a new row, Excel adjusted last row’s formula to something silly.
- Before: Let’s say the last row formula reads =[@[Machine ID]]=B20
- After: The last but one row (as you now have an empty row)’s formula reads =[@[Machine ID]]=B21
Now that is clearly wrong!
What is going on here?
Your relative referencing worked ok, until the last row. At this stage, Excel understood the formula as Current row value = value in first cell below table
The part in green is what caused trouble. As soon as you add a new row to your table, fist cell below table is moved down. So Excel adjusted that reference alone.
How to fix this problem?
The usual method to fix this:
- Insert as many rows as you need and complete entering / pasting all data.
- Select the formula in very first cell.
- Fill it down all the way (you can double click on the bottom right corner of the cell)
But that is soooo not awesome.
You are right. This method is manual and error prone. It is the opposite of awesome.
Problems when you delete too: In fact, if you ever delete a row from your table, the formulas further down would show #REF! errors. So this method is not very effective in real life.
An elegant way to get relative references in tables
Instead of using cell address based references, like B6, if you use pure structured references, then Excel will automatically adjust them as your table grows or shrinks.
Simple, we can use OFFSET function along with @ references.
To get next machine ID, you can use
So, to check if same machine failed twice in a row, use
=[@[Machine ID]]=OFFSET([@[Machine ID]],1,0)
As this uses no cell references, whenever you add / change / remove table rows, Excel automatically scales the formula.
But I heard OFFSET is volatile / dangerous / RDX / %#$&@#?
Unless your table has a 200k+ rows or you plan to set up 100s of columns like this, don’t bother. for small, day to day tables, there is no change in performance. If you really hate OFFSET, try talking about it during your next therapy session. Jokes aside, you can also use a longer INDEX based formula to get similar result, but that is semi-volatile too.
Here is one such INDEX based formula.
=INDEX([@[Machine ID]]:INDEX([Machine ID],COUNTA([Machine ID])), 2)
It sure is a mouthful. You can shorten it by using a named formula for INDEX([Machine ID],COUNTA([Machine ID])) portion or the whole thing. Again, I wouldn’t recommend the INDEX based approach over OFFSET for smaller data sets. For larger datasets, see if you can fix the problem at source (for example, modifying your SQL to get offset values in a separate column) or using Power Query to mash the data (more on this in a next post).
So there you go, an elegant and simple way to deal with the relative reference problem in tables.
Bonus tip: generating running numbers in tables
You can use this approach to generate running numbers (1,2,3…) in a table column that grow / change / shrink based on your table. This can be very useful in many scenarios.
To get running numbers in a table column, just use:
=ROWS(fail[[#Headers],[Machine ID]]:[@[Machine ID]])-1
The pattern that you can use in any table goes like this:
=ROWS(Table_Name[[#Headers],[Col 1]]:[@[Col 1]])-1
Bonus Bonus tip: If you have running numbers in a column …
You can then change the OFFSET based relative ref to INDEX like below.
This method is 100% non-volatile, but does return #REF! error for the last row. So use it with IFERROR when nesting in other formulas.
Download Example Workbook
Click here to download an example workbook showcasing all these techniques. Use the extra data to paste and test various methods.
How do you write relative refs in tables?
The handful times when I had to use relative refs in a table, I resorted to cell refs (like B6 above). But this created too much headache further down. So I switched to OFFSET / INDEX approaches.
What about you? How do you write relative references in tables? Please share your stories & struggles in the comments section.
Related Reading (no pun)…
If you are relatively free and want some relaxed reading then check out below related reference links.
- An intro to Excel relative references, structured references
- INDEX formula, OFFSET formula, ROWS formula
Leave a Reply
|Modelling Inventory Run Rate & Cash Flows using Excel|