Sub topic 1
What messy data looks like and why it breaks your sheet
Data that looks fine on screen often fails in formulas. A lookup returns #N/A for a name you can see in the list. A pivot table shows the same customer twice. A SUM comes out lower than the numbers you can see. In each case the cells look correct to you, but Google Sheets compares the exact characters stored in them.
This chapter covers the common kinds of messy data, the symptom each one causes, and the checks that tell you which problem you have. The later chapters cover the fixes.
Prerequisites
This chapter assumes you can already do the following:
- Move around a spreadsheet and describe cells, rows, columns, and ranges such as
A2:A. - Enter a simple formula such as
=A1&B1. - Select a range and use the menu bar.
You do not need to know any cleanup functions yet. The functions used here (LEN, ISNUMBER, COUNT, COUNTA, UNICODE, EXACT) are small diagnostic helpers, and each one is explained where it appears.
Why a sheet that looks right can be wrong
Sheets does not compare what you see. It compares the characters in the cell. Two cells that look the same can differ in ways your eyes cannot detect:
- One cell may end with a space.
- One may contain a non-breaking space, which looks like a normal space but is a different character.
- One may be a number and the other a text string that only looks like a number.
Software that imports data usually causes this. CSV exports, web pages pasted into a sheet, form responses, CRM downloads, and PDF copy-and-paste all bring in odd characters. Hand-typed data adds its own inconsistency, because different people type the same thing in different ways.
Here is a small sample that shows most of the problems at once:
| Row | Name (A) | Email (B) | Amount (C) | Phone (D) |
|---|---|---|---|---|
| 2 | Jane Doe | [email protected] | 1200 | (555) 010-1234 |
| 3 | `jane doe ` (trailing space) | [email protected] | `1,200` (text) | 555.010.1234 |
| 4 | ` JANE DOE` | [email protected] | 1200 | 5550101234 |
| 5 | Doe, Jane | [email protected] | 1200 | 555 010 1234 |
A person reads this as the same customer four times. Sheets sees four different name strings, three different capitalizations of the email, one text amount, and four phone formats.
The common kinds of messy data
Extra spaces
There are three forms of extra space:
- Leading spaces come before the text.
- Trailing spaces come after it. They are the hardest to spot, because nothing visible changes.
- Repeated internal spaces put two or more spaces between words.
Extra spaces are the most common cause of failed lookups. "Jane Doe" and "Jane Doe " are different strings, so VLOOKUP and MATCH treat them as different keys.
Invisible characters
Some characters take up no visible space or look like something else:
- Non-breaking spaces (character code 160) usually come from web pages and some exports. They look like spaces but are not the normal space (code 32), so the standard whitespace tools may leave them in place.
- Non-printing characters include line breaks, tabs, and control characters left over from other systems. They can make a cell show an odd extra line or make two matching cells fail a comparison.
Inconsistent capitalization
NEW YORK, new york, and New York mean the same thing to a person. Whether Sheets treats them as equal depends on the feature:
- The
=operator,COUNTIF,VLOOKUP, andUNIQUEignore case. EXACTand some comparisons made in scripts or other tools do not.- Data exported from your sheet to another system may become case-sensitive again.
Case differences are therefore a risk even when they do not break a formula today. They cause trouble when the data moves to a database or when a different feature handles it.
Duplicate rows
Duplicates appear when you merge exports, import the same file twice, or when a person submits a form more than once. Inflated counts, double-counted revenue, and repeated mailings are typical results. A duplicate can be an exact copy of a row, or a near-duplicate where the values differ only by spaces or case. Near-duplicates are the harder case, because a duplicate check compares exact values.
Combined values in one cell
A single cell may hold several facts, such as Doe, Jane, Austin, TX 78701, or red;blue;green. You cannot sort by last name or filter by state when both pieces share a cell. Any per-part count or lookup needs the values split into separate columns first.
Numbers and dates stored as text
A value that looks like a number but is text is one of the most damaging problems. Common causes are:
- thousands separators or currency symbols in the imported text, as in
1,200or$1,200; - a leading apostrophe;
- a trailing space;
- a locale mismatch, such as
1.200,50in a sheet set to a US locale.
Text values are usually left-aligned in a cell and numbers right-aligned, unless someone changed the alignment. That makes alignment a quick visual hint, though not proof.
Inconsistent formats
Phone numbers, postal codes, and dates are often entered in several styles, as in the Phone column above. Each style is a different string, so none of them match. Formatting punctuation also gets in the way of numeric handling. Pattern-based cleanup is the usual fix, and a later chapter covers it.
How these problems break your sheet
| Problem | What you see | Why it happens |
|---|---|---|
| Trailing or leading space | `VLOOKUP` returns `#N/A` for a visible match | The lookup key and the table value are different strings |
| Non-breaking space | `TRIM` appears to do nothing | `TRIM` removes the normal space (code 32) and not code 160 |
| Number stored as text | `SUM` is lower than expected, or `0` | Aggregate functions skip text values |
| Case differences | Duplicates survive, or a pivot shows split groups | Some tools compare exactly and others ignore case |
| Near-duplicate rows | Counts are inflated | Rows differ by one hidden character, so none are exact copies |
| Combined values | You cannot sort or filter by one part | Several facts share one cell |
Diagnose before you fix
Before you change anything, confirm the problem with a formula. Put these in a spare column so they do not touch the data.
To detect extra spaces, compare a cell's length with the length of the same text after normal spaces are cleaned up:
=LEN(A2) - LEN(TRIM(A2))A result above 0 means the cell has leading, trailing, or repeated spaces. A result of 0 does not prove the cell is clean, because TRIM ignores non-breaking spaces.
To check for a hidden first character, return the character code of the first character:
=UNICODE(LEFT(A2, 1))A normal letter gives its letter code. A result of 32 means a leading space, and 160 means a leading non-breaking space. Use RIGHT(A2, 1) to check the end of the text.
To check whether two cells that look identical really match, use EXACT:
=EXACT(A2, A3)This returns TRUE only if the strings are identical, including case. Plain =A2=A3 ignores case, so it can say TRUE for text that differs in capitalization.
To check numbers stored as text, test the type directly:
=ISNUMBER(C2)FALSE for a value that looks like a number means it is text. You can also compare the totals of two counting functions over the column:
=COUNT(C2:C) & " numbers of " & COUNTA(C2:C) & " filled cells"COUNT counts only numeric cells and COUNTA counts every non-empty cell. A gap between them points to text where numbers should be.
Visual clues and their limits
A few quick visual checks catch many problems:
- Click into a cell and press the End key. If the cursor lands past the last visible letter, there is trailing whitespace.
- Look at alignment. Left-aligned values in a column that should be numeric are suspicious.
- Sort the column. Entries with leading spaces sort to the top, and different capitalizations may sort separately from each other.
- Filter by value. The filter list shows near-identical entries as separate choices, such as
TexasandTexas(with a trailing space).
These clues are useful for spotting a problem, but they do not give certainty. An invisible character or a text number may show no visual sign at all, so rely on the formulas above for confirmation.
Making the problem stay fixed
Cleaning once is not enough if the source keeps producing messy data. Think about where the mess comes from:
- If it comes from a recurring import, plan to clean on every import, ideally with formulas that update when new data arrives.
- If it comes from typing, restrict input using data validation, such as dropdown lists for fields with a fixed set of values.
- If it comes from a form, check whether the form can validate responses before they reach the sheet.
The later chapters teach both approaches: one-time menu tools and repeatable formulas. Which fits depends on whether the data is a one-off file or an ongoing feed.
Exercises
Exercise
A cell in A2 shows "Jane Doe" and a lookup of "Jane Doe" in another sheet returns #N/A. Name two formulas you could put in a helper column to find out why, and say what each result would tell you.
Show answer
Use =LEN(A2) - LEN(TRIM(A2)). A result above 0 means leading, trailing, or repeated spaces. Use =UNICODE(RIGHT(A2, 1)) (or LEFT for the start). A result of 32 means a normal space, and 160 means a non-breaking space, which TRIM does not remove. You could also use =EXACT(A2, "Jane Doe"), where FALSE shows the strings differ in some character.
Exercise
A column of amounts looks numeric, but =SUM(C2:C) is lower than expected. Which two functions can show how many cells are text, and what do you compare?
Show answer
=COUNT(C2:C) counts numeric cells and =COUNTA(C2:C) counts all non-empty cells. If COUNTA is larger than COUNT, the difference is the number of cells stored as text. =ISNUMBER(C2) on individual cells shows which ones are text.