The same tool as in Power BI
Power Query has been built into Excel for Windows for several versions and into Microsoft 365; Excel for Mac and Excel for the web offer part of it. It is the same engine as in Power BI Desktop, with the same M language: everything you learn here is reused as is in the BI2 course, and vice versa. Only the location of the commands and the load options differ.
Why use it in Excel rather than copy-paste? Because cleaning becomes a recorded recipe: next month, you replace the source file, click Refresh, and all the steps are replayed. No more manual operations to redo, no more forgetting.
Importing a file
On the Data tab:
- Get Data > From File > From Text/CSV (or the From Text/CSV button);
- choose the file: a preview opens, with the file origin (encoding, 65001 for UTF-8), the delimiter and type detection;
- click Transform Data to open the editor, rather than Load, which would import without cleaning.
The editor is identical to Power BI’s: queries pane, preview, applied steps on the right, formula bar (View tab), and the Column quality, Column distribution and Column profile options for diagnosis.
Cleaning in the right order
A file exported from software often contains spaces, inconsistent capitalization, blank rows, duplicates and missing values. Recommended order:
- Structure: check the promoted headers, Home > Remove Rows > Remove Blank Rows;
- Text: select the text columns, Transform > Format > Trim, then Capitalize Each Word;
- Duplicates: select the key column (or all columns), Remove Rows > Remove Duplicates;
- Types: right-click the header > Change Type > Using Locale if the format differs from the machine’s (dates
dd/mm/yyyy, decimals); - Missing values: Transform > Replace Values, replacing
nullwith0when it makes business sense.
Normalizing text before removing duplicates is essential: “Paris” and “paris” are not duplicates for Power Query as long as the case differs.
let
Source = Csv.Document(File.Contents("C:\Formation\clients_brut.csv"), [Delimiter = ";", Encoding = 65001]),
Entetes = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),
SansVides = Table.SelectRows(Entetes, each [ID Client] <> "" and [ID Client] <> null),
Texte = Table.TransformColumns(SansVides, {{"Nom", each Text.Proper(Text.Trim(_)), type text},
{"Ville", each Text.Proper(Text.Trim(_)), type text}}),
SansDoublons = Table.Distinct(Texte, {"ID Client"}),
Types = Table.TransformColumnTypes(SansDoublons, {{"ID Client", Int64.Type},
{"Date inscription", type date}, {"CA", type number}}, "fr-FR"),
CAComplet = Table.ReplaceValue(Types, null, 0, Replacer.ReplaceValue, {"CA"})
in
CAComplet
You don’t have to write this code: the interface generates it. But reading it in Home > Advanced Editor lets you check the order and give the steps meaningful names.
Choosing where to load
Home > Close & Load creates a structured table on a new sheet. Close & Load To… offers a choice:
| Option | When to use it |
|---|---|
| Table | the result must be visible, filtered, used in formulas |
| PivotTable Report | you want a PivotTable directly |
| Only Create Connection | intermediate query, reused by other queries |
| Add this data to the Data Model | large volumes, several related tables, DAX measures (lessons 3 and 4) |
An Excel sheet is limited to 1,048,576 rows; the data model has no such per-sheet limit, which makes it the right destination for large volumes.
The Queries & Connections pane (Data > Queries & Connections) lists the queries, the number of rows loaded and any errors. A right-click > Load To… lets you change the destination afterwards.
Never edit the loaded table by hand
The table produced by a query is rewritten on every refresh: a correction typed into it will be lost. Every correction is made in the query. You can, however, add formula columns to the right of the loaded table: Excel keeps them and copies them down to new rows.
Exercises
Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.
Clean a customer file
With Notepad, create the file
C:\Formation\clients_brut.csv(UTF-8 encoding):ID Client;Nom;Ville;Date inscription;CA 1; dupont marie ;lyon;15/01/2025;1200 2;MARTIN Paul;Paris;03/02/2025;850 2;MARTIN Paul;Paris;03/02/2025;850 3;Durand Léa; LILLE;20/02/2025; ;;;; 4;Petit Hugo;Nantes;11/03/2025;430 5;Moreau Chloé;paris;28/03/2025;2100- Import it into Excel with Power Query and open the editor (semicolon delimiter).
- Remove the blank rows, clean
NomandVille(Trim, then Capitalize Each Word). - Remove duplicates on
ID Client. - Type
ID Clientas whole number,Date inscriptionas date with the French (France) locale,CAas decimal number. - Replace the
nullvalues inCAwith0, rename the queryClientsand load it into a table.
- The Clients query has named steps in this order: blank rows, text, duplicates, types, replacement
- The loaded table contains 5 rows and 5 columns
- The cities displayed are Lyon, Paris, Lille, Nantes, Paris (no spaces, initial capital)
- MARTIN Paul's date is 3 February 2025
Change the load destination
In the workbook from the previous exercise:
- Duplicate the
Clientsquery and name the copyClientsParis; filter it onVille = Paris. - Load
ClientsParisas connection only. - Open the Queries & Connections pane and note what is displayed for each query.
- Then change the load of
ClientsParisto add it to the data model.
- The pane shows "5 rows loaded" for Clients and "Connection only" for ClientsParis before step 4
- No sheet was created for ClientsParis
- After step 4, ClientsParis appears in the data model (Data > Data Tools > Manage Data Model, or Power Pivot)
- Duplicate the
Shared engine
Can a Power Query query written in Excel be reused in Power BI Desktop?
- No, the languages are different
- Yes, it is the same M language; you can copy the code from the advanced editor
- Only if the file is in .xlsm format
- Only for CSV files
End-of-lesson quiz
What happens to the source CSV file after your Power Query transformations?
- It is modified
- It stays intact; only the steps are saved in the workbook
- It is deleted
Which option should you choose for an intermediate query used only by other queries?
- Table
- Only Create Connection
- PivotChart Report
Why normalize case before removing duplicates?
- To speed up the refresh
- Because "paris" and "Paris" are different values for Power Query
- It is not necessary
The rest of the course
- Combining Several Sources: Append and Merge20 min
- The Data Model and Power Pivot20 min
- First DAX Measures20 min
- Refreshing, Parameterizing and Moving to Power BI15 min