Excel Power Query For Beginners
Excel Power Query For Beginners
2
Excel: Power Query for Beginners: A Step by Step Guide
1st edition
© 2022 Saskia Giessen & bookboon.com
ISBN 978-87-403-4261-1
Name and title of reviewer: Hiroshi Nakanishi
3
EXCEL: POWER QUERY FOR BEGINNERS Contents
CONTENTS
Preface 6
4
EXCEL: POWER QUERY FOR BEGINNERS Contents
Index 73
5
EXCEL: POWER QUERY FOR BEGINNERS Preface
PREFACE
This manual was written to accompany our seminar on Microsoft Power Query. Its target
audience is users who need to import external data into Excel or transform data into Excel
before further processing.
The exercises in the book are easy to understand and can be adapted with little effort to
your own needs.
Power Query has very comprehensive feature set which is also easy to use. The software can
do a great deal of work for you without requiring any programming skills.
The output of Power Query is a list that you can then use for further analysis in Excel or
in the data model of PowerPivot.
You can download the sample files from the following website:
https://bit.ly/3xxFXR1
Saskia Gießen
Cologne, Germany
April 2022
6
EXCEL: POWER QUERY FOR BEGINNERS Introducing Power Query
Power Query is an Excel add-in that runs in its own task. The content you create in Power
Query are called Queries and stored in the current Excel workbook.
The add-in is a master of ETL, a widely used acronym for Extract, Transform and Load.
Power Query can access a wide variety of data sources. The following table provides an
overview and shows the file types in the group Get & Transform Data.
7
EXCEL: POWER QUERY FOR BEGINNERS Introducing Power Query
Online
Web File Database Azure Other
Services
PostgreSQL Microsoft
Exchange
Sybase ODBC
Teradata OLEDB
Table 1.1: Power Query can work with these diverse data sources
The toolbox is brimming with a variety of tools. Here are some of the available transformations:
8
EXCEL: POWER QUERY FOR BEGINNERS Introducing Power Query
In the following chapters all these actions and commands will be explained and illustrated
with practical examples. The commands can be used in combination, and this makes Power
Query truly a master of data transformation.
9
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
This chapter provides information about how to import external data using Power Query.
The import is described in an example using the text file shown below.
During the import process, you work in a window separate from Excel called the Query Editor.
The Editor window provides additional information about Power Query. There are several
ways to transfer your data to Excel after the external data has been prepared and transformed.
10
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
In the Import Data window, choose the file containing your data and click Open.
A window opens with the file name as its title. You can now make several settings that you
later no longer need to repeat in Power Query.
In most cases, Power Query recognizes the File Origin and the Delimiter. For this example,
the data type detection is not used because the first four lines of the file either contain text
or are empty.
By clicking on the Load button, you would have imported the file as it is to Excel.
11
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
You can edit the retrieved data using the tabs Home, Transform, Add Column and View.
The Navigator Pane on the left shows all the queries in the Excel workbook. You can
expand or collapse it with one click.
In Query Settings on the right, you can enter a descriptive name in the Name field of your
query. In the APPLIED STEPS section, you will see a list of the commands that you or
Power Query has performed.
The status bar tells you how many columns and rows have been retrieved.
Directly above the status bar you can see the content of the selected cell. If you select an
entire column, this area disappears.
12
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
Info: The command View / Formula bar shows or hides the formula bar. The formula bar
displays the command selected in the query settings area.
Note that Power Query does not change the data in the original file! All the editing
steps you do in Power Query are done on a copy of your data.
13
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
The first four lines have now been removed. In the preview, the remaining lines slide upwards.
In the edit bar and in the APPLIED STEPS area on the right you see your command
Removed Top Rows.
By clicking on the icon to the left of the Removed Top Rows command, you can undo
this command. Click on the gear in the APPLIED STEPS pane to change the number of
lines the command removes.
14
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
In the second step, you can use a command to move the contents of the first line into the
column titles. By default, Power Query labels columns with Column1, Column2, and so on.
Figure 2-8: Using the first row of the table as column headers.
Now the first row of the table has moved into the column headers.
Download now
15
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
Figure 2-9: The data types are displayed in the column headers
Excel can only calculate if the data type is correct, so you should choose the correct data
type for each column in this step.
In the Date column, click the icon and select the data type DATE.
Confirm the question about changing the data type with Replace current.
Select the column Gross_profit and choose the data type Currency.
Info: Power Query does not assign a currency symbol to the values, even if you select the
Currency data type. You will need to do this later in Excel.
Here you can see from the icon in the column title which data type you have selected.
16
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
The pane APPLIED STEPS on the right summarizes your last three actions in a Changed
Type1 command.
Figure 2-11: The correct data types have now been assigned to the columns
In the final step we remove unneeded columns from the data import.
Click in the No_Material column and use the Home / Manage Columns / Remove
Columns command.
Select the Cat_No column, hold down the >6KLIW@ key, and click the Cust_City
column. Press the >'HO@ key.
Remove any empty columns.
Figure 2-12: Removing some of the columns from the import process
As before, Power Query saves these steps in the APPLIED STEPS section. In this case it
is the Removed Columns command.
This is all the processing we need. Your table is now ready to be transferred to Excel.
17
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
All the commands you have run are displayed in the APPLIED STEPS area on the right.
You can show or hide this area using the View / Query Settings command.
When you click on a command, the Power Query displays the state of the data after that
command is executed.
Figure 2-13: The state of the data after the Promoted Headers command
If you select the last step, you see the final state of your work. When the data is transferred
to Excel, it is always transferred in this final state, regardless of which command is currently
selected.
Important! If a processing step is selected other than the last one in the list and you insert
a command, for example Removed Columns, the following message box appears:
Figure 2-14: Power Query warns you before inserting an intermediate step
Power Query asks if you want to insert this step at the highlighted position.
Commands with a gear to the right can be edited by clicking on the icon. For example,
this allows you to change the path or filename for the Source command.
18
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
For the Removed Top Rows command, click on the gear to change the number of lines
to be removed.
Info: You can change the order of steps using the context menu with the commands Move
Up or Move Down. The context menu also contains a Properties dialog. This allows you
to document each step separately.
Before you import the data to Excel, take a quick look at the commands that Power Query
recorded for you. The steps are detailed in the programming language M.
The Advanced Editor allows you to make changes to your script as well as just viewing it.
For example, you might want to keep one of the columns deleted in the script. If you click
the button in front of the Removed Columns command, all columns will be displayed
again. But you can restore a single column in the editor. This is done by deleting just the
column name and its accompanying syntax, which is the quotation marks and the comma
separating the column names.
19
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
When you have finished editing, you can transfer the data to Excel. This creates a workbook
query in Excel, which is stored in the Excel workbook.
In the Home tab, click on the upper part of the Close & Load button.
This closes the Query Editor and inserts the data as a dynamic table on a new worksheet.
Info: To get back to the Query Editor, select Query / Edit or Data / Get & Transform
Data / Get Data / Launch Power Query Editor.
Figure 2-17: The imported data and the tabs Design and Query in Excel
Info: Later in this chapter we’ll dive deeper into how to handle a query in Excel and how
to use a dynamic table.
This step increases the physical size of the spreadsheet file. The imported data is now
contained in the workbook.
20
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
Now if you go back to Power Query with the Query / Edit command and click on the
lower part of the Close & Load button, the Close & Load To command is inactive.
Power Query itself does not allow you to change from your original choice. But not to
worry, in Excel there is still a way to change this option.
There are four variations on the way you transfer data. The following sections provide an
overview of all four variations.
The first variation transfers the whole table to a new worksheet. This is the step you have
already done above.
This closes the Query Editor and inserts the data as a dynamic table on a new worksheet.
21
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
To insert the table in an existing worksheet of your choice, perform the following steps:
In the Power Query Editor, choose Home / Close & Load To.
Choose the option Existing worksheet.
Power Query provides a cell reference as the starting point for the data insertion. You can
edit the cell reference.
Click on the sheet and in the cell where you want the data inserted.
As in the first case, the table is inserted as a dynamic table. The only difference to the first
variation is that you can determine the place where the table is inserted.
If you want to analyze a table in PowerPivot, you can push it directly to the PowerPivot
data model.
22
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
Figure 2-20: Pushing data to the spreadsheet and into the Data Model at the
same time
As explained above, the table is transferred as a dynamic table to Excel. At the same time,
it is inserted into the data model.
To check this, select Data / Data Tools / Go to the Power Pivot Window. If you
are starting PowerPivot for the first time, click OK.
23
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
If you pass the table to both Excel and the data model, the file size is greatly expanded.
There are two sets of data in the workbook. If you only need the data in PowerPivot, select
Only Create Connection, which is described in the next section.
The last variation creates a connection to the data. The data is not shown in Excel on a
worksheet and the file size does not change.
24
EXCEL: POWER QUERY FOR BEGINNERS Creating your first query
There is only a data connection to the information in Power Query. The data can still be
used later in various ways.
In the Query & Connections section, right-click the query that you want.
Select the Load To command.
The Import Data window opens, and you can change your selection.
Info: The command Data / Query & Connections show the task area if it is hidden.
Download now
25
IMPORTANT INFORMATION ABOUT
EXCEL: POWER QUERY FOR BEGINNERS WORKING WITH QUERIES
3 IMPORTANT INFORMATION
ABOUT WORKING WITH QUERIES
Each query is stored in the worksheet where it is visible. But the Connection queries works
a little differently. They are stored directly in the workbook. This section assumes that you
have imported the table into a worksheet.
On the right edge of the screen, you should be able to see all the workbook queries in the
current Excel workbook. Click on a query to display the worksheet with the corresponding
table.
If you do not see this area, choose Data / Query & Connections.
Info:
Open the Power Query Editor by double clicking the query.
When you point to the query name, the information for the current query is displayed
out. This section is called Peek. You get a preview of the data and more information about
the query.
Figure 3-1: The Peek shows you all the information about the query
26
IMPORTANT INFORMATION ABOUT
EXCEL: POWER QUERY FOR BEGINNERS WORKING WITH QUERIES
VIEW IN WORKSHEET activates the worksheet with the table of the query. Clicking
EDIT opens the query editor.
If you click on the three dots, you get a selection of additional commands.
When you click Delete, the query is deleted, but the table remains. The Query register
disappears, and 0 Query is displayed in the Query & Connections pane.
The commands in the Peek are also available in the context menu when you right click the
query in the Query & Connections area.
27
IMPORTANT INFORMATION ABOUT
EXCEL: POWER QUERY FOR BEGINNERS WORKING WITH QUERIES
After you call the command to delete the worksheet via Home / Delete / Delete Sheet
and confirm the first security prompt with a click on Delete, Excel sets up a connection
to the data for you. That means the query is still there.
By right-clicking on the query and using the Load To command, you can use the Table
and New Worksheet options to re-insert your data as a dynamic table.
28
IMPORTANT INFORMATION ABOUT
EXCEL: POWER QUERY FOR BEGINNERS WORKING WITH QUERIES
Once you have started working with the product, and you have multiple queries in a
workbook, Excel offers the option of presenting them in a structured manner.
Right-click the query for the context menu, and then create a folder using the Move to
group / New group command. Now you can move queries to this folder. Note that this is
only possible via the context menu. Once you have created a folder, all remaining queries
will be moved to the Other Queries folder.
You can change the order of the queries in the group with the Move Up and Move Down
commands.
When you open an Excel workbook that contains a query, you are warned that there are
external connections in the workbook.
You may also see a warning that your Excel spreadsheet is receiving data from a network.
Enable the content.
If you ignore the security warnings and you try to refresh or edit the query, the following
message appears:
29
IMPORTANT INFORMATION ABOUT
EXCEL: POWER QUERY FOR BEGINNERS WORKING WITH QUERIES
Now you need to click the Yes button to work with the query.
If you have transferred the table to Excel, the two tabs Query and Design are available in
the worksheet tabs at the top of Excel.
When the cursor is in the table and you scroll down, the first line does not scroll with the
rest of the table. The column headers are displayed as column titles.
You can change the colors in the Table Styles command group on the Design tab.
You can make further adjustments in the Table Styles Options command group to the left,
such as Header Row, Banded Rows, First Column, Last Column or Banded Columns.
The filter is activated by default for the dynamic table. Use the Filter Button checkbox to
disable it.
30
IMPORTANT INFORMATION ABOUT
EXCEL: POWER QUERY FOR BEGINNERS WORKING WITH QUERIES
Hold down [Ctrl] and select one or more entries to filter for. You can also use the
Multi-Select button to enable multiple entries.
Check the Total Row check box in the Table Style Options area.
31
IMPORTANT INFORMATION ABOUT
EXCEL: POWER QUERY FOR BEGINNERS WORKING WITH QUERIES
The last column of the table is aggregated in the total row. If the column is numerical, the
default calculation is a sum. Otherwise it is a count.
When the table is refreshed and new rows of data are added, the result line automatically
moves down and the calculations are updated.
When you create a dynamic table, it automatically gets a range name. The name is visible
and can be modified in the Table Name field on the Design tab.
32
EXCEL: POWER QUERY FOR BEGINNERS Error! Use the Home tab Editing
to applycell
Überschrift
content 1 to
the text that you want to appear here.
Editing
4 cell content
EDITING CELL CONTENT
In mostcases,
In most cases,imported
imported data
data has has
to betoedited
be edited (or »transformed«)
(or »transformed«) before itbefore
can beitanalyzed.
can be analyzed.
The tools in
The tools in Power Query let you split, merge, replace, and
Power Query let you split, merge, replace, and cleanse cell content.
cleanse cell content.
Open
Openthethe Chapter4_1.csv.
filefile Chapter4_1.csv.
This chapter
chapterwill
willintroduce
introduceyou
you
to to a set
a set of of commands
commands thatthat allow
allow you
you to toyour
edit edit data
yourefficiently.
data efficiently.
Figure 4-1:
Figure 0-1:There
Thereareare several
several problems
problems withtable
with this this table
The No_Material column contains the material number and name together in a single cell. Some of the
The
namesNo_Material column contains
of the sales representatives in thethe materialcolumn
Rep_Name number anddigits
have name together
in them. The in a single
column cell.
Cust_City
Some ofthe
contains thecustomer
names ofname
the and
saleslocation
representatives
separated in
bythe Rep_Name column have digits in them.
a hyphen.
The column Cust_City contains the customer name and location separated by a hyphen.
Power Query helps you deal with these problems without having to enter a single formula. But before we
Power Query
get started, we’llhelps
have you deal
a look withtothese
at how move problems without
data from an having
open Excel to entertoa Power
spreadsheet singleQuery.
formula.
But before we get started, we’ll have a look at how to move data from an open Excel
spreadsheet to Power Query.
Bringing Excel data to Power Query
The steps to get data from Excel into Power Query are converting the table to a dynamic table, renaming
it, and then passing it to Power Query.
4.1 BRINGING EXCEL DATA TO POWER QUERY
Click anywhere in the table and press [Ctrl] + [T].
The steps to get data from Excel into Power Query are converting the table to a dynamic
Make sure that Excel automatically included all the cells in your table in the Create Table window.
table, renaming it, and then passing it to Power Query.
39
33
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
Figure 4-2: Checking which cells will be included in the dynamic table
In the last chapter we saw how to handle a dynamic table. The only editing step you need
is naming the table. Power Query uses this name as the query name.
Leave the cursor in the table and click on the Design tab.
Enter the new name, List1, in the Table Name field and press [].
Now the table is just one click away from Power Query.
34
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
This command transfers the data in the table to the query editor. The name of the table is
used as the query name by default.
35
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
Figure 4-6: The material number and name don’t have any separator
Right-click the column title No_Material and select the command Duplicate
Columns.
The new column appears beside the table to the right of the last column.
Drag the new column titled No_Material-Copy and drop it to the right of the
column No_Material.
Leave the No_Material-Copy column highlighted and select Split Column / By
Number of Characters on the Home tab.
Enter 5 in the field Number of characters to split out the five digits of the material
number. It’s important to activate option Once, as far left as possible. You need this
option because you know that column has to be split after the first five characters.
36
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
The column splits into two columns. The contents are separated at the position specified.
The titles of the new columns are No_Material - Copy.1 and No_Material - Copy.2. Power
Query has also converted the data type of the first column to Whole Number.
Right click each column and use the Rename command to change their names to
Material_No and Material.
You now have two new columns containing the numbers and names separately. In addition,
you kept the original column by copying.
Select the column Rep_Name. Choose Home / Replace Values. The command is
also available in the context menu with the right mouse button.
Enter 1 in the Value To Find field.
Leave the Replace With box empty.
37
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
The values in the column Rep_Name have now been cleaned up.
38
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
Now the values in the columns Cust_City are split into two columns.
39
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
40
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
4.6 SORTING
In the last step, you want to sort your data. First, sort by sales representative name. For
each sales representative, the data should be sorted again by the material.
Click the column Rep_Name and choose Home / Sort Ascending . You can
also use the filter symbol.
For the Material column, click the filter symbol und choose the command
Sort Ascending.
The content of the Rep_Name column is now sorted alphabetically. The Material column
is also sorted by material name for each sales representative.
41
Error! Use the Home tab to apply Überschrift 1 to
the text that you want to appear here.
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
Open
Openthethe Chapter4_1.csv.
filefile Chapter4_2.csv.
Make
This chapter willthe table you
introduce a dynamic table
to a set of and name
commands it List2.
that allow you to edit your data efficiently.
The No_Material column contains the material number and name together in a single cell. Some of the
names of the sales representatives in the Rep_Name column have digits in them. The column Cust_City
contains the customer name and location separated by a hyphen.
Bringing
Power Excel
Query data
queries canto
bePower Query
copied and pasted into other workbooks.
The steps to get data from Excel into Power Query are converting the table to a dynamic table, renaming
Switch
it, and then toitthe
passing workbook
to Power Query.with the query and double-click the query or select Query
/ Edit.
Select the query in the left pane and copy it.
Click anywhere in the table and press [Ctrl] + [T].
Close Power Query and switch to the new workbook.
Make sure that Excel automatically included all the cells in your table in the Create Table window.
Select the command line Data / Get & Transform Data / Launch Power Query
Editor. This starts Power Query without a query.
Right-click in the left query area and select the Paste command.
39
42
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
In the first line of the query, Power Query accesses the dynamic table List1. Your dynamic
table is called List2.
Modify the code in the first line of List1 to List2. Be careful not to delete the
quotes.
Click Done.
Change the name of the query.
Now the query will run again, and you will see the imported table.
43
EXCEL: POWER QUERY FOR BEGINNERS Editing cell content
Now you can transfer the data to Excel with the Close & Load command on the Home
tab and continue editing there.
This shows how queries can be copied and used to modify other data.
44
EXCEL: POWER QUERY FOR BEGINNERS Import Several Files in one Step
This is what the folder contains. Not all the files will be used for importing.
The column structure of the four Excel files is different as is the number of rows of data.
What is important is that the name of the worksheets is identical.
Create an empty workbook and use the command Data / Get & Transform Data /
From File / From Folder.
45
EXCEL: POWER QUERY FOR BEGINNERS Import Several Files in one Step
In the Folder window, click on the Browse button and select the folder where the
files you want to import are located.
Power Query returns a list of the files in the selected folder. First get all files for Power
Query and then exclude the unnecessary files.
46
EXCEL: POWER QUERY FOR BEGINNERS Import Several Files in one Step
Figure 5-4: These are the files Power Query found in the selected folder
47
EXCEL: POWER QUERY FOR BEGINNERS Import Several Files in one Step
Now there are only Excel files in the list. Power Query protocols your choices in the Query
Settings area on the right.
Before clicking on the button in the Content column, consider which columns you
want to move and in what order you want to move them. The first file in the list specifies
the order of the columns. If one of the other files has an additional, it will appear after the
column of the first file.
Click the button with the two arrows in the first column, Content .
Select the table where the data you want to import is located. In this example, it
is the Data table.
48
EXCEL: POWER QUERY FOR BEGINNERS Import Several Files in one Step
In the left pane, you can see that Power Query has done a lot of work for you, putting
these four files together.
Figure 5-9: The content of the four files in the Query Editor
The first column contains the file name of the source of the data in each row. You don’t
need this information, so you can delete the column.
49
EXCEL: POWER QUERY FOR BEGINNERS Import Several Files in one Step
Figure 5-11: The content of the four Excel files in one list
Now you can continue editing the data with the standard Excel tools. For example, you can
use a PivotTable to determine how much revenue you have generated with each material.
50
EXCEL: POWER QUERY FOR BEGINNERS Extracting data from an Excel spreadsheet
However, you only want to evaluate the Date, Region, Distributor, Weight, Product_extern
and Supply_by fields.
In addition, you need two calculated columns. You want to calculate the total costs from
columns E to K (Cost_1 to Cost_7) as well as the difference between the fields Order
and Supply.
51
EXCEL: POWER QUERY FOR BEGINNERS Extracting data from an Excel spreadsheet
There are two worksheets in the sample workbook. Select the Report_LOG sheet
in the Navigator window.
Figure 6-2: Select the spreadsheet you want to import data from
Info: An Excel table can contain a maximum of 1048576 rows and 16384 columns.
52
EXCEL: POWER QUERY FOR BEGINNERS Extracting data from an Excel spreadsheet
First remove the first five lines from the Home / Reduce Rows / Remove Columns /
Remove Top Rows command.
Next, use the Use First Rows as Headers command to create the correct headings.
Assign the appropriate data types to the columns. To do this, click on the data
type symbol of each column and select the desired type.
The column Date should have the data type Date.
The columns Cost_1 to Cost_7, Order, Supply and Weight are given the data
type Decimal Number.
53
EXCEL: POWER QUERY FOR BEGINNERS Extracting data from an Excel spreadsheet
The result is a new column that is the sum of the selected columns.
In the second calculation, you want to determine how high the loss is in deliveries. To do
this, calculate the difference between the Order and Supply fields.
54
EXCEL: POWER QUERY FOR BEGINNERS Extracting data from an Excel spreadsheet
Now you just remove the columns you don’t need. You can remove a column by right-
clicking and using the Remove command.
55
EXCEL: POWER QUERY FOR BEGINNERS Extracting data from an Excel spreadsheet
Info: In this step you only delete the columns in Power Query. The original Excel spreadsheet
is not touched.
You can also highlight the columns listed above and use the Remove Other Columns
command in the context menu.
This completes the processing of your data in Power Query. You have reduced the number
of columns from 41 to six. Of course, you could have done the calculations in the two
additional columns in Excel as well. But this would have required you to import all nine
Cost columns.
Click the Close & Load button on the Home tab and watch Excel import the
records.
56
EXCEL: POWER QUERY FOR BEGINNERS Extracting data from an Excel spreadsheet
Figure 6-11: You can see a clear difference in the file size before and after
importing the data
The file edited by Power Query is only a fraction of the size of the original file.
Click in your dynamic table and select Design / Summarize with Pivot Table or
Insert / PivotTable.
The dynamic table is called Report_LOG. This will be the basis of your PivotTable.
57
EXCEL: POWER QUERY FOR BEGINNERS Extracting data from an Excel spreadsheet
If Excel does not calculate the sum, right-click on the result and choose Summarize Values
By / Sum.
Mark the cell with the sum and assign the cell the number format with thousands
separators. Set the number of decimal places to zero.
Now you have summarized the total weight of the goods you have sent in a single number.
You can see immediately which freight forwarder has delivered the greatest weight.
Right click any value in the Sum_of_Weight column and select Sort / Sort Largest
To Smallest.
You may also want to change the layout and formatting of the table using the Design tab.
58
EXCEL: POWER QUERY FOR BEGINNERS Extracting data from an Excel spreadsheet
Figure 6-14: The result: The list of freight forwarders and the weight of
goods each delivered
59
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
7 MERGING MULTIPLE
SPREADSHEETS
In this chapter you will learn how to merge several tables together.
Error! Use the Home tab to apply Überschrift 1 to
the text that you want to appear here.
In this example we transfer four tables from a workbook to Power Query. The example
We also demonstrate that when merging tables, the order of the columns in Excel does not
matter. The reason
In most cases, is that
imported Power
data has Query
to be editedis(orbased on column
»transformed«) titles.
before In addition,
it can it does
be analyzed. not in
The tools
matter if one table has more columns than another table.
Power Query let you split, merge, replace, and cleanse cell content.
Open
Openthethe Chapter4_1.csv.
filefile Chapter 7.xlsx.
This chapter will introduce you to a set of commands that allow you to edit your data efficiently.
The No_Material column contains the material number and name together in a single cell. Some of the
names of the sales representatives in the Rep_Name column have digits in them. The column Cust_City
contains the customer name and location separated by a hyphen.
Power Query helps you deal with these problems without having to enter a single formula. But before we
get started, we’ll have a look at how to move data from an open Excel spreadsheet to Power Query.
60
39
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
It sometimes happens in practice that users change the table layout. If you only work with
copy and paste, this can easily create problems.
The Base Price column does not appear in all the tables.
Now let’s transfer the content of the first table to Power Query. Power Query only recognizes
dynamic tables, so the first step is to convert the table to a dynamic table.
Activate the worksheet 2017_12 and click any cell in the table.
Press [Ctrl] + [T], check the cells Excel finds, correct them if necessary, and confirm
with OK.
Excel now converts the data into a dynamic table. It is named Table1 by default. This name
is not very meaningful, which matters because it will later be used as the name for the query.
61
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
Info: The table names must not start with a number or contain spaces. You also need to
complete the entry with [].
Activate the Data tab and click the From Table/Range button in the Get &
Transform Data area.
The table is displayed in the query editor, and you can see the query name in the Navigator
Pane on the left. When there are multiple queries in the current workbook this area is very
important, because it displays a complete list.
62
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
For the time being no changes to the data structure are needed, since you have not merged
all the data yet. The structure changes come at the very end.
To get the second table into the editor, you must now move the first query to Excel. That
is the only way to get back to Excel.
All the original data is in Excel. There is no need to paste another copy into the current
workbook. It would increase the file size unnecessarily.
Your first query is listed on the right side of the screen. Since it’s just a connection, you
will not see any other information about the table.
63
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
64
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
The query that you have currently selected is already selected and that means it is the
primary table.
Since there are three queries in this example, you need the Three or more tables
option.
All available queries are visible on the left in the list box Available table(s).
Info: If »Y2018_2« is first in the field Tables to append, then its data is inserted first and
the column Base Price is in the fifth position. If the field Tables to append contains the
query »Y2017_12«, then the Base Price column is at the last position.
The query data is merged together in the order of the tables in the Tables to append field.
But because Power Query also provides you with the Sort command and the ability to
move columns, the order of the queries does not matter.
65
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
You can already see the new query in the pane on the left. It has been named Append1.
The Base Price column is in the fifth position. Up to line 160, you can see the contents
of query Y2018_2. From line 161, you see the contents of the query Y2018_1. From line
186, you see the contents of the query Y2017_12. Since the Base Price column does not
exist in query Y2017_12, Power Query has entered the value null there.
66
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
First, the null values should be replaced by 0 in the Base Price column. Then the data
types are adjusted for all columns.
Info: Pay attention to capitalization on the Value To Find field. If you enter the word
NULL, a warning appears to the right of the field. It says that this word is not found
in the active column.
Now there are numbers in all cells of the Base Price column.
67
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
Delete the columns Mat No and Cat no. The quickest way to do this is to highlight
the column and press the [Del] key.
Figure 7-17: Three tables merged into one
Power Query gives you a good overview of your queries and their dependencies on each other.
Figure 7-18: Overview of the queries and their dependencies
68
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
Assign the data type Currency to the Base Price, Net Price and Net Sales columns.
Rename the worksheet to Sales_Volume.
The next step is to insert another table into the query Sales. This requires several steps.
If you do not perform the last step of returning to Excel as a connection, the data will be
passed to Excel as a table. This is not necessary because this data is already available as a table.
• Double-click on a query.
The new query Y2018_3 has now been transferred.
69
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
The next step is to integrate the new query in the existing query Sales Volume.
If you do not see the pane, activate the View tab and select the command Query Settings.
In the field Available table(s) on the left, select the new table Y2018_03.
Click the Add button.
This appends all data from query Y2018_3 to the Sales Volume query. If you want to see
this new data at the top, follow these steps:
Select query Y2018_3 in the Tables to append area and click on the button
until the new query is at the top.
70
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
In the status bar you can see the new number of lines. Now only the new data has to be
transferred to Excel.
In the Queries & Connections area, you can see the new number of rows.
71
EXCEL: POWER QUERY FOR BEGINNERS Merging multiple Spreadsheets
You could move the four connection queries to the sources into a folder called Data Source.
And the result query can be stored, for example, in the folder Result. Use the right mouse
button for all these commands.
The Other Queries folder appears automatically and cannot be deleted. New queries
automatically land in this folder.
72
EXCEL: POWER QUERY FOR BEGINNERS Index
INDEX
A Q
Advanced Editor 9, 19, 43, 73 Query Dependencies 68, 74
all files from a folder 73
R
Append Queries 60, 64, 65, 73
APPLIED STEPS 12, 14, 17, 18, 73 Rename 37, 39, 69, 73
Replace values 74
C
S
content 7, 9, 12, 29, 33, 35, 39, 41, 49, 50, 61
Copy query 73 Split 9, 36, 39, 73, 74
Create Table 33, 73 Split Column by Delimiter 74
Split columns 74
D Split text 74
Delimiter 11, 39, 73, 74
T
E table 7, 15, 17, 20, 21, 22, 23, 24, 25, 26, 27, 28,
Extension 47, 73 30, 31, 32, 33, 34, 35, 36, 42, 43, 48, 49, 51,
52, 57, 58, 60, 61, 62, 63, 64, 65, 69, 70
F
Table
filter 8, 30, 31, 41, 47, 51 primary 74
Filter
Extension 47
folder 10, 29, 45, 46, 47, 72, 73
From Table/Range 34, 62, 73
G
gear 14, 18, 19, 70, 73
I
import 6, 7, 10, 17, 19, 45, 46, 47, 48, 52, 56, 69
M
Multiple Spreadsheets 73
O
Only Create Connections 63, 73
P
PowerPivot 6, 21, 22, 23, 24, 74
primary 65, 74
Primary Table 74
73