Excel Power‌ Query Tutorial (Get & Transform) + Examples

puneet-gogia-excel-champs

- Written by Puneet

Written by Puneet for Excel 2010, Excel 2013, Excel 2016, Excel 2019

If you are one of those people who work with data a lot, you can be anyone (Accountant, HR, Data Analyst, etc.), power query can be your power tool.

Let me come straight to the point, Power Query is one of the advanced Excel skills that you need to learn and in this tutorial, you will be exploring power query in detail and will be learning to transform data with it.

Let’s get started.

What is Excel Power Query

Power Query is an Excel add-in that you can use for ETL. That means, you can extract data from different sources, transform it, and then load it to the worksheet. You can say POWER QUERY is a data cleansing machine as it has all the options to transform the data. It is real-time and records all the steps that you perform.

Why Should You Use Power Query (Benefits)?

If you have this question in your mind, here’s my answer for you:

  • Different Data Sources: You can load data into a power query editor from different data sources, like, CSV, TXT, JSON, etc.
  • Transform Data Easily: Normally you use formulas and pivot tables for data transformations but with POWER QUERY you can do a lot of things just with clicks.
  • It’s Real-Time: Write a query once and you can refresh it every time there is a change in data, and it will transform the new data which you have updated.

Let me share an example:

Imagine you have 100 Excel files that have data from 100 cities and now your boss wants you to create a report with all the data from those 100 files. OKAY, if you decide to open each file manually and copy and paste data from those files and you need at least one hour for this.

But with the power query, you can do it in minutes. Feeling excited? Good.

Further in this tutorial, you will learn how to use Power Query with a lot of examples, but first, you need to understand its concept.

The Concept of Power Query

To learn power query, you need to understand its concept that works in 3 steps:

1. Get Data

Power query allows you to get data from different sources like web, CSV, text files, multiple workbooks from a folder, and a lot of other sources where we can store data.

2. Transform Data

After getting data in the power query you have a whole bunch of options that you can use to transform it and clean it. It creates queries for all the steps you perform (in a sequence one step after another).

3. Load Data

From the power query editor, you can load the transformed data to the worksheet, or you can directly create a pivot table or a pivot chart or create a data connection only.

Where is Power Query (How to Install it)?

Below you can see how to install access to the power query in the different versions of Microsoft Excel.

Excel 2007

If you are using Excel 2007, I’m sorry PQ is not available for this version so you need to upgrade to the latest version of Excel (Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2010).

power-query-excel-2007

Excel 2010 and Excel 2013

For 2010 and 2013, you need to install an add-in separately which you can download from this link and once you install it, you’ll get a new tab in the Excel ribbon, like below:

power-query-excel-2013-2010
  • First, download the add-in from here (Microsoft’s Official Website).
  • Once you have downloaded the file, open it and follow the instructions.
  • After that, you’ll automatically get the “Power Query” tab on your Excel ribbon.

If somehow that “POWER QUERY” tab doesn’t appear, there is no need to worry about it. You can add it using the COM Add-ins option.

  • Go to File Tab ➜ Options ➜ Add-ins.
com-add-ins-1
  • In “Add-In” options, select “COM Add-ins” and click GO.
  • After that, tick mark “Microsoft Power Query for Excel”.
tick-mark-microsoft-power-query-for-excel-1
  • In the end, click OK.

Excel 2016, 2019, Office 365

If you are using Excel 2016, Excel 2019, or you have OFFICE 365 subscription, it’s already there on the Data tab, as a group named “GET & TRANSFORM” (I like this name, do you?).

get-and-transform-1

Excel Mac

If you are using Excel in Mac I’m afraid that there is no power query add-in for it and you can only refresh an existing query but you can’t create a new one and or even edit a query (LINK).

power-query-excel-mac

Power Query Editor

Power Query has its own editor where you can get the data, perform all the steps to create queries, and then load it to the worksheet. To open the power query editor, you need to go to the Data Tab and in the Get & Transform ➜ Get Data ➜ Launch Power Query Editor.

launch-power-query-editor-1

Below is the first look at the editor which you will get when you open it.

first-look-of-the-editor

Now, let’s explore each section in detail:

1. Ribbon

Let’s look at all the available tabs:

  • File: From the file tab, you can load the data, discard the editor, and open the query settings.
  • Home: In the HOME Tab, you have options to manage the loaded data, like, delete and move columns and rows.
  • Transform: This tab has all the options which you need to transform and clean the data, like merge columns, transpose, etc.
  • Add Column: Here you have the option to add new columns to the data you have in the power editor.
  • View: From this tab, you can make changes to the view for the power query editor and data loaded.

2. Applied Steps

On the right side of the editor, you have a query setting pane which includes the name of the query and all the applied steps in a sequence.

applied-steps-list-of-options

When you right-click on a step you have a list of options that you can perform, like, rename, delete, edit, move up or down, etc. and when you click on a step, the editor will take you to the transformation done on that step.

applied-steps-list-of-options

Look at the below where you have the total five steps applied and when I click on the 4th step it takes me to step four’s transformation where the columns name hasn’t changed.

total-five-steps-applied

3. Queries

The queries pane on the left side lists all the queries you have in the workbook right now. It’s basically one place where you can manage all the queries.

the-queries-pane

When you right-click on a query name you can see all the options that you can use (copy, delete, duplicate, etc.)

right-click-on-a-query-name

You can also create a new query by simply right click on the blank space on the queries pane and then select the option for the data source.

blank-space-on-the-queries-pane

4. Formula Bar

As I said, whenever you apply a step in the editor it generates M code for that step, and you can see that code in the formula bar. You can simply click on the formula bar to edit the code.

formula-bar

Once you learn to use M code you can also create step by writing the code and simply clicking on the “FX” button to enter a custom step.

fx-button

5. Data Preview

The data preview area looks like an Excel worksheet but there’s a little different than a normal worksheet where you can edit a cell or data directly. When you load data into the editor (we will do it in a while) it shows all the columns with the headers with the columns name and then rows with data.

the-data-preview-area

At the top of each column, you can see the data type of the data in the column. When you load data into the editor the power query applies the right data type (almost every time) to each column automatically.

You can click on the top left button on the column header to change the data type applied to the column. It has a list of all the data types from where you can.

top-left-button-on-the-column-header

And on the left side of the column header there you have the filter button which you can use to filter values from the column. Note: When you filter values from a column, the power query takes it as one step and lists it in the applied steps.

the-left-side-of-the-column-header

If you right-click on the header of the column you can see that there is a menu that includes a list of the options which you can use to transform the data and use any of the options and PQ stores it as a step in the applied steps.

right-click-on-the-header-of-the-column

Data Sources for Power Query

The best part of the power query is you have the option to get data from multiple sources and transform that data and then load it into the worksheet. When you click on the Get Data in the GET & TRANSFORM you can see the complete list of data sources that you can get data load into the editor.

the-option-to-get-data-from-multiple-sources

Now let’s look at some of the data sources:

  • From Table/Range: With this option, you can load data into the power query editor directly from the active worksheet.
  • From Workbook: From a different workbook that you have on your computer. You just need to locate that file using an open dialog box and it will get data from that file automatically.
  • From Text/CSV: Get data from a text file or a comma-separated file and then you can load it into the worksheet.
  • From Folder: It takes all the files from the folder and load data from them into the power query editor. (See this: Combine Excel Files from a Folder).
  • From Web: With this option, you get data from a web address, imagine you have a File that is stored on the web or you have a web page from where you need to get the data.

How to Load Data into Power Query Editor

Now let’s learn to load data into the power query editor. Here you have a list of student names and their scores (LINK).

learn-to-load-data-into-the-power-query-editor

You will be loading data directly from the worksheet, so you need to open the file first and then follow the below steps:

  • First, apply an Excel table to the data (Even if you don’t do it Excel will do it for you before loading data into PQ editor).
  • Now, select a cell from the table and click on the “From Table/Range” (Data Tab Get & Transform).
click-on-the-from-table-range
  • Once you click on the button, Excel confirms the range of data to apply an Excel table to it.
the-range-of-data-to-apply-an-excel-table
  • At this point, you have the data into the power query editor, and it looks something like below.
the-data-into-the-power-query-editor
  • Here you can see:
    • In the Formula bar, PQ has generated the M code for the table you have just loaded into the editor.
    • On the left side of the editor, you have the queries pane where you have the list of the queries.
    • On the right side, in the query settings, you have the section called “Applied Steps” where you have all the steps listed. Note: You must be thinking that you haven’t performed any “Changed Type” but there’s a step called “Changed Type” is there. Let me tell you the SMARTNESS of POWER QUERY when you load data into the editor it checks and applies the correct data types for all the columns automatically.
formula-bar-pq

Power Query Examples (Tips and Tricks)

You can learn to perform some of the basic tasks which you normally do with functional formulas in Excel, but with power query, you can do it with a few clicks:

1. Replace Values

2. Sort Data

3. Remove Columns

4. Split Column

5. Rename a Column

6. Duplicate Column

7. Transpose Column or Row

8. Replace/Remove Errors

9. Change Data Type

10. Add Column from Examples

11. Change Case

12. Trim and Clean

13. Add Prefix/Suffix

14. Extract Values

15. Only Date or Time

16. Combine Date and Time

17. Rounding Numbers

18. Calculations

19. Group by

20. Remove Negative Values

How to Load Data Back to the Worksheet

Once you transform your data, you can load it to the worksheet and use it for further analysis. On the home tab there is a button called “Close and Load” when you click on it you get a drop-down which has options further:

  • Close and Load
  • Close and Load To
close-and-load-to
  • Once you click on the button, it will show the following options:
import-data-options
  • Select how you want to view this data in your worksheet.
    • Table
    • Pivot Table Report:
    • Pivot Chart
    • Only Create Connection
    • Where do you want to put the Data?
      • Existing Worksheet
      • New Worksheet.
    • Add this data to the Data Model.
  • Just select the table option and new worksheet and don’t tick mark the data model and click OK.
import-data-options-2
  • The moment you click OK, it adds a new worksheet with the data.
new-worksheet-with-the-data

More Examples to Learn

Auto Refresh a Query

From all the examples that I have mentioned here, this one is the most important. When you create a query, you can make it auto-refresh (you can set a timer).

And here are the steps:

  • On the Data tab, click on “Queries & Connections” and you’ll get the Queries and Connection pane on the right side of the window.
queries-and-connections
  • Now, right-click on the query and tick mark “Refresh every” and enter the minutes.
right-click-on-the-query-tick-mark

How to use a Formula and a Function in Power Query

Just like you can use functions and formulas in Excel worksheet, the power query has its own list of functions that you can use. The basics of function and formulas in power query are the same as Excel’s worksheet functions.

In PQ, you need to add a new custom column to add a function or a formula. Let’s take an example: In the below data (already in the PQ editor) you have the first name and last name (DOWNLOAD LINK).

data-already-in-the-pq-editor-1

Imagine you need to merge both names and create a column for the full name. In this case, you can enter a simple formula to concatenate names from both columns.

  • First, go to the Add Column tab and click on the “Custom Column”.
add-column-tab
  • Now in the custom column dialog box, enter the name of the new column “Full Name” or anything you want to name the new column.
custom-column
  • The custom column formula is the place where you need to enter the formula. So enter the below formula in it:
[First Name]&" "&[Last Name]
custom-column-formula
  • When you enter a formula in the “custom column formula”, PQ verify the formula that you have entered and shows a message “No syntax error have been detected” and if there’s an error it will show an error message based on the type of the error.
  • Once you enter the formula and that formula doesn’t have any errors in it, simply press OK.
  • Now you have a new column at the end of the data which has values from two columns (first name and the last name).
new-column-at-the-end-of-the-data

How to use a Function in Power Query

In the same way, you can also use a function while adding a custom column and Power Query has a huge list of functions that you can use.

Let’s understand how to use a function with an easy and simple example. I’m continuing the above example where we have added a new column by combining the first name and last name.

use-function-while-adding-a-custom-column

But now, you need to convert that full name text which you have in that column into the upper case. The function which you can use is Text.Upper. As the name suggests, it converts a text to an upper-case text.

  • First, go to the add column tab and click on the custom column.
click-on-the-custom-column
  • Now in the custom column dialog box, enter the column name and below formula in the custom column formula box:
Text.Upper([Full Name])
custom-column-dialog-box
  • And when you click OK it creates a new column with all the names in the uppercase.
  • The next thing is to delete the old column and rename the new column. So right-click on the first column and select remove.
new-column-with-all-the-names-in-the-uppercase
  • In the end, rename the new column his “Full Name”.

There are a total of 700 functions that you can use in power query while adding a new column and here is the complete list provided by Microsoft for these functions, do check them out.

How to Edit a Query in PQ

If you want to make some changes in the query which is already in your workbook you can simply edit it and then make those changes. On the Data tab, there’s a button named Queries and Connections.

button-named-queries-and-connections

When you click on this button, it opens a pane on the right side that lists all the queries that you have in the current workbook.

lists-all-the-queries-did

You can right-click on the query name and select edit and you will get it in the power query editor to edit.

right-click-on-the-query-name-and-select-edit

When you edit a query, you can see that all the steps which you have performed earlier are listed in the “Applied Steps” that you can also edit or you can perform new steps.

applied-steps-that-you-can-also-edit

And once you are done with your changes you can simply click on the “Close & Load” button.

Export and Import Connections

If you have a connection which you have used for a query and now you want to share that connection with someone else, you can export that connection as an odc file.

On the query table, there’s a button called “Export Connection” and when you click on it, it allows you to save that query’s connection in your system.

export-connection

And if you want to import a connection that is shared by someone else, you can simply go to the Data tab and in the Get & Transform click on the existing connections.

import-a-connection-get-transform

And then click on the “Browse for More” button from where you can locate the connection file which has been shared with you and import it to your workbook.

browse-for-more-button

Power Query Language (M Code)

As I mentioned earlier that for every step you performed in power query it generates a code (at the backend) which is called M Code. On the Home tab, there is a button called “Advanced Editor” which you can use to see the code.

advanced-editor

And when you click on the advanced editor it will show you the code editor and that code looks something like below:

the-code-editor

M is a case sensitive language and like all the other languages it uses variables and expressions. The basic structure of code looks like below where the code starts with the LET expression.

basic-structure-of-code

In this code, we have two variables and the values defined to them. In the end, to get the value, IN expression has been used. Now when you click OK it will return the value assigned to the variable “Variablename” in the result.

variablename

Check out this resource to learn more about Power Query Language.

You can’t afford to avoid the POWER QUERY. If you think like this, a lot of things which we do with Excel functions or VBA codes can be automated using it, and I’m sure this tutorial inspires you to use it more and more.

What is Excel Power Query?

Power Query is a data transforming engine which you can use to get data from multiple sources, clean and transform that data and then use it further in the analysis.

But now you need to tell me one thing. Which thing do you like most about the POWER QUERY?

You must check out these tutorials

Last Updated: February 18, 2024