Power Query in Excel: Import and Clean

First lesson of the Power Query and Power Pivot in Excel course, free to read: about 20 minutes, 3 exercises and 3 quiz questions. This is the catalogue's base lesson; in the student space, it is rewritten for your profile.

Objectives

  • Import a CSV file into Excel with Power Query
  • Apply common cleanups and read the applied steps
  • Choose the load destination: table, connection only or data model

Power Query in ExcelData cleaningLoad options

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:

  1. Structure: check the promoted headers, Home > Remove Rows > Remove Blank Rows;
  2. Text: select the text columns, Transform > Format > Trim, then Capitalize Each Word;
  3. Duplicates: select the key column (or all columns), Remove Rows > Remove Duplicates;
  4. Types: right-click the header > Change Type > Using Locale if the format differs from the machine’s (dates dd/mm/yyyy, decimals);
  5. Missing values: Transform > Replace Values, replacing null with 0 when 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.

  1. TaskDifficulty 22 hints

    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
    1. Import it into Excel with Power Query and open the editor (semicolon delimiter).
    2. Remove the blank rows, clean Nom and Ville (Trim, then Capitalize Each Word).
    3. Remove duplicates on ID Client.
    4. Type ID Client as whole number, Date inscription as date with the French (France) locale, CA as decimal number.
    5. Replace the null values in CA with 0, rename the query Clients and 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
  2. TaskDifficulty 12 hints

    Change the load destination

    In the workbook from the previous exercise:

    1. Duplicate the Clients query and name the copy ClientsParis; filter it on Ville = Paris.
    2. Load ClientsParis as connection only.
    3. Open the Queries & Connections pane and note what is displayed for each query.
    4. Then change the load of ClientsParis to 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)
  3. Multiple choiceDifficulty 11 hint

    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

  1. 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
  2. Which option should you choose for an intermediate query used only by other queries?

    • Table
    • Only Create Connection
    • PivotChart Report
  3. 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

  1. Combining Several Sources: Append and Merge20 min
  2. The Data Model and Power Pivot20 min
  3. First DAX Measures20 min
  4. Refreshing, Parameterizing and Moving to Power BI15 min

Power Query and Power Pivot in Excel · Advanced · 1 h 35 min