Sub topic 10
Sorting and filtering
This chapter assumes you can create a table, know its name, and can read basic structured references. You should also be comfortable with the Ribbon and with entering simple formulas.
Every table header cell gets a drop-down button as soon as the table exists. That button is the main way to sort and filter a table. You don't need to turn on AutoFilter yourself.
Opening the header menu
Click the arrow in a header cell. The menu has three parts:
- Sort commands at the top. The wording changes with the data type: "Sort A to Z" for text, "Sort Smallest to Largest" for numbers, "Sort Oldest to Newest" for dates.
- Filter by Color and a text, number or date filter submenu, depending on the column.
- A checklist of the distinct values in the column.
The header arrows stay visible when you scroll down. Excel replaces the column letters with the table's header names while the active cell is inside the table, so you can still see which column is which.
Sorting a table
Choose a sort command from the header menu to sort the whole table by that column. Excel moves entire rows, so each record stays intact. You don't need to select the rows first.
To sort by more than one column, open Data > Sort with a cell in the table selected. Add a level for each column. For example, sort by Region first, then by Amount from largest to smallest.
A small arrow on the header button shows which column is sorted and in which direction.
Filtering a table
Clear the check boxes for the values you want to hide, or use the submenu for a condition such as "Greater Than". A filtered column shows a funnel icon on its button. Rows that don't match are hidden, not deleted.
You can filter several columns at once. Excel shows only the rows that satisfy every filter, which is an AND condition. To remove filters, use Clear Filter From in the header menu, or Data > Clear to reset every column.
Formulas and filtered rows
Hidden rows still count in many formulas. This formula adds every row, whether it is visible or not:
=SUM(SalesTable[Amount])To total only the rows that a filter leaves visible, use SUBTOTAL with function number 109:
=SUBTOTAL(109,SalesTable[Amount])Function number 109 sums the values and ignores rows hidden by a filter. The next chapter shows how the total row builds this formula for you.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Sort scrambles the data | Rows in the table were not sorted as a whole, often because a range outside the table was sorted | Sort from inside the table, using the header button |
| A new row is not sorted | Sorting is applied once and is not live | Reapply it with **Data > Reapply** |
| Filtered rows still appear in a total | The formula uses SUM | Use SUBTOTAL with 109 |
| Filter list misses new values | The list is stale after you add data | Reapply the filter, or reopen the menu |
Exercise
A table named SalesTable has a Region column and an Amount column. You filter Region to show only "West". A cell contains =SUM(SalesTable[Amount]). A second cell must show the total for the visible West rows only. What formula do you use, and why is the first formula not enough?
Show answer
Use =SUBTOTAL(109,SalesTable[Amount]). SUM adds every row in the Amount column, including rows the filter has hidden. SUBTOTAL with function number 109 ignores rows hidden by a filter, so it totals only the visible West rows.
Exercise
You sort a table by Amount from largest to smallest, then add a new row at the bottom. The new row has the largest value, but it stays at the bottom. How do you put it in the right place?
Show answer
Sorting is applied once, so Excel does not move new rows automatically. Click a cell in the table and choose Data > Reapply. You can also run the sort again from the header menu.