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.
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)

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.
- An intro to Excel relative references, structured references
- INDEX formula, OFFSET formula, ROWS formula
Happy learning.














13 Responses to “Using pivot tables to find out non performing customers”
To avoid the helper column and the macro, I would transpose the data into the format shown above (Name, Year, Sales). Now I can show more than one year, I can summarize - I can do many more things with it. ASAP Utilities (http://www.asap-utilities.com) has a new experimental feature that can easily transpose the table into the correct format. Much easier in my opinion.
David
Of course with alternative data structure, we can easily setup a slicer based solution so that everything works like clockwork with even less work.
David, I was just about to post the same!
In Contextures site, I remember there's a post on how to do that. Clearly, the way data is layed out on the very beginning is critical to get the best results, and even you may thinkg the original layout is the best way, it is clearly not. And that kind of mistakes are the ones I love ! because it teaches and trains you to avoid them, and how to think on the data structure the next time.
Eventually, you get to that place when you "see" the structure on the moment the client tells you the request, and then, you realized you had an ephiphany, that glorious moment when data is no longer a mistery to you!!!
Rgds,
Chandoo,
If the goal is to see the list of customers who have not business from yearX, I would change the helper column formula to :
=IF(selYear="all",sum(C4:M4),sum(offset(C4:M4,,selyear-2002,1,columns(C4:M4)-selyear+2002)))This formula will sum the sales from Selected Year to 2012.
JMarc
If you are already using a helper column and the combox box runs a macro after it changes, why not just adjust the macro and filter the source data?
Regards
I gotta say, it seems like you are giving 10 answers to 10 questions when your client REALLY wants to know is: "What is the last year "this" customer row had a non-zero Sales QTY?... You're missing the forest for the trees...
Change the helper column to:
=IFERROR(INDEX(tblSales[[#Headers],[Customer name]:[Sales 2012]],0,MATCH(9.99999999999999E+307,tblSales[[#This Row],[Customer name]:[Sales 2012]],1)),"NO SALES")
And yes, since I'm matching off of them for value, I would change the headers to straight "2002" instead of "Sales 2002" but you sort the table on the helper column and then and there you can answer all of your questions.
Hi thanks for this. Just can't figure out how you get the combo box to control the pivot table. Can you please advise?
Cheers
@Kevin.. You are welcome. To insert a combo box, go to Developer ribbon > Insert > form controls > combo box.
For more on various form controls and how to use them, please read this: http://chandoo.org/wp/2011/03/30/form-controls/
Thanks Chandoo. But I know how to insert a combobox, I was more referring to how does in control the year in the pivot table? Or is this obvious? I note that if I select the Selected Year from the PivotTable Field List it says "the field has no itens" whereas this would normally allow you to change the year??
Thanks again
worked it out thanks...
when =data!Q2 changes it changes the value in column N:N and then when you do a refreshall the pivottable vlaues get updated
Still not sure why PivotTable Field List says “the field has no itens"?? I created my own pivot table and could not repeat that.
Hi, I put the sales data in range(F5:P19) and added a column D with the title 'Last sales in year'. After that, in column D for each customer, the simple formula
=2000+MATCH(1000000,E5:P5)
will provide the last year in which that particular customer had any sales, which can than easily be managed by autofilter.
Somewhat longer but perhaps a bit more solid (with the column titles in row 4):
=RIGHT(INDEX($F$4:$P$19,1,MATCH(1000000,F5:P5)),4)
[…] Finding non-performing customers using Pivot Tables […]