Power Query is the data preparation engine built into Power BI, and you use it any time you want to bring outside data into a report and clean it up before it reaches your visuals. The short version: open Power BI Desktop, click Get data, pick your source, then click Transform Data instead of Load so the Power Query Editor opens. From there you shape the tables (remove columns, change types, filter rows, merge or append queries), then click Close & Load to push the cleaned result into the data model. Everything Power Query does is recorded as repeatable steps, so the same clean-up runs again every time you refresh. You need Power BI Desktop installed and the credentials or file path for your data source before you start.

What You'll Need

  • Power BI Desktop installed from Microsoft's official download page (the Power BI service in a browser has only limited query editing and is not a full replacement for Desktop).
  • The exact location of your data: a file path for Excel or CSV, a server and database name for SQL, a site and list for SharePoint, or an account with permission to read the source.
  • Any credentials the source requires, and permission from whoever owns the data to connect to it.
  • A clear idea of what the finished table should look like: which columns you need, what the rows mean, and which column is the unique key.

Step-by-Step

  1. Start a connection. In Power BI Desktop, go to Home > Get data and choose your source from the list. For a file, pick Excel workbook, Text/CSV, JSON or Folder. For a database, pick SQL Server, PostgreSQL, MySQL or the relevant connector. For cloud services, pick SharePoint, OneDrive or the connector for your app. Labels shift slightly between versions and regions, but the Get data button is always on the Home ribbon.
  2. Point at the data. Paste the file path or server name, choose the table or sheet you want, and click Load only if you truly want raw data. Instead, click Transform Data, which opens the Power Query Editor. If you already clicked Load, you can still get there later from Home > Transform data.
  3. Check the source step. The right-hand Applied Steps pane lists each action. The first step is the source itself. If Power Query guessed a delimiter or a header row wrong for a CSV, delete the step and reconnect, or fix the options in the Source step's settings rather than patching it later.
  4. Promote the right headers. If the first row of data became a header row of generic names like Column1, use Home > Use First Row as Headers. Do this before anything else, because renaming columns after you have already built steps on top of them is more work.
  5. Set data types deliberately. Click the small icon at the left of each column header and choose Text, Whole Number, Decimal Number, Date or the type you need. Power Query sometimes guesses Date/Time where you want a plain Date, so check every column. Wrong types are the most common reason a visual later refuses to aggregate or a relationship will not form.
  6. Trim the table. Use Home > Remove Columns > Choose Columns to keep only what you need, and Remove Rows > Remove Top Rows or Remove Blank Rows to drop junk. Narrow tables load faster and are easier to model.
  7. Filter and clean values. Use the dropdown on a column to filter out nulls, errors or test rows. Use Transform > Trim to remove stray spaces and Transform > Format > Capitalize Each Word for messy text. Right-click a column header > Replace Values handles consistent typos.
  8. Rename columns meaningfully. Double-click a header and give it a business name such as Order Date or Customer ID. This name flows into the model and into your report fields.
  9. Combine tables if needed. Use Home > Merge Queries to join two tables on a matching key column (a left outer join keeps every row from the first table). Use Home > Append Queries to stack tables with the same shape, such as twelve monthly files. For a folder of identical files, connect to the Folder connector and use Combine Files.
  10. Rename the query. In the Queries pane on the left, right-click the query and choose Rename so it reads like the table it produces.
  11. Load it. Click Home > Close & Load to send the result to the data model, where you can build relationships and visuals. If you want the query available but not loaded, choose Close & Load To and pick Only Create Connection.
  12. Refresh and confirm. Click Refresh on the Home ribbon in the main window. Watch the refresh dialog for errors. In the Power BI service, scheduled refresh only works after you publish and, for on-premises sources, after a gateway is configured.

Common Mistakes

  • Loading before transforming. Clicking Load first and then trying to fix a broken table leaves messy columns in the model. Transform first, load second.
  • Ignoring data types. A date column stored as text will not work in a date slicer, and numbers stored as text will not sum. Set types explicitly.
  • Building steps in the wrong order. If you remove a column and then try to use it in a merge, the step fails. Order matters because steps run top to bottom.
  • Hard-coding a file path. A path like C:\Users\YourName\Desktop\sales.xlsx breaks when the file moves or someone else refreshes. Use a parameter for the path or point at a shared location.
  • Merging on mismatched keys. Joining on a text ID against a number ID returns no matches. Convert both sides to the same type first.
  • Doing heavy cleaning in DAX instead. Calculated columns and complex measures are slower than cleaning once in Power Query. If a transformation does not need to react to slicers, do it upstream.

Tips

  • Name every applied step clearly where possible. A well-named step list is documentation for the next person who opens the file.
  • Use the Advanced Editor to review the generated M code. You do not need to write it, but reading it helps you spot duplicated filters or accidental type changes.
  • Keep a reference query (right-click > Reference) when you need two different shapes from one source, rather than connecting twice.
  • Prefer parameters for file paths and server names so the same file works on your machine and on the refresh server.
  • Do the filtering early. Removing unneeded rows before a merge keeps later steps fast and easier to debug.
  • Save versions of the .pbix file before large reshapes so you can step back if a chain of edits goes wrong.

Troubleshooting

Refresh fails with a credentials error. Open Data source settings from the Home ribbon, select the source, and edit or clear permissions, then sign in again. In the service, check the gateway mapping for that source.

An error appears in one column. Click the cell to read the message, then use Home > Remove Rows > Remove Errors or Transform > Replace Errors with a sensible value. Investigate the cause before deleting rows silently, since you may be hiding real data.

The table is empty after a merge. Check the key columns' data types and watch for leading or trailing spaces. Preview both queries and compare sample values side by side.

Refresh is very slow. Look for steps that load more data than needed, such as pulling an entire table then filtering. Use the query folding indicator where available and push filters to the source database.

The transform works locally but not in the service. The service cannot see local files or drives. Move the source to SharePoint, OneDrive or a database, or install and configure the on-premises data gateway.

If that did not work, delete the failing step, re-add it, and test each subsequent step one at a time by clicking it in Applied Steps. Rebuilding a short step is usually faster than hunting for the one bad action in a long list.

Frequently Asked Questions

Is Power Query the same in Excel and Power BI?

The engine is the same and most transformations work identically, but the editors are separate. A query copied between the two may need small adjustments, especially around load settings and connectors.

Can I edit a query after loading it?

Yes. In Power BI Desktop, use Home > Transform data to reopen the Power Query Editor, change steps, and click Close & Load again. The model updates on refresh.

Does Power Query change my original file?

No. It reads from the source and stores a cleaned copy in the model. Your Excel or CSV file stays untouched.

Why does a date column show as text?

The column type was never set, or the source values are in a format Power Query could not parse. Change the type explicitly and check for mixed formats in the column.

Can the Power BI service refresh a local Excel file?

Not directly. The service cannot reach your local drive, so the file needs to sit in SharePoint, OneDrive or a database, or you need a configured gateway.

What is M code?

M is the language Power Query uses to record your steps. You can view it in the Advanced Editor, and every button click adds to it.