Sub topic 1
What Power Query Does and Why It Beats Manual Cleanup
Power Query is the data import and transformation engine built into Excel, under the Data tab as Get & Transform Data. You use it to connect to a data source, reshape the data, and load the result into a worksheet. Each change you make is recorded as a step. When the source data changes, you refresh the query and Excel replays every step on the new data.
This chapter explains what Power Query does, how it differs from manual cleanup and formulas, and when to choose it.
Prerequisites
This chapter assumes you can do the following:
- Work with Excel worksheets, tables, and basic formulas.
- Recognize rows, columns, and data types such as text, number, and date.
- Open and save Excel workbooks and CSV files.
You do not need to know any programming. Later chapters introduce the M language, which Power Query uses behind the scenes.
The problem with manual cleanup
Suppose you receive a sales export every Monday. It has extra spaces in the Region column, dates stored as text, and a blank row at the top. You fix it by hand:
- Delete the blank row.
- Run TRIM in a helper column, then copy and paste the results as values.
- Convert the text dates.
- Save the result in a new sheet.
This works once. The next Monday you repeat every step from memory. Manual cleanup has three weaknesses:
- It is not repeatable. Nothing records what you did, so a colleague (or you, in six months) cannot reproduce it exactly.
- It is error-prone. Skipping one step, or pasting over the wrong range, produces wrong numbers with no warning.
- It destroys the original. If you edit the raw data in place, you can no longer compare it to what you received.
What Power Query does differently
Power Query separates the source from the result. The raw data stays untouched. The query holds a list of instructions, and the output table is rebuilt from the source each time.
The workflow has four stages:
- Connect to a source such as an Excel table, a CSV file, a folder, or a database.
- Transform the data in the Power Query Editor. Each action becomes an entry in the Applied Steps list.
- Load the result to a worksheet table, or keep it as a connection only.
- Refresh to rerun all steps when the source changes.
Steps are recorded, not applied to the source
When you click a command such as Remove Duplicates, Power Query does not delete anything in your file. It adds a step to the query. The step says "remove duplicates from the previous step's result." The source stays exactly as it was.
This is why the approach is safe. You can click any step in the Applied Steps pane to see the data as it looked at that point, and you can delete or edit a step if you made a mistake.
A first look at a query
Here is a small query. It reads an Excel table named SalesRaw, sets types for two columns, and cleans the text in a third.
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Typed = Table.TransformColumnTypes(Source, {{"Date", type date}, {"Amount", type number}}),
Trimmed = Table.TransformColumns(Typed, {{"Region", each Text.Trim(Text.Clean(_)), type text}})
in
TrimmedYou do not have to write this code. Power Query generates it as you click commands. Each line matches one entry in Applied Steps:
| Applied Step | What it does |
|---|---|
| Source | Reads the table `SalesRaw` from the current workbook |
| Typed | Sets Date to a date type and Amount to a number type |
| Trimmed | Removes non-printing characters and extra spaces from Region |
Each step uses the output of the step before it. The last line after in names the step whose result becomes the query output.
Power Query compared with formulas, macros, and PivotTables
Power Query overlaps with other Excel tools. Choosing the right one saves effort.
| Tool | Best for | Limitation for cleanup |
|---|---|---|
| Worksheet formulas | Calculations that depend on live cell values | Cleanup needs helper columns, and the raw data stays mixed with the cleaned data |
| VBA macros | Custom automation, including actions outside the data | Needs code skills, and is harder to inspect and maintain |
| PivotTables | Summarizing already-clean data | Does not fix headers, types, or text problems |
| Power Query | Repeatable import, cleanup, reshaping, and combining | Does not recalculate until you refresh, and it is not meant for ad hoc what-if analysis |
Power Query typically comes first in the pipeline. It prepares a clean table, and then formulas, PivotTables, and charts work from that table.
When to use Power Query
Power Query is a good fit when:
- You receive data on a regular schedule and clean it the same way each time.
- The data comes from several files, such as one CSV per month, and must be combined.
- The layout is wrong for analysis. For example, months run across the columns and you need them down the rows.
- You need to join two tables, such as orders and customers, without writing lookup formulas.
- The data has many rows, since worksheet formulas on large ranges can slow a workbook.
Manual work or formulas may be better when:
- The task is a one-time fix of a few cells.
- You need results that update instantly as you type, without a refresh.
- The output must be a formatted report layout and not a table of data.
Common errors and misunderstandings
- Expecting live updates. A query output does not change when the source changes until you refresh it. Use Data > Refresh All, or set refresh options on the query.
- Editing the output table by hand. The output table is rebuilt on every refresh. Manual edits to it are lost. Make the change as a step in the query instead.
- Forgetting that steps run in order. A step that refers to a column name fails if an earlier step removed or renamed that column. Check the step order when you see an error.
- Assuming the source is changed. Power Query reads from the source. It never writes back to it.
- Hard-coded file paths. A query that points to
C:\Data\sales.csvbreaks when a colleague opens the workbook on a computer without that path. A later chapter on parameters covers this.
What the rest of this tutorial covers
The following chapters follow the workflow above. You will import from an Excel table, a CSV file, and a folder. You will set data types, fix headers, and clean text. You will reshape data with splits, fills, and unpivoting. You will combine data with append and merge, summarize it with Group By, and load and refresh the results. The final chapters cover M code, error handling, folder imports, query folding, and parameters.
Exercise
A colleague cleans a weekly export by hand. She trims the Region column with a helper column, pastes values over the original, and deletes blank rows. Give two reasons Power Query is a better choice, and name one situation where it would not be.
Show answer
First, the steps are recorded, so the cleanup repeats exactly on each refresh and any colleague can inspect it. Second, the source data is never changed, because the query reads from it and applies the steps to a copy in the output. Power Query would not be the best choice for a one-time fix of a few cells, or when results must update instantly as you type without a refresh.
Exercise
You load a query to a worksheet and then type a correction directly into one cell of the output table. After the next refresh, the correction is gone. Why, and what should you do instead?
Show answer
The output table is rebuilt from the source by replaying every step, so manual edits to it are overwritten. Make the correction as a step in the query (for example, a Replace Values step), or fix it in the source data.