Power Query Editor: How to Clean and Transform Data
In this article
The Power Query Editor is a data transformation and preparation engine that allows you to import data from various sources, clean it, reshape it, and load it into a destination like an Excel spreadsheet or a Power BI report. It achieves this through a process called ETL (Extract, Transform, Load), which automates the repetitive tasks of scrubbing data so you do not have to manually delete rows or rewrite formulas every time your data source updates. To use it, you simply need a version of Microsoft Excel that supports Power Query or a Power BI Desktop installation.
What You'll Need
- A computer running Windows or macOS with Microsoft Excel (Office 365 or standalone versions) or Power BI Desktop.
- A data source to connect to, such as an Excel workbook, a CSV file, a folder of files, a SQL database, or a web URL.
- Administrative permissions to access the data sources if they are stored on a secured corporate network or server.
Step-by-Step
- Connect to Your Data: Open Excel or Power BI. In Excel, go to the Data tab on the ribbon. Look for the Get & Transform Data group. Click on Get Data and select your source (e.g., Get Data > From File > From Text/CSV). In Power BI, click Get Data on the Home tab. Note that menu labels may differ slightly depending on your software version or region.
- Launch the Editor: After selecting your file or database, a preview window will appear showing a sample of the data. Instead of clicking Load, click Transform Data. This action opens the Power Query Editor in a separate window, which is where all the cleaning happens.
- Inspect the Data Types: Look at the icons next to each column header (e.g., 1.2 for decimal, ABC for text, a calendar icon for dates). Power Query attempts to guess the data type automatically. If a date column is listed as text, right-click the column header, select Change Type, and choose Date. This ensures that date-based filters and calculations work correctly.
- Remove Unnecessary Data: To clean up the dataset, select columns you do not need by holding Ctrl and clicking the headers, then right-click and select Remove Columns. To remove empty rows or errors, click the drop-down arrow on a column header, go to Remove Rows, and select Remove Blank Rows or Remove Errors.
- Split and Merge Columns: If a column contains combined data (like "City, State"), right-click the header and select Split Column > By Delimiter. Choose the comma as the delimiter to create two separate columns. Conversely, if you have first and last names in separate columns, select both, right-click, and choose Merge Columns, then specify a space as the separator.
- Filter and Sort: Use the drop-down arrows on the column headers to filter out specific values. For example, if you only want data from the year 2023, use the date filter to exclude other years. This reduces the volume of data loaded into your final report, improving performance.
- Pivot and Unpivot Data: If your data is "wide" (e.g., months are listed as column headers), it is often difficult to analyze. Select the columns that should remain fixed, right-click, and select Unpivot Other Columns. This transforms the data into a "long" format, which is the standard requirement for pivot tables and Power BI visuals.
- Create Custom Columns: To perform calculations, go to the Add Column tab and select Custom Column. Give the column a name and write a simple formula using the provided column references (e.g., [Price] * [Quantity]). Power Query uses the M language for these formulas, though the interface provides most of the logic for you.
- Review Applied Steps: Look at the Applied Steps pane on the right side of the window. Every action you took—from removing columns to changing types—is recorded here as a sequential step. If you make a mistake, click the X icon next to the specific step to undo it. You can also click on a previous step to see how the data looked at that exact moment in the process.
- Load the Data: Once the data is cleaned, go to the Home tab and click Close & Apply (in Power BI) or Close & Load (in Excel). This pushes the transformed data into your worksheet or data model. Whenever the original source file is updated, you can simply click Refresh in the Data tab to run all these steps again automatically.
Common Mistakes
One of the most frequent errors is neglecting to check data types before performing calculations. If you try to sum a column that Power Query has labeled as "Text," the operation will fail or return an error. Always verify the type icon in the header before adding custom columns.
Another common mistake is performing "hard-coded" filters. For example, if you filter a column to only show "January" and then refresh the data in February, your report will still only show January because that specific filter is locked into the Applied Steps. Use dynamic filters or parameters if the data is expected to change over time.
Users often forget that Power Query is case-sensitive. If you create a filter for "New York" but the source data contains "new york," those rows will be excluded. To fix this, right-click the column, select Transform, and choose Capitalize Each Word or Lowercase before applying filters.
Lastly, many users perform too many redundant steps. For instance, changing the data type three different times for the same column creates three separate entries in the Applied Steps pane, which can slow down the refresh speed of large datasets.
Tips
To speed up your workflow, use the "Column from Examples" feature. Instead of writing a complex formula to extract a piece of text, go to Add Column > Column from Examples. Type a few examples of what you want the result to look like, and Power Query will automatically figure out the logic and write the M code for you.
When dealing with multiple files in a single folder, use the "Combine Files" feature. Instead of importing each CSV individually, connect to the Folder. Power Query will create a function that applies the same cleaning steps to every file in that folder and stacks them into one master table.
Keep your Applied Steps organized. You can right-click any step in the Applied Steps pane and select Rename. Instead of "Removed Columns1," rename it to "Removed Internal ID Columns." This makes it much easier for you or a colleague to understand the logic of the query months later.
Use the "Group By" feature to summarize data before loading it. If you have millions of rows of transaction data but only need daily totals, grouping by date and summing the amount will drastically reduce the size of the resulting table and make your Excel workbook much faster.
Troubleshooting
If you encounter a "Data Format Error" or "Expression Error" after refreshing, the most likely cause is a change in the source file. If a column header was renamed in the original Excel file, Power Query will not be able to find that column and the query will break. To fix this, go to the Applied Steps, find the step where the error first appears (usually the "Promoted Headers" or "Changed Type" step), and update the column name in the formula bar to match the new source name.
If the Power Query Editor is running slowly or freezing, try disabling the "Background Data" load. Go to File > Options and settings > Query Options. Under the "Data Load" section, uncheck "Allow data preview to download in the background." This prevents the editor from constantly refreshing the preview while you are working.
When a query fails to load into Excel due to a "Privacy Level" warning, it is usually because you are combining data from two different sources (e.g., a local file and a website). To resolve this, go to File > Options and settings > Query Options > Privacy and select "Ignore the Privacy Levels" if the data is not sensitive, or set both sources to "Organizational."
If you accidentally deleted a step and cannot undo it via the X icon, check the formula bar at the top. If you are comfortable with the M language, you can manually edit the step names or logic. If not, the safest route is to delete the problematic step and re-perform the action from the ribbon menus.
Frequently Asked Questions
Can Power Query handle millions of rows?
Yes, Power Query can process millions of rows because it does not load the data into the Excel grid until you specifically tell it to. You can load the results directly into the Data Model (Power Pivot), which can handle significantly more data than a standard Excel worksheet.
Is Power Query the same as VBA macros?
No. VBA is a programming language used to automate tasks within the Excel interface. Power Query is a dedicated data transformation engine. While VBA can be used to trigger a Power Query refresh, Power Query is generally easier to maintain and more powerful for data cleaning.
Do I need to learn the M language to use Power Query?
No. The vast majority of users can perform all necessary transformations using the buttons and menus in the ribbon. The M language is generated automatically in the background; you only need to learn it if you want to create highly complex, custom logic.
Can I use Power Query to merge two different tables?
Yes. You can use the "Merge Queries" feature, which works similarly to a VLOOKUP or a SQL JOIN. You select a common column (a key) between two tables, and Power Query will join the rows based on that matching value.
Does Power Query change the original source file?
No. Power Query is read-only. It takes a snapshot of the data, applies transformations in its own memory, and outputs the result to a new location. Your original CSV or database file remains completely untouched.