--- title: "Cleaning data with Power Query, SQL and Python" description: "Clean a messy data set the repeatable way: fix types, remove duplicates, handle blanks and reshape wide tables, using Excel Power Query, SQL or Python with Polars, with AI as a helper." updated: 2026-12-03 minutes: 13 level: Intermediate order: 2 sources: - https://support.microsoft.com/en-us/excel/power-query-for-excel-help - https://support.microsoft.com/en-us/excel/unpivot-columns-power-query - https://support.microsoft.com/en-us/Excel/keep-or-remove-duplicate-rows-power-query - https://learn.microsoft.com/power-query/working-with-duplicates - https://docs.pola.rs/user-guide/lazy/ - https://docs.pola.rs/api/python/stable/reference/ - https://pandas.pydata.org/docs/user_guide/missing_data.html - https://duckdb.org/docs/ --- Real data is messy. Names have extra spaces, dates arrive as text, the same customer appears three times, and someone built the sheet with one column per month. If you analyze it as is, your answers will be wrong. The modern rule is simple: **never clean by hand in the original file.** Clean with recorded steps you can rerun next month on fresh data. Excel Power Query, SQL and Python all work this way. ## The cleaning checklist Work through these in order. Most messes fall into one of them. Save the original export in a raw folder and never edit it. All cleaning reads from raw and writes to a new, clean output. Make dates real dates, numbers real numbers and IDs text. An ID like 00451 stored as a number becomes 451 and no longer matches. Remove extra spaces, fix capitalization and map spelling variants to one value, so "NY", "N.Y." and "New York" become one. Decide which columns define a duplicate, such as order ID alone, then remove repeats. Do not drop rows just because two customers share a name. For each column with blanks, choose: leave blank, fill with a known default, or drop the row. Write the choice down. Never fill missing sales with zero unless zero is true. Analysis tools want long, tidy tables: one row per observation, one column per variable. Unpivot wide month columns into a month column and a value column. Count rows before and after each step. Compare a total, like sum of sales, with the source system. ## Wide to long: the reshape most people miss Wide (hard to analyze) Store Jan Feb Mar A 10 12 9 Unpivot Long (tidy) Store Month Sales A Jan 10 A Feb 12 A Mar 9 Next month adds rows, not columns, so nothing downstream breaks ## Option 1: Excel Power Query Power Query is built into Excel under the **Data** tab, in the **Get and Transform Data** group. Every click becomes a recorded step listed under **Applied Steps**. Next month, drop in the new file and press **Refresh**. - **Get Data** from your file, folder or database, then choose **Transform Data** to open the editor. - Set each column's type with the icon at the left of its header. - **Transform, Format, Trim** removes extra spaces. **Replace Values** fixes spelling variants. - Select the columns that define a duplicate, then **Home, Remove Rows, Remove Duplicates**. Power Query compares text with case, so make case consistent first. - Select the columns to keep, such as Store, then **Transform, Unpivot Other Columns**. This keeps working when a new month column appears. - **Close and Load** sends the clean table back to a sheet or the Data Model. ## Option 2: SQL If the data lives in a database or warehouse, clean it there. This query trims names, converts text to a date, drops blank order IDs and keeps one row per order: ```sql SELECT DISTINCT order_id, TRIM(customer_name) AS customer_name, CAST(order_date AS DATE) AS order_date, amount FROM raw_orders WHERE order_id IS NOT NULL; ``` `DISTINCT` only removes rows that match in every selected column. If the same order appears twice with different amounts, find out why before you choose one. ## Option 3: Python with Polars For large files or many steps, Python is the most flexible. Polars reads big files fast and can plan the whole job before running it, which is called **lazy** mode. The same ideas work in pandas with slightly different code. ```python import polars as pl clean = ( pl.scan_csv("raw/orders.csv") .with_columns( pl.col("customer_name").str.strip_chars(), pl.col("order_date").str.to_date("%Y-%m-%d"), ) .filter(pl.col("order_id").is_not_null()) .unique(subset=["order_id"]) .collect() ) clean.write_parquet("clean/orders.parquet") ``` Save clean output as Parquet, a compact file type that keeps column types, so the next step does not have to guess them again. ## Using AI as a cleaning partner AI assistants are very good at this work. Paste in the column names and five sample rows with personal details removed, describe the mess, and ask for Power Query steps, SQL or Polars code. Then: - Run it on a copy and compare row counts and totals with the raw data. - Ask it to list every assumption it made, such as the date format. - Keep the code or query in a file next to the data so the cleaning is documented. Only share real data with AI tools your employer has approved for it. Customer names, emails and IDs often fall under privacy rules that vary by place. Use made-up sample rows when you ask for help. ## Checklist - [ ] Raw file saved and never edited - [ ] Types set for every column, IDs kept as text - [ ] Text trimmed and standardized - [ ] Duplicate rule written down and applied - [ ] Blank-handling choice written down for each column - [ ] Wide tables unpivoted to long - [ ] Row counts and a key total checked against the source - [ ] Steps saved so you can refresh next month