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

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
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.

21 Responses to “How to Filter Odd or Even Rows only? [Quick Tips]”

  1. Vijay says:

    Infact, instead of using =ISEVEN(B3), how about to use =ISEVEN(ROW())

    So it takes away any chance of wrong referencing.

  2. Hui... says:

    I like Daily Dose of Excel

  3. vimal says:

    I like it.

  4. Luke M says:

    Just a heads up, you do need to have the Analysis ToolPak add-in activated to use the ISEVEN / ISODD functions. An alternative to ISEVEN would be:
    =MOD(ROW(),2)=0

  5. Debbi says:

    rather than use a formula, couldn't you enter "true" in first cell and "false" in the second and drag it down and than filter on true or false.

  6. Paul S says:

    Just for clarification, is Ashish looking to filter by even or odd Characters or rows?

  7. Fred says:

    so many functions to learn!

  8. Istiyak says:

    Nice support by chandoo and team as a helpdesk. Give us more to learn and make us awesome. Always be helpful.......

  9. Arps says:

    In case you want to delete instead of filter,

    IF your data is in Sheet1 column A
    Put this in Sheet2 column A and drag down
    =OFFSET(Sheet1!A$1,(ROWS($1:1)-1)*2,,)
    (This is to delete even rows)

    To delete odd rows :
    =OFFSET(Sheet1!A$2,(ROWS($1:1)-1)*2,,)

  10. Pippa says:

    If your numbered cells did not correspond to rows, the answer would be even simpler:
    =MOD([cell address],2), then filter by 0 to see evens or 1 to see odds.

  11. Matthew D. Healy says:

    I sometimes do this using an even simpler method. I add a new column called "Sign" and put the value of 1 in the first row, say cell C2 if C1 contains the header. Then in C3 I put the formula =-1 * C2, which I copy and paste into the rest of the rows (so C4 has =-1 * C3 and so forth). Now I can just apply a filter and pick either +1 or -1 to see half the rows.

    Another way, which works if I want three possibilities: in C2 I put the value 1, in C3 I put the value 2, in C4 I put the value 3, then in C5 I put the formula =C2 then I copy C5 and paste into all the remaining rows (so C6 gets =C3, C7 gets =C4, etc.). Now I can apply a filter and pick the value 1, 2, or 3 to see a third of the rows.

    Extending this approach to more than 3 cases is left as an exercise for the reader.

  12. Paulo says:

    Another way =MOD(ROW();2). In this case, must to choose betwen 1 and 0.

  13. Makhan Butt says:

    very different style Odd or Even Rows very easy way to visit this site

    http://www.handycss.com/tips/odd-or-even-rows/

  14. Terhile says:

    Thanks for the tip, it worked like magic, saved having to delete row by row in my database.

  15. majid says:

    Thankssssssssssssssss

  16. Bhanu says:

    Hi Chandoo- First of all thanks for the trick. It helped me a lot. Here I have one more challenge. Having filtered the data based on odd. I want to paste data in another sheet adjacent to it. How can I do that?
    For Example-
    A 1 odd
    B 3 odd
    C 4 even
    D 6 even
    I have fileted the above data for odd and want to copy the "This is odd number" text in adjacent/next sheet here. How can I do that. After doing this my data should look like this
    A 1 odd This is odd number
    B 3 odd This is odd number
    C 4 even
    D 6 even

  17. Adriana says:

    Hi! Could you please help me find a formula to filter by language?
    Thank you!

  18. avinash says:

    Chandoo SIR,

    I HAVE A DATA IN EXCEL ROWS LIKE BELOW IS THERE ANY FORMULA OR A WAY WHERE I CAN INSTRUCT I CAN MAKE CHANGES , MEANS I WANT TO WRITE ONLY , THE FIG IS FRESH, BUT IN BELOW ROW IT WILL AUTOMATICALLY TAKE THE SOME WORDS FROM FIGS AND MAKE IN PLURAL FORM , WHILE USING '' ARE'' LIKE BELOW

    The fig is fresh - row 1
    Figs are fresh - row 2
    The Pomegranate is red - row 3
    Pomegranates are red - row 4

  19. Arshad Hussain Shah says:

    =IF(EVEN(A1)=A1,"EVEN - do something","ODD - do something else") with iferron (for blank Cell)

Leave a Reply