Lompat ke konten

Pelajaran 23 ยท Cleaning in SQL and Spreadsheets

Cleaning Data in Spreadsheets

Clean spreadsheet data safely: protect raw sheets, import with explicit types, use text and lookup formulas, remove duplicates and record steps in Power Query.

Many people clean data in a spreadsheet, and for small, one-off tasks that can be reasonable. The risk is that edits leave little trace and automatic formatting changes values. This lesson shows safer habits. The formulas are read-only here, so try them in your own spreadsheet. Functions differ between products and versions, and some regional settings use semicolons instead of commas between arguments.

Protect the original

  • Keep a Raw sheet and do not edit it.
  • Build cleaned columns on a separate Clean sheet with formulas.
  • Add a Notes sheet that lists your rules and decisions.

Import, do not just open

Opening a CSV file directly can let the program guess types. It may remove leading zeros, round long numbers or turn codes into dates. Use the import feature and set identifier and code columns to text. Also check the encoding and delimiter.

Text formulas

Goal Formula example Note
Remove extra spaces =TRIM(A2) In Excel and Google Sheets, it does not remove non-breaking spaces
Remove control characters =CLEAN(A2) Does not remove every invisible character
Replace non-breaking spaces =TRIM(SUBSTITUTE(A2,CHAR(160)," ")) CHAR results can depend on the platform; Excel also has UNICHAR
Change case =LOWER(A2), =UPPER(A2) For comparison keys
Capitalize words =PROPER(A2) Often wrong for names
Compare exactly =EXACT(A2,B2) Case-sensitive
Check length =LEN(A2) Can reveal hidden characters

A useful trick: if two cells look the same but do not match, compare their LEN values.

Numbers and dates as text

Text that looks like a number is often aligned like text, and SUM skips it in a cell range. =VALUE(A2) converts text using locale settings. A locale mismatch can silently read dates or decimals wrongly, so check results against the source. When you know the format of a date, split its parts and use DATE(year, month, day) instead of relying on automatic conversion.

Categories with a mapping table

Put variants and standard values on a Map sheet, then look them up:

TEXT
=XLOOKUP(LOWER(TRIM(A2)), Map!A:A, Map!B:B, "UNMAPPED")

XLOOKUP is not available in all spreadsheet versions. For example, Excel 2016 and Excel 2019 do not have it. An alternative is an exact-match lookup such as this:

TEXT
=IFERROR(INDEX(Map!B:B, MATCH(LOWER(TRIM(A2)), Map!A:A, 0)), "UNMAPPED")

Filter for UNMAPPED to review new variants. Do not use approximate match modes for categories. Note that MATCH with exact matching can still treat * and ? in the search text as wildcards.

Duplicates

Count how often a key appears before you remove anything:

TEXT
=COUNTIF(B:B, B2)

Values above 1 suggest repeats. Be aware that COUNTIF ignores letter case and treats * and ? as wildcards. In Excel, it can also treat long digit strings as numbers, so do not trust it for long numeric identifiers. Microsoft also warns that it returns wrong results when matching text longer than 255 characters. A Remove duplicates command deletes rows in place, so use it on a copy and select the right key columns. Some versions offer a UNIQUE function that produces a separate list instead.

Data validation

Restrict what people can type: drop-down lists, whole numbers within a range or dates within limits. This helps prevent new errors. In Excel, values that are pasted or filled in, or that come from formulas or macros, can get past validation messages, so keep checking. See data validation in Excel.

Visual checks

Use filters to view the distinct values of a column, sort to bring extremes to the ends, and use conditional formatting to highlight blanks or values outside limits.

Power Query for repeatable steps

Excel's Power Query imports data and records transformations as steps that you can inspect, edit and refresh when the source changes. Common steps include setting types, trimming text, splitting columns, filtering rows, removing duplicates, replacing values, unpivoting wide tables and merging with a mapping table. The steps are written in the M language.

TEXT
Illustrative step list
1. Source: import the CSV file
2. Promote the first row to headers
3. Set column types (IDs as text)
4. Trim and clean the text columns
5. Merge with the status mapping table
6. Remove rows with an empty order ID (record the count)
7. Load to the Clean sheet

Power Query text comparisons are case-sensitive by default, unlike many worksheet formulas, so normalize case before you remove duplicates or merge. When it matters which duplicate survives, do not assume that an earlier sort decides it: group the rows and choose explicitly, then verify the result. Check Microsoft's current documentation for features in your version. See Power Query basics.

A stand-in: formulas in Python

You can test the logic of a formula chain elsewhere. The next block is a stand-in: Python code that imitates the idea of a trim-and-lookup formula. It does not show how any spreadsheet works internally.

Python Stand-in: a trim and lookup formula in Python

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Know when to move on

Consider SQL or Python when files get large, when you repeat the same steps every week, when several people edit the same logic, or when you need tests and a clear history.

Common mistake: sorting only one column. The values detach from their rows. Sort the whole table, or use a table feature that keeps rows together.

Follow your organization's rules before you upload spreadsheets with personal data to cloud tools or add-ons.

Check your formulas

After you fill a formula down a column, confirm that it reached the last row and that nobody has typed a value over one of the cells. Add a few summary checks on the Notes sheet: the number of raw rows, the number of clean rows, the number of UNMAPPED results and a total compared between sheets. When a new batch changes these numbers in an unexpected way, investigate before you share the file.

Avoid hiding errors. Wrapping every formula in IFERROR with an empty result makes broken references and bad values look like ordinary blanks. Return a visible label, and only for the error you expect.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why import a CSV with explicit column types?

    Type guessing can drop leading zeros or turn codes into dates.
  2. 2. Two cells look identical but do not match. What is a good first check?

    Different lengths point to hidden spaces or invisible characters.
  3. 3. What is an advantage of Power Query over manual edits?

    Recorded steps are repeatable and reviewable, though you must still verify results.

Summary

  • Keep raw sheets untouched and import with explicit types.
  • Combine text formulas and exact lookups to standardize.
  • Count duplicates before you remove them.
  • Use data validation and Power Query for repeatable work.

Next: Project: Clean a Customer List

Progres Anda disimpan di browser ini. Tidak perlu akun.