Become a Pro / website · becomeapro.ai / content / lessons / knowledge-work / data-analysis / cleaning-data-power-query-sql-python.mdx
| 1 | --- |
| 2 | title: "Cleaning data with Power Query, SQL and Python" |
| 3 | 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." |
| 4 | updated: 2026-12-03 |
| 5 | minutes: 13 |
| 6 | level: Intermediate |
| 7 | order: 2 |
| 8 | sources: |
| 9 | - https://support.microsoft.com/en-us/excel/power-query-for-excel-help |
| 10 | - https://support.microsoft.com/en-us/excel/unpivot-columns-power-query |
| 11 | - https://support.microsoft.com/en-us/Excel/keep-or-remove-duplicate-rows-power-query |
| 12 | - https://learn.microsoft.com/power-query/working-with-duplicates |
| 13 | - https://docs.pola.rs/user-guide/lazy/ |
| 14 | - https://docs.pola.rs/api/python/stable/reference/ |
| 15 | - https://pandas.pydata.org/docs/user_guide/missing_data.html |
| 16 | - https://duckdb.org/docs/ |
| 17 | --- |
| 18 | |
| 19 | 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. |
| 20 | |
| 21 | 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. |
| 22 | |
| 23 | ## The cleaning checklist |
| 24 | |
| 25 | Work through these in order. Most messes fall into one of them. |
| 26 | |
| 27 | <Steps> |
| 28 | <Step title="Keep the raw data untouched">Save the original export in a raw folder and never edit it. All cleaning reads from raw and writes to a new, clean output.</Step> |
| 29 | <Step title="Set the right data types">Make dates real dates, numbers real numbers and IDs text. An ID like 00451 stored as a number becomes 451 and no longer matches.</Step> |
| 30 | <Step title="Trim and standardize text">Remove extra spaces, fix capitalization and map spelling variants to one value, so "NY", "N.Y." and "New York" become one.</Step> |
| 31 | <Step title="Remove true duplicates">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.</Step> |
| 32 | <Step title="Handle blanks on purpose">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.</Step> |
| 33 | <Step title="Reshape to one row per thing">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.</Step> |
| 34 | <Step title="Check the result">Count rows before and after each step. Compare a total, like sum of sales, with the source system.</Step> |
| 35 | </Steps> |
| 36 | |
| 37 | ## Wide to long: the reshape most people miss |
| 38 | |
| 39 | <Diagram caption="Unpivoting turns one column per month into a month column and a value column. Charts, pivots and SQL all work better on the long shape."> |
| 40 | <svg viewBox="0 0 600 260" role="img" aria-label="A wide table with columns Store, Jan, Feb, Mar becomes a long table with columns Store, Month, Sales"> |
| 41 | <text x="130" y="25" fontSize="16" textAnchor="middle" fill="currentColor">Wide (hard to analyze)</text> |
| 42 | <rect x="20" y="40" width="220" height="30" className="soft" stroke="currentColor" strokeWidth="2" /> |
| 43 | <text x="50" y="61" fontSize="14" textAnchor="middle" fill="currentColor">Store</text> |
| 44 | <text x="110" y="61" fontSize="14" textAnchor="middle" fill="currentColor">Jan</text> |
| 45 | <text x="160" y="61" fontSize="14" textAnchor="middle" fill="currentColor">Feb</text> |
| 46 | <text x="210" y="61" fontSize="14" textAnchor="middle" fill="currentColor">Mar</text> |
| 47 | <rect x="20" y="70" width="220" height="30" fill="none" stroke="currentColor" strokeWidth="1.5" /> |
| 48 | <text x="50" y="91" fontSize="14" textAnchor="middle" fill="currentColor">A</text> |
| 49 | <text x="110" y="91" fontSize="14" textAnchor="middle" fill="currentColor">10</text> |
| 50 | <text x="160" y="91" fontSize="14" textAnchor="middle" fill="currentColor">12</text> |
| 51 | <text x="210" y="91" fontSize="14" textAnchor="middle" fill="currentColor">9</text> |
| 52 | <line x1="260" y1="80" x2="330" y2="80" stroke="currentColor" strokeWidth="2" /> |
| 53 | <path d="M320 74 L332 80 L320 86" fill="none" stroke="currentColor" strokeWidth="2" /> |
| 54 | <text x="295" y="110" fontSize="14" textAnchor="middle" fill="currentColor">Unpivot</text> |
| 55 | <text x="460" y="25" fontSize="16" textAnchor="middle" fill="currentColor">Long (tidy)</text> |
| 56 | <rect x="350" y="40" width="220" height="30" className="soft" stroke="currentColor" strokeWidth="2" /> |
| 57 | <text x="390" y="61" fontSize="14" textAnchor="middle" fill="currentColor">Store</text> |
| 58 | <text x="460" y="61" fontSize="14" textAnchor="middle" fill="currentColor">Month</text> |
| 59 | <text x="530" y="61" fontSize="14" textAnchor="middle" fill="currentColor">Sales</text> |
| 60 | <rect x="350" y="70" width="220" height="30" fill="none" stroke="currentColor" strokeWidth="1.5" /> |
| 61 | <text x="390" y="91" fontSize="14" textAnchor="middle" fill="currentColor">A</text> |
| 62 | <text x="460" y="91" fontSize="14" textAnchor="middle" fill="currentColor">Jan</text> |
| 63 | <text x="530" y="91" fontSize="14" textAnchor="middle" fill="currentColor">10</text> |
| 64 | <rect x="350" y="100" width="220" height="30" fill="none" stroke="currentColor" strokeWidth="1.5" /> |
| 65 | <text x="390" y="121" fontSize="14" textAnchor="middle" fill="currentColor">A</text> |
| 66 | <text x="460" y="121" fontSize="14" textAnchor="middle" fill="currentColor">Feb</text> |
| 67 | <text x="530" y="121" fontSize="14" textAnchor="middle" fill="currentColor">12</text> |
| 68 | <rect x="350" y="130" width="220" height="30" fill="none" stroke="currentColor" strokeWidth="1.5" /> |
| 69 | <text x="390" y="151" fontSize="14" textAnchor="middle" fill="currentColor">A</text> |
| 70 | <text x="460" y="151" fontSize="14" textAnchor="middle" fill="currentColor">Mar</text> |
| 71 | <text x="530" y="151" fontSize="14" textAnchor="middle" fill="currentColor">9</text> |
| 72 | <text x="300" y="220" fontSize="14" textAnchor="middle" fill="currentColor">Next month adds rows, not columns, so nothing downstream breaks</text> |
| 73 | </svg> |
| 74 | </Diagram> |
| 75 | |
| 76 | ## Option 1: Excel Power Query |
| 77 | |
| 78 | 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**. |
| 79 | |
| 80 | - **Get Data** from your file, folder or database, then choose **Transform Data** to open the editor. |
| 81 | - Set each column's type with the icon at the left of its header. |
| 82 | - **Transform, Format, Trim** removes extra spaces. **Replace Values** fixes spelling variants. |
| 83 | - Select the columns that define a duplicate, then **Home, Remove Rows, Remove Duplicates**. Power Query compares text with case, so make case consistent first. |
| 84 | - Select the columns to keep, such as Store, then **Transform, Unpivot Other Columns**. This keeps working when a new month column appears. |
| 85 | - **Close and Load** sends the clean table back to a sheet or the Data Model. |
| 86 | |
| 87 | ## Option 2: SQL |
| 88 | |
| 89 | 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: |
| 90 | |
| 91 | ```sql |
| 92 | SELECT DISTINCT |
| 93 | order_id, |
| 94 | TRIM(customer_name) AS customer_name, |
| 95 | CAST(order_date AS DATE) AS order_date, |
| 96 | amount |
| 97 | FROM raw_orders |
| 98 | WHERE order_id IS NOT NULL; |
| 99 | ``` |
| 100 | |
| 101 | `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. |
| 102 | |
| 103 | ## Option 3: Python with Polars |
| 104 | |
| 105 | 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. |
| 106 | |
| 107 | ```python |
| 108 | import polars as pl |
| 109 | |
| 110 | clean = ( |
| 111 | pl.scan_csv("raw/orders.csv") |
| 112 | .with_columns( |
| 113 | pl.col("customer_name").str.strip_chars(), |
| 114 | pl.col("order_date").str.to_date("%Y-%m-%d"), |
| 115 | ) |
| 116 | .filter(pl.col("order_id").is_not_null()) |
| 117 | .unique(subset=["order_id"]) |
| 118 | .collect() |
| 119 | ) |
| 120 | clean.write_parquet("clean/orders.parquet") |
| 121 | ``` |
| 122 | |
| 123 | Save clean output as Parquet, a compact file type that keeps column types, so the next step does not have to guess them again. |
| 124 | |
| 125 | ## Using AI as a cleaning partner |
| 126 | |
| 127 | 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: |
| 128 | |
| 129 | - Run it on a copy and compare row counts and totals with the raw data. |
| 130 | - Ask it to list every assumption it made, such as the date format. |
| 131 | - Keep the code or query in a file next to the data so the cleaning is documented. |
| 132 | |
| 133 | <Callout type="warning">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.</Callout> |
| 134 | |
| 135 | ## Checklist |
| 136 | |
| 137 | - [ ] Raw file saved and never edited |
| 138 | - [ ] Types set for every column, IDs kept as text |
| 139 | - [ ] Text trimmed and standardized |
| 140 | - [ ] Duplicate rule written down and applied |
| 141 | - [ ] Blank-handling choice written down for each column |
| 142 | - [ ] Wide tables unpivoted to long |
| 143 | - [ ] Row counts and a key total checked against the source |
| 144 | - [ ] Steps saved so you can refresh next month |