This comprehensive Power Query tutorial is your entry point to mastering advanced data transformations in Excel. You will learn how to import data from diverse sources, transform and merge it according to your requirements, write formulas in the M language, automate query refreshes, and more.
In the ever-evolving world, data is essential for practically everything we do, from making crucial business decisions to settling friendly debates about the best pizza toppings. However, information often comes from different sources, in different formats, and sometimes seems as organized as a toddler's toy chest. So, here's the conundrum - how can we import, transform, and analyze data in Excel without spending hours on manual manipulations or writing complex formulas? The solution is Power Query, an invaluable tool for any data analyst, saving hours of time, reducing manual errors, and transforming an unruly informational chaos into a harmonious symphony of insights.
Power Query, also known as Get & Transform, is a multi-purpose tool in Microsoft Excel and Power BI programs that allows you to import data from a wide variety of sources and transform that data to meet your specific needs.
It was first introduced as an add-in for Excel 2010 and later became a native feature in Excel 2016 and subsequent versions. Power Query (PQ) provides a user-friendly interface for connecting to various data sources, shaping, merging and cleaning your data, and then loading it into Excel for in-depth analysis and polished reporting in the form of pivot tables, charts, dashboards, and more.
In essence, Power Query empowers Excel users to effortlessly extract, manipulate, and organize data from an expansive array of sources, making it a game-changer for professionals dealing with complex datasets.
Power Query is a versatile tool with a multitude of applications. Here are some common uses:
Data extraction. Connect to various data sources, including databases, Excel files, text files, and online services.
Data transformation. A wide range of data transformation functions are available to reshape your data into a more suitable structure for analysis, such as filtering, sorting, grouping, merging, splitting columns into multiple fields, creating calculated columns, and more.
Data cleansing. Data quality is crucial for accurate analysis. PQ provides tools to identify and correct errors, remove duplicates, trim extra spaces, fill missing values, and standardize formats, ensuring that your data is clean and reliable.
Combining data. PQ makes it simple to combine and merge data from different locations into a single cohesive dataset, facilitating comprehensive analysis.
Automating workflows. Power Query records every step you take, creating a query that can be easily modified or reused later. This is especially useful when dealing with recurring data sources or when you want to keep your analysis up to date with minimal effort. Also, you can refresh your query to get the latest data from the source at any time, on demand or on a schedule.
Now that we've established what Power Query is and its potential applications, let's see how you can get this useful feature in your Excel.
The Power Query tool is available in all modern versions, spanning from Excel 2010 to Excel 365. Depending on the specific version you are using, you may need to install it as an add-in or access it directly from the ribbon.
Note.. In Excel 2007 and earlier, Power Query is not supported.
In newer versions of Excel, Power Query is a built-in feature, readily accessible without any additional installations. It can be found on the Data tab under the Get & Transform Data group. To start using its features, just click on any of the commands in this group.
In older Excel versions, Power Query is not included by default, but you can download and install it as an add-in from the Microsoft website.
After installing the add-in and restarting Excel, you should see a new tab in the ribbon labeled Power Query. To start importing data from various sources, click one of these buttons: From File, From Database, From Web or From Other Sources.
Tip. Be sure to download the correct package for your version of Excel and operating system. To check your version and bitness (32-bit or 64-bit), go to File > Account > About Excel.
The heart and soul of all data transformations lies within the Power Query Editor. It is a separate graphical interface within Excel that plays host to your queries. It is the primary workspace where you'll spend most of your time refining and reshaping your data.
You can access the Editor in two ways:
When you connect to a data source or create a new query, the Editor opens automatically.
Here are the six main elements of Power Query Editor:
The Advanced Editor is a text-based interface that allows you to write and edit M code, the language behind Power Query. Think of it as your VIP pass to the Power Query transformation backstage. While the standard PQ interface enables you to reshape data with point-and-click actions, the Advanced Editor gives you precise control over your data transformations.
Every action you perform in Power Query generates a corresponding code in the background, using the M Language. You can view and edit this code in the Advanced Editor. To open it, click the Advanced Editor button on the Home tab, in the Query group.
This section provides an overview of how to use Power Query for data analysis. You will learn the main steps involved in importing, transforming, combining, and exporting data. These steps are:
To start working with your data in Power Query, you need to import it from a source. PQ supports many different data sources such as files, databases, online services, and more.
The three most common sources can be accessed directly from the Excel ribbon - on the Data tab, in the Get & Transform Data group. These primary sources are:
If you need to import data from other sources, click the Get Data button, and then explore further options:
For example, to use a CSV file as a data source, you can either click the From Text/CSV button on the ribbon or go a longer way: Get Data > From File > From Text/CSV. If you have multiple CSV files to combine, save them in one folder and choose the From Folder option.
In the next screen, you'll see a preview of the imported data, along with these choices:
If you aim to reshape your data, the Transform Data option is the logical next step.
Once your data is loaded into Power Query, you're in the driver's seat to refine it according to your needs. All data transformations are done within a separate interface called the Power Query Editor, where you have four main tabs to explore. You can think of data transformation as sculpting. You start with a raw material that you shape into a form that suits your purpose and that allows you to create appealing reports and dashboards.
Here are some common transformations you can perform using Power Query:
These transformations empower you to sculpt your data into a work of art that's not only functional but also elegant and insightful. For detailed instructions, refer to this article: How to use Power Query in Excel – practical examples.
Note. All modifications you make in the editor, collectively forming a query, constitute a new representation of the original data. When you refresh a query, each step runs automatically, eliminating the need to manually repeat the same actions. However, to use your refined data in Excel, you must load it into your workbook.
One of the most useful features of Power Query is the ability to combine data from different sources into a single table. There are two main ways to do this: append and merge.
To combine data in the Power Query Editor, use the Merge Queries or Append Queries command, on the Home tab, in the Combine group.
To combine data that is not in Power Query yet, click Get Data > Combine Queries on the Data tab of the Excel ribbon, and then choose Merge or Append.
For a detailed end-to-end example of merging two tables by a common column, see How to join tables with Power Query.
Similar to using functions and formulas in Excel worksheets, PQ also has its own set of functions to perform various calculations. These formulas are written in the Power Query formula language, also known as M. Now, here's the great news: you don't need to learn the intricacies of M language syntax to build your custom formulas. Unlike traditional coding, Power Query makes it wonderfully simple and user-friendly using a visual interface.
Let’s take an example. Imagine you want to create a column that calculates the net donation amount after tax deduction based on the values in the Donation and Tax Rate columns. The steps are:
=[Donation] * (1 - [Tax Rate])
To refer to a specific column in the formula, select its name under Available Columns on the right side of the dialog and click Insert, or just double click the column name.
That's it! You've successfully created a custom column with a formula in Power Query. The formula bar shows its syntax:
All the transformations you apply to your data connections collectively constitute a query, which is a new representation of the original (and unchanged) data source. When you refresh a query, each step runs automatically. Queries replace the need to manually connect and shape data in Excel.
Once you've performed the necessary data transformations within Power Query, the final step is to load the resulting data into your Excel workbook. You have two options:
The following steps show you how to do it from the Power Query Editor:
If you select the first option, the loading process is already complete. If you choose the second option, proceed to the next step.
When working with Power Query, you may need to refresh your queries from time to time to get the latest data. Here are some ways to do that:
To update the data displayed in the Power Query preview, simply click the Refresh Preview button on the Home tab of the editor:
This will reload the data from the source to reflect any changes made to your original data. All the changes you’ve made to the query steps will be applied to the new data automatically.
To refresh the Power Query results that have been loaded into an Excel workbook, do one of the following:
If you need Power Query in Excel to refresh automatically without having to open the source file, you can schedule these refreshes at specified intervals. Here's how to set it up:
That’s it! From now on, your query will be refreshed automatically every time you open the workbook or at the specified time interval, whichever comes first. This way, you can always have the latest data in your Excel file without manually refreshing it.
These are the essentials of Excel Power Query - your secret weapon for making data work for you. Now, you can easily connect to various data sources, transform, combine and clean your data, and then load it into Excel or Power BI. And don't forget the cherry on top – automatic refreshes that keep your data always up-to-date, even when you're not looking :)
The above is the detailed content of Excel Power Query tutorial for beginners: how to get, use and auto-refresh. For more information, please follow other related articles on the PHP Chinese website!