Skip to content

Lesson 31 of 92 · Free Excel Course

Excel

Excel Power Query for Beginners (Plus Power Pivot): Clean Data Step by Step

Learn Excel Power Query: import, trim, split, fix types, remove duplicates, unpivot, refresh. Then a first Power Pivot relationship and DAX measure. Free workbook.

Every month someone sends you the same messy report: names in one column, stray spaces, numbers that Excel treats as text, the odd duplicate row, and months spread across the top. You can fix it by hand every time, or you can teach Power Query to fix it once and then click Refresh forever after. Power Query is Excel's built-in tool for importing and cleaning data, and you don't write a single formula. This lesson cleans one messy table from start to finish, then uses Power Pivot to link it to a second table and add your first DAX measure.

What Power Query and Power Pivot do (and where they work)

  • Power Query (also called Get & Transform) connects to data, such as a table in your workbook, a CSV file, a folder or a database, and cleans it in a separate window called the Power Query Editor. Every change you make is saved as a step. When the source changes, Refresh runs all the steps again.
  • Power Pivot works on the Data Model, a set of tables stored inside the workbook. You link the tables with relationships and write calculations called measures in a formula language called DAX.

Your original data is never changed. Power Query loads a cleaned copy.

Excel for WindowsExcel for MacExcel for the web
Power QueryFull: Microsoft 365, 2024, 2021, 2019, 2016 (Data, then Get & Transform Data)Microsoft 365 only, version 16.69 or later (Data, then Get Data (Power Query)); not in Excel 2016 or 2019 for MacAll Microsoft 365 subscribers can view and refresh queries; Microsoft lists extra functionality for Business and Enterprise (E3+) plans
Power Pivot and the Data ModelYes: Microsoft 365, 2024, 2021, 2019, 2016No: Microsoft says Data Models aren't supported on MacBuild the model in Excel for Windows

So if you're on a Windows PC with Microsoft 365, you have everything. On a Mac you can clean data with Power Query but not use Power Pivot.

The messy data

Here's the Practice sheet from the workbook. Before you start, click any cell in it, press Ctrl+T, tick My table has headers and click OK. Then on Table Design, type SalesRaw in the Table Name box.

C21200
ABCDEF
1RepRegionJanFebMarNotes
2Ann Lee East12009501100call Mon
3Ben OrtizWest 8001250900
4Carla Diaz North150014001650new client
5Ann Lee East12009501100call Mon
6Dev PatelSouth700650820
7Ella Brown West110013001250
8Ben OrtizWest 8001250900
Seven rows, five problems: first and last names together, extra spaces (before East, after West), numbers stored as text (C2, E4), two duplicate rows (5 and 8) and months spread across three columns.

The goal is one row per rep per month, with clean names and real numbers, which is the shape pivot tables and charts like best.

Step 1: Load the table into Power Query

  1. Click any cell in the SalesRaw table.
  2. On the Data tab, in the Get & Transform Data group, click From Table/Range.
  3. The Power Query Editor opens with a preview of your data. On the right, the Query Settings pane shows the query's name and a list called Applied Steps, which already has Source and Changed Type.
  4. Under Properties, change the name to Sales.

Other sources start from the same place: Data, then Get Data, then From File (for a CSV, text file, workbook or PDF) or From Other Sources. Importing files is covered in Import, export and convert.

On a Mac: go to Data, then Get Data (Power Query). Microsoft's Mac list of sources includes Excel workbooks and Text/CSV files, so the reliable route is to choose Excel Workbook, pick the saved practice file and select the Practice sheet. Then carry on with the same steps.

Step 2: Clean it in the Power Query Editor

Do these in order. Each one adds a line to Applied Steps.

  1. Remove a column. Click the Notes header, then on the Home tab click Remove Columns (or right-click the header and choose Remove).
  2. Trim the spaces. Click the Rep header, Ctrl+click Region, then on the Transform tab click Format, then Trim. " East" becomes "East" and "Ella Brown " becomes "Ella Brown". Trim removes spaces at the start and end only.
  3. Split the names. Click Rep, then Home, Split Column, By Delimiter. Choose Space, pick Left-most delimiter and click OK. You get Rep.1 and Rep.2. Double-click each header and rename them First and Last.
  4. Fix the number types. Click Jan, Shift+click Mar, then on Home open Data Type and choose Whole Number (or click the small icon at the left of a header). The text "1200" becomes the number 1200.
  5. Remove duplicates. Click the First header and Shift+click the last header to select every column, then Home, Remove Rows, Remove Duplicates. Seven rows become five.
  6. Unpivot the months. Select First, Last and Region, right-click one of them and choose Unpivot Other Columns. Jan, Feb and Mar turn into two columns, Attribute and Value. Rename them Month and Amount.

Now Applied Steps reads like a recipe, with names like Removed Columns, Trimmed Text, Split Column by Delimiter, Renamed Columns, Changed Type1, Removed Duplicates and Unpivoted Other Columns. Click any step to see the data as it was at that point. Click the X next to a step to delete it, or the gear icon to change its settings.

Why Unpivot Other Columns rather than selecting the month columns? With "other columns", a new Apr column next month gets unpivoted automatically on refresh. If you had picked Jan to Mar by name, April would be left out.

Step 3: Close & Load, then Refresh

On the Home tab, click Close & Load. Excel adds a new sheet with a green table of 15 rows:

E21200
ABCDE
1FirstLastRegionMonthAmount
2AnnLeeEastJan1200
3AnnLeeEastFeb950
4AnnLeeEastMar1100
5BenOrtizWestJan800
6BenOrtizWestFeb1250
7BenOrtizWestMar900
8CarlaDiazNorthJan1500
9CarlaDiazNorthFeb1400
10CarlaDiazNorthMar1650
11DevPatelSouthJan700
12DevPatelSouthFeb650
13DevPatelSouthMar820
14EllaBrownWestJan1100
15EllaBrownWestFeb1300
16EllaBrownWestMar1250
The cleaned, unpivoted result: five reps times three months. The amounts add up to 16,570 (Jan 5,300, Feb 5,550, Mar 5,720).

Power Query doesn't promise which copy of a duplicate it keeps, so your rows may come out in a slightly different order. The values will match.

Now the clever part. Go back to the Practice sheet and type a new row under the table: Femi Okafor, North, 600, 750, 900. The table grows to include it. Then click Data, then Refresh All (or right-click the green table and choose Refresh). Every step runs again and the result has 18 rows. Next month, paste the new report in and refresh. That's the whole point.

To change the cleaning later, open Data, then Queries & Connections, right-click Sales and choose Edit. Don't type into the green table itself; your changes are wiped out at the next refresh.

Power Pivot: relate two tables and add a measure

The workbook also has a small table on the Regions sheet: East is managed by Grace, North by Iris, South by Jon and West by Hugo. Your boss wants sales by manager. In a normal sheet you'd add an XLOOKUP column. With the Data Model, you link the tables instead.

  1. Turn on Power Pivot if you don't see a Power Pivot tab: File, Options, Add-ins, choose COM Add-ins in the Manage box, click Go, tick Microsoft Office Power Pivot and click OK.
  2. Add both tables to the Data Model. Click inside the Regions list (turn it into a table named Regions with Ctrl+T first), then Power Pivot, Add to Data Model. For the query, open Queries & Connections, right-click Sales, choose Load To, tick Add this data to the Data Model and click OK.
  3. Create the relationship. Click Data, then Relationships, then New. Set Table to Sales, Column (Foreign) to Region, Related Table to Regions and Related Column (Primary) to Region. Click OK, then Close. Regions is the "one" side, so each region must appear only once there.
  4. Add a measure. On the Power Pivot tab, click Measures, then New Measure. Set Table name to Sales, Measure name to Total Sales, type the formula below, click Check Formula, choose Currency under Category if you like, and click OK.
=SUM(Sales[Amount])

Sales[Amount] means "the Amount column of the Sales table". A measure is calculated inside whatever pivot it's dropped into, so the same one line gives a total per manager, per month or overall.

  1. Build the pivot. Click Insert, the arrow under PivotTable, then From External Data Source, Choose Connection. On the Tables tab, pick Tables in Workbook Data Model, click Open, then OK. In the field list, put Manager (from Regions) in Rows and Total Sales (from Sales) in Values.
B56600
AB
3Row LabelsTotal Sales
4Grace$3,250.00
5Hugo$6,600.00
6Iris$4,550.00
7Jon$2,170.00
8Grand Total$16,570.00
Manager comes from the Regions table, Total Sales from the Sales table. The relationship on Region joins them. Hugo has two West reps (Ben 2,950 and Ella 3,650).

The relationship only works because Power Query trimmed the spaces. " East" with a leading space would never match "East", and Grace would show blank.

Try it yourself

Download the practice workbook. The Your task sheet has the steps, Practice has the messy table, Regions has the managers, and Answer key shows the expected results.

  1. Make the messy data a table named SalesRaw, then Data, From Table/Range, and rename the query Sales.
  2. Remove Notes, trim Rep and Region, split Rep into First and Last, and set Jan to Mar to Whole Number.
  3. Remove duplicates (7 rows become 5), then Unpivot Other Columns and rename to Month and Amount.
  4. Close & Load. Check you have 15 rows totalling 16,570.
  5. Add the Femi Okafor row and Refresh All. Check for 18 rows.
  6. (Windows) Add both tables to the Data Model, relate them on Region, create the Total Sales measure and build the manager pivot.

The Answer key also has SUMIFS formulas that work out the manager totals from the cleaned table, so you can prove the pivot right with ordinary formulas.

Common mistakes

  • Editing the loaded table. Anything you type into a query's output disappears at the next refresh. Change the query instead.
  • Forgetting to refresh. Queries don't update by themselves. Use Data, Refresh All.
  • Removing duplicates on one column. Remove Duplicates compares only the columns you selected. Select one column and you keep just one row per value, which deletes real data. Select every column for true duplicate rows.
  • Case and spaces. Power Query treats "east" and "East" as different, and " East" with a space as different again. Trim (and fix the case) before removing duplicates or relating tables.
  • Unpivot Columns instead of Unpivot Other Columns. Selecting the months by name means a new month is skipped on refresh.
  • A relationship that won't create. The column on the Regions side must have each value once, and both columns must be the same data type.

What's next

You've cleaned a messy report with Power Query, made the cleaning repeat with one click, and used Power Pivot to link two tables with a DAX measure. The previous lesson, Macros and VBA, automates tasks with code; Power Query often does the same cleaning with none. Next, Goal Seek, Solver and what-if analysis works backwards from the answer you want. You can also go back to the full Excel course.

Frequently asked questions

What is Power Query in Excel used for?

Power Query imports data and cleans it up for you: removing columns, trimming spaces, splitting names, fixing numbers stored as text, removing duplicates and reshaping wide tables. It records every change as a step, so next month you just click Refresh and the same cleaning runs again on the new data. You find it on the Data tab under Get & Transform Data.

Does Power Query change my original data?

No. Power Query reads a copy of the source, shapes it in the Power Query Editor and loads the result to a new table (or the Data Model). Your original sheet, CSV file or database stays exactly as it was. To change the cleaning, edit the query's steps, not the loaded table.

Is Power Query available on Mac and in Excel for the web?

Yes, with limits. On a Mac you need Excel for Microsoft 365 version 16.69 or later; use Data, then Get Data (Power Query). Excel 2016 and 2019 for Mac don't have it. In Excel for the web every Microsoft 365 subscriber can view and refresh queries, and Microsoft says the extra functionality needs a Business or Enterprise (E3 and above) plan. The full feature set is in Excel for Windows.

What's the difference between Power Query and Power Pivot?

Power Query gets and cleans data. Power Pivot models it: it links several tables in the Data Model with relationships and adds calculations called measures, written in DAX. A common workflow is to clean each table in Power Query, load them to the Data Model, relate them, then build a pivot table. You can use either one on its own.

Is Power Pivot available on a Mac?

No. Power Pivot is a Windows feature (Excel for Microsoft 365, 2024, 2021, 2019 and 2016), and Microsoft states that Data Models aren't supported in Excel for Mac. Mac users can still use Power Query to clean data and a normal pivot table to summarise it.

Do I need to learn the M language or DAX?

Not to start. Every Power Query button writes M code for you, and you never have to open the Advanced Editor. For Power Pivot, simple measures such as a total are one short line of DAX, like `=SUM(Sales[Amount])`. Learn more only when the buttons can't do what you need.