Sub topic 8
Reading and writing structured references
This chapter assumes you can already create a table, name it (for example SalesTable) and enter ordinary formulas with A1-style references. It covers the syntax of structured references, how Excel generates them, and the errors you are most likely to meet.
The examples use a table named SalesTable with these columns: Date, Region, Product, Quantity, Unit Price and Amount.
What a structured reference is
A structured reference is a formula reference that uses the table name and column headers in place of cell addresses. Compare these two formulas:
=SUM(F2:F400)
=SUM(SalesTable[Amount])Both add up the same cells today. The second one stays correct when rows are added or removed, because Excel resolves SalesTable[Amount] to the current data cells of that column each time. It also tells the reader what is being summed.
A structured reference points at the data body of a column. It does not include the header cell or the total row unless you ask for them with a special item specifier, described below.
Anatomy of a reference
A full reference has up to three parts: the table name, an optional item specifier, and a column specifier.
You can leave out parts you do not need:
| Reference | What it returns |
|---|---|
| `SalesTable[Amount]` | Data cells of the Amount column |
| `SalesTable[[#Headers],[Amount]]` | The header cell of Amount |
| `SalesTable[[#Totals],[Amount]]` | The total row cell of Amount |
| `SalesTable[#Data]` | All data cells, with no headers or totals |
| `SalesTable[#Headers]` | The header row |
| `SalesTable[#All]` | Headers, data and total row |
| `SalesTable[[Quantity]:[Amount]]` | The data cells of every column from Quantity through Amount |
| `SalesTable[@Amount]` | The Amount cell in the same row as the formula |
| `SalesTable[@]` | The whole current row |
The [@Column] form is the "this row" specifier. Older workbooks and Excel 2007 write the same thing as SalesTable[[#This Row],[Amount]]. Current Excel converts that older form to the @ form when it opens the workbook.
Inside the table versus outside it
Inside the table, you can drop the table name. These two formulas, entered in a row of SalesTable, mean the same thing:
=SalesTable[@Quantity]*SalesTable[@[Unit Price]]
=[@Quantity]*[@[Unit Price]]Outside the table, the table name is required. Excel always shows the shorter form while you edit a formula inside the table and the longer form when you view the same formula from elsewhere. If a formula inside a table refers to a whole column, [Amount] is enough:
=[@Amount]/SUM([Amount])This gives each row's share of the column total.
Letting Excel write the reference for you
You rarely need to type structured references in full. When you type =SUM( and then click or drag over a column of data in the table, Excel inserts SalesTable[Amount]. Typing the table name also opens an autocomplete list of column names. Press Tab to accept a suggestion.
If you click cells in the same row of the table while building a formula, Excel writes [@Column]. If you would rather get A1-style references such as F2, turn off File > Options > Formulas > Use table names in formulas. The setting only changes what Excel generates when you click. You can type either style at any time, and existing structured references are not converted.
Brackets and special characters
Bracket rules cause most of the typing mistakes. Follow these three:
- A single column name with no special characters needs one pair of brackets:
SalesTable[Amount]. - If the column name contains a space or punctuation, Excel wraps it in an extra pair of brackets when it is combined with
@or an item specifier:[@[Unit Price]]. Characters that trigger this include space, comma, colon, period, parentheses, braces, and operators such as+,-,*,/,=,<,>and&. - Four characters need an apostrophe in front of them inside a column name: the left bracket
[, the right bracket], the pound sign#, and the apostrophe'itself.
For a column headed Sales [USD], the reference is:
=SUM(SalesTable[Sales '[USD']])If a formula will not accept your typing and you cannot see why, check the brackets first. Better still, avoid the problem by giving columns plain names without brackets, # or apostrophes.
Combining item specifiers and column ranges
When you use more than one specifier, wrap them in an outer pair of brackets and separate them with commas:
=SalesTable[[#Headers],[Quantity]:[Amount]]
=SUM(SalesTable[[#Data],[Quantity]:[Amount]])
=COUNTA(SalesTable[[#All],[Region]])The first returns the header cells of three adjacent columns. The second adds the data cells in those three columns. The third counts every non-empty cell in Region, headers and total row included, so it returns the data count plus one (or plus two with a total row showing).
A row-based range works the same way. This formula adds Quantity through Amount for the current row only:
=SUM(SalesTable[@[Quantity]:[Amount]])Use a column range like this with care. Adding a column between the two ends changes what the formula includes.
Common patterns
Conditional totals read like sentences:
=SUMIFS(SalesTable[Amount],SalesTable[Region],"West")A lookup against a table keeps working as rows are added:
=INDEX(SalesTable[Amount],MATCH("P-1042",SalesTable[Product],0))A count that ignores the total row:
=ROWS(SalesTable[#Data])A header can feed a formula too, which is useful for labels:
="Total "&SalesTable[[#Headers],[Amount]]Because the label comes from the header cell, it updates when you rename the column.
How structured references behave when you edit
Structured references follow the table, not the cell addresses. This produces behavior that differs from A1 references:
- Renaming a table or column. Excel rewrites every formula that refers to the old name. You do not need to fix them.
- Adding rows. A reference to
SalesTable[Amount]includes new rows as soon as they join the table. - Copying a formula. Copy
=SUM(SalesTable[Amount])to another cell and it still points at the same column. A reference to a whole column has no relative offset, so nothing shifts. This differs from an A1 reference without$signs. - Filling right. A formula such as
=SUM(SalesTable[Quantity])filled to the next column does not becomeSalesTable[Unit Price]. Excel keeps the column you named. If you need relative columns, use A1 references orINDEXwith a column number. - Deleting a column. Any formula that refers to that column returns
#REF!. - Converting to a range. Excel changes structured references into ordinary A1 references. See the chapter on converting a table back to a range.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| `#REF!` after editing a header | The column was deleted, or a hand-typed name no longer matches | Restore the column or retype the reference with the new name |
| Formula shows `F2` instead of `[@Amount]` | Click-to-reference is off, or the formula was typed as A1 | Enable **Use table names in formulas** and retype the formula |
| "There's a problem with this formula" on entry | Missing outer brackets, or an unescaped `[`, `]`, `#` or `'` | Rewrite with double brackets or add apostrophes |
| Total includes the header or total row | `#All` used where `#Data` was meant | Use `SalesTable[Amount]` or `SalesTable[#Data]` |
| `@` appears in a formula that worked before | The workbook was opened in a newer Excel and the older `[#This Row]` form was converted | No fix needed. Both forms mean the same thing |
| Formula returns the wrong row's value | `[@Column]` used outside the table | Outside the table, `@` uses implicit intersection with the row. Use `INDEX` and `MATCH` instead |
Exercises
Exercise
A table named Orders has a column called Unit Price and a column called Qty. Write a formula, entered in a new column of that table, that gives the line total for each row. Then write a formula outside the table that adds up the Unit Price header text to the word "Total", producing for example "Total Unit Price".
Show answer
Inside the table: =[@Qty]*[@[Unit Price]]. The extra brackets around Unit Price are needed because the name contains a space. Outside the table: ="Total "&Orders[[#Headers],[Unit Price]].
Exercise
You write =SUM(Orders[#All]) expecting the sum of the data. It returns a larger number than expected, and the table has a total row showing. Why, and what is the fix?
Show answer
#All includes the header row and the total row. The total row holds a SUBTOTAL result, so its value is added into your sum a second time. Use =SUM(Orders[#Data]) or simply =SUM(Orders[Amount]), which count only the data cells.