fbpx
Search
Close this search box.

Relative References in Excel Tables

Share

Facebook
Twitter
LinkedIn

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.

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:

repair-log-data

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.

repair-log-relative-ref-done-wrong

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.

table-relative-refs-gone-wrong

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.

But how?

Simple, we can use OFFSET function along with @ references.

To get next machine ID, you can use

=OFFSET([@[Machine ID]],1,0)

So, to check if same machine failed twice in a row, use

=[@[Machine ID]]=OFFSET([@[Machine ID]],1,0)

relative-refs-in-tables-solution

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.

=INDEX([Machine ID],[@Running]+1)

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.

Happy learning.

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Excel School made me great at work.
5/5

– Brenda

Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

Weighted Average in Excel with Percentage Weights

Weighted Average in Excel [Formulas]

Learn how to calculate weighted averages in excel using formulas. In this article we will learn what a weighted average is and how to Excel’s SUMPRODUCT formula to calculate weighted average / weighted mean.

What is weighted average?

Wikipedia defines weighted average as, “The weighted mean is similar to an arithmetic mean …, where instead of each of the data points contributing equally to the final average, some data points contribute more than others.”

Calculating weighted averages in excel is not straight forward as there is no built-in formula. But we can use SUMPRODUCT formula to easily calculate them. Read on to find out how.

12 Responses to “Relative References in Excel Tables”

  1. UniMord says:

    If you're going to go volatile with Offset(), you can use: =INDIRECT("R[1]C[-1]", 0) or the like, as well, to get to the next row, without errors.

  2. David N says:

    Another INDEX option for "next row" without a column of running numbers...

    =IFERROR(INDEX([Col Name],ROW([@[Col Name]])-ROW(Table_Name[[#Headers],[Col Name]])+1),"")

  3. bharat says:

    can you please help me with this syntax [@[Machine ID]]=B20
    i didn't know what does @ operator do.
    can you please explain

  4. Mark Knochel says:

    For generating running numbers in tables, I use the following formula statement: =ROW()-ROW(fail[[#Headers],[Running]])

  5. Jeff Weir says:

    Chandoo: very cool solution. Of course, I'm going with the INDEX one, on account of it being semi-volatile. But thanks immensely for both.

  6. rohan says:

    Wow!!

  7. Tim says:

    Any solutions for relative references in tables where you intend to change the order of the row often?

    For example, let's say I have a table with 3 rows (apple, banana, carrot), and 3 columns (fruit, count, cost). What are some ways I can make it that the cost for banana is always twice the cost of apple, regardless of how the table has been sorted? The only one I can think of is Index-Match. Are there any others?

  8. Amar says:

    Nice blog !!! Awesome posts & informations, i had Liked Your Blog & way Of Writing/ Composing The Posts In Your Blog,Share More Informations Through Your Blog,Keep Going On And All The Best

  9. Alex Groberman says:

    1) Select cell A1
    2) New defined name: Call it "below" (or whatever) and set it to "=2:2" (relative! If you need it on more than one sheet, you can make it "=!2:2" although I've heard relative sheet referencing like this occasionally confuses excel in some formulas like INDIRECT)
    3) The formula is "=[Machine ID] below"
    4) WOW!

    The intersection operator (space) is easily the most underrated operator in excel, allowing you to take two ranges (a row and a column in this case) and return only where they overlap. You can even use this on same the column you're typing the formula into with no circular reference!

    Regards,

    Alex

  10. rama shankar says:

    Question - 1 Multiply the time given in column 'B' and 'C' into column 'D' and result should be like given example.
    coloumn B Coloumn C Coloumn D
    8 9 72:00:00
    7 9:30:00
    5.5 8
    8.3 8:25:20
    10 5:20:00
    6.5 7

Leave a Reply