0% found this document useful (0 votes)
194 views

Excel Power Query For Advanced

The document describes how to use Power Query to combine data from multiple Excel tables using a key field, similar to a VLOOKUP function. It demonstrates selecting multiple tables, merging them based on a common field, and loading the results back to Excel. It then shows creating a pivot table from the combined data.

Uploaded by

skarbekjo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
194 views

Excel Power Query For Advanced

The document describes how to use Power Query to combine data from multiple Excel tables using a key field, similar to a VLOOKUP function. It demonstrates selecting multiple tables, merging them based on a common field, and loading the results back to Excel. It then shows creating a pivot table from the combined data.

Uploaded by

skarbekjo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

Saskia Gießen

Excel: Power Query for


Advanced
A Step by Step Guide
SASKIA GIESSEN

EXCEL: POWER QUERY


FOR ADVANCED
A STEP BY STEP GUIDE

2
Excel: Power Query for Advanced: A Step by Step Guide
1st edition
© 2022 Saskia Giessen & bookboon.com
ISBN 978-87-403-4264-2
Name and title of reviewer: Hiroshi Nakanishi

3
EXCEL: POWER QUERY FOR ADVANCED Contents

CONTENTS
Preface 5

1 VLOOKUP in Power Query 6


1.1 Bringing data together 6
1.2 Create a pivot table from the data 13

2 Understanding and using Join types 16


2.1 Left Outer 17
2.2 Right Outer 20
2.3 Full Outer 22
2.4 Inner Join 25
2.5 Left Anti 26
2.6 Right Anti-Join 28

3 Unpivot data – convert results back to a list 31


3.1 Creating a List from a Pivot Result 31
3.2 Transferring the data to Excel 34

4 Many useful tools in Power Query 39


4.1 Recognizing duplicates 39
4.2 Recognize duplicate values in Excel 41
4.3 Deleting Duplicate Values in Power Query 42
4.4 Create cross table 46
4.5 Distinct Count - count unique values per month 49

5 Calculations and more 53


5.1 The Text Start function 54
5.2 The Function Text.Range 57
5.3 The Function Text.PositionOf 58
5.4 Nested functions 61
5.5 Changing the Data Type for the EAN Column 64
5.6 The If Function in Power Query 64
5.7 Evaluations with date values 68

6 Data from the Web 71


6.1 Analyzing the Wikipedia website 71
6.2 Transfer the list to Excel 76

7 Troubleshooting 78
7.1 Error Messages in Queries 78

Index 80

4
EXCEL: POWER QUERY FOR ADVANCED 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/3JSpB7O

Please also read our book Power Query for Beginners

Saskia Gießen

Cologne, Germany

Mai 2022

5
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

1 VLOOKUP IN POWER QUERY


The VLOOKUP() function is very popular in Excel because it brings data together. Using
a key (search criterion), a suitable value is searched for and an information from another
column will be the result.

Power Query does not have the VLOOKUP() function, but the Merge Queries command
is much more powerful and much faster.

In this chapter, you will learn how to take multiple tables from Excel and merge them using
a key. The second example shows you how to process Access tables that are already related
to each other in Power Query.

In the section Understanding and Using Join Types, we introduce you to all six types of
join using sample data.

The practical example illustrates how flexible Power Query deals with different data sources.

1.1 BRINGING DATA TOGETHER


In this example, you have three tables in a workbook. The first table contains sales data.

The other two tables are much smaller and contain master data. In the Data List table,
you have stored the number of the salesperson for each sale. However, the name of the
salesperson is in the Salespersons table.

The material group number is similar. You can find the name of the material group in the
Groups table.

6
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

Figure 0-1: Three tables to be merged

In Excel, you would now use the VLOOKUP() function to assign the salesperson number
to the salesperson name, which is also in another spreadsheet. However, the processing speed
in Excel is slowed down by the VLOOKUP() for large amounts of data.

Now it’s time for Power Query: Here you have an elegant and fast solution.

1.1.1 SELECT THE THREE TABLES IN ONE STEP

Power Query allows you to select multiple tables in a workbook in one step.

• Take a new empty workbook and choose the command sequence Data / Get &
Transform Data / From File / From Workbook.
• Open the file Adv_Chapter1.xlsx.
• Select the Select multiple items option and activate all three tables.

7
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

Figure 0-2: Bring all three tables to Power Query

• Click on Edit.

Figure 0-3: All tables are available in the Power Query-Editor

Power Query has created queries for all tables.

• Check all tables to see if the column headers and data types match.
• In the left part of the editor, select the Datasheet query.
• Choose the command Home / Combine / Merge Queries.
• In the list field, choose the query Sales Reps.
• In both preview areas, select the column with the salesperson numbers Rep_No.

8
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

Figure 0-4: Select the fields in the Merge-Dialog

• Leave the Left Outer (all from first, matching from second) entry activated in
the Join Kind field. This join corresponds to the VLOOKUP() function in Excel.
• Confirm with OK.

All types of links are explained later in this chapter.

On the right a new column with the title Sales Reps has been added.

9
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

Figure 0-5: Load columns from a linked table

• Click on the icon .


• Now select the column(s) you want to see in the Datasheet query. In this
example, it is only the column Rep_Name.
• Remove the check mark from the field Use original column name as prefix . If
you leave the check mark activated, then Sales Reps.Rep_No would be taken as
column header.

Figure 0-6: Set the option to show column Rep_Name

• Click on OK.

10
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

Figure 0-7: The new column with the names from the table Sales Rep

The new column is displayed. Now you see the name of the sales consultant for each sale,
just as the VLOOKUP() function would have done for you. Carry out the same steps for
the material groups:

• In the left part of the editor, select the Datasheet query and choose the
command sequence Home / Combine / Merge Queries.
• In the list field, choose the Category query and select both Cat_No columns.

Figure 0-8: Assigning the category numbers

• Confirm with OK.


• Click on the button in the new Category column and select only the Cat_
Name field for the display.
• Remove the check mark from the Use original column name as prefix field.

11
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

Figure 0-9: Select the group names

• Click on OK.

Now your query data list has two more columns Rep_Name and Cat_Name.

Figure 0-10: The data list is ready

Remove the columns Cat_No to Rep_No.

1.1.2 TRANSFERRING THE DATA TO EXCEL

In the last step, the queries will be loaded to Excel. Note that you have to load all three
queries to Excel.

• Select Home / Close & Load.

12
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

Figure 0-11: All queries are now available in Excel

Power Query has added three spreadsheets and has loaded one query per sheet. Now the
data transfer is finished and you can create your reports.

1.2 CREATE A PIVOT TABLE FROM THE DATA


Now you want to evaluate your data with a pivot table.

• Click in your new Datasheet table and select the command sequence Design /
Summarize with PivotTable. Confirm with OK.
• Drag the Gross profit field to the Values area. Design the sum in the currency
format.
• Drag the Rep_Name field to the Rows area.
• Drag the Cat_Name field to the Columns area.
• Format the values in the currency format.
• Finally, select Design / Report Layout / Show in Tabular Form to see the field
names in the pivot table.

Figure 0-12: Your report

Save your changes once now.

13
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

1.2.1 A NEW SELLER ENTERS THE SCENE

Imagine a new salesperson starting work.

• Open the folder with the original data and activate the Sales Reps tab.
• Enter a new salesperson.

Figure 0-13: The new sales representative

• Activate the Datasheet tab and enter a few records for the new seller.

Zeitmanagement
Finden Sie heraus, wie Sie Ihre Zeit besser nutzen

Download eBooks

14
EXCEL: POWER QUERY FOR ADVANCED VLOOKUP in Power Query

The new salesman Mr. York with the number 8 has started his work.

• Save the changes and close the workbook. Switch to your workbook
with the queries.
• Update the query Sales Reps and then the query Datasheet.

Figure 0-14: The sales of the new seller have arrived

The data rows from the new sales representative has arrived in all tables.

• In the last step, switch to the pivot table and update the data with a right click
and the Refresh command.

Figure 0-15: The updated pivot table

The results for Mr. York has arrived in the pivot table.

15
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

2 UNDERSTANDING AND
USING JOIN TYPES
In this chapter, the usage and the results for the various types of link are presented. To
avoid too much theory, the types of »join data« are explained on examples.

A classic question in practice is the comparison of two inventory lists.

Questions are then:

• Which new lines have been added?


• Which data are no longer in the inventory?
• and more

Figure 0-1: The two tables for the example (Adv_Chapter2.xlsx)

The differences between the two lists are to be compared using the first column No. The
following is to be examined:

- whether there are new lines

- whether there are lines that have been omitted

• Take a new empty workbook and fetch the two tables with the command
sequence Data / Get & Transform Data / From File / From Workbook.
• Activate the Select multiple items checkbox and select both tables.

16
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

Figure 0-2: Import the two tables

• Click on Edit.

Figure 0-3: The queries week50 and week51

With these two queries we will describe all six types of join.

2.1 LEFT OUTER


This type of connection is used very often. It uses a similar method as the VLOOKUP()
function in Excel. There, the matching data from a second list is linked to a given amount
of data if the key values match.

17
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

Figure 0-4: The viewing direction is from left to right.

• Select the query week50 and choose the command sequence Home / Combine
/ Merge Queries / Merge Queries as New.
• In the list field, select the query week51 and in both queries, select the column No.
• In the Join Child field, choose Left Outer (all from first, matching from second).

The name of the join already describes the result: ALL records from the query week50
are displayed, because this query is the left one (the top one in the window). In addition,
all records from the week51 query are shown, where the content of the No column also
appears in the week50 query.

You can already see the result in the Merge window. For the 20 records from the week50
query, there are 15 matching records in the week51 query.

18
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

Figure 0-5: Left Outer join

• Confirm with OK.


• Click on the button in column week50, remove only the tick from the Use
original column name as prefix field and confirm with OK.
• Give the query the name Left Outer.
• Now transfer the data to Excel with Home / Close & Load.

Figure 0-6: The result in a worksheet

19
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

• Rename the worksheet to Left Outer.

As a result, you see all the data for the query week50. Matching this, you see the data
records for the query week51. This means that for five records of the query week50 there
is no counterpart in the query week51.

Info: If you had done this with the VLOOKUP() function in Excel, #NV would appear
in cells H17 through O21.

2.2 RIGHT OUTER


This type of connection is equivalent to the Left Outer, except that the viewing direction
is from the right (lower) data set and all matching data from the left data set is loaded
with matching key values.

Figure 0-7: Direction of view from right to left

• Double-click a query again to switch to the query editor and select the week50
query again.
• Perform all steps as described above.
• In the Join Child field, choose the entry Right Outer (all from second, matching
from first).

The name of the join also describes the result: ALL data records from the week51 query
are displayed, since this query is the right one (the lower one in the window). In addition,
all data records from the week50 query are displayed whose contents of the No column
are also in the week50 query.

20
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

Figure 0-8: Right Outer join

You can see the result in the Merge window. For the twenty records from the week51
query, there are 15 matching records in the week50 query.

• Confirm with OK.


• Click on the button in the column Week, remove only the check from the field
Use original column name as prefix and confirm with OK.
• Give the query the name Right Outer.
• Now transfer the data to Excel with Home / Close & Load.

21
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

Figure 0-9: The results of the right outer join query

• Give the worksheet the name Right Outer.

As a result, you see all the data for the query week51. You see the matching records for
the query week50. This means that for five records for the query week51, there is no
counterpart in the query week50.

Info: You could also have selected week51 as the first query and week50 as the second
query in the Merge window. As a join child, you could have selected Left Outer (all from
first, matching from second). Then the records of the week51 would be on the left.

2.3 FULL OUTER


This type of link is a special feature of Power Query. All values from both data sets are
merged. With suitable key pairs, the fields from both tables are output next to each other.
The remaining data from the overlaps is displayed separately.

Figure 0-10: Full outer join shows all rows

22
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

• Switch back to the query editor and select the week50 query again.
• Perform all steps as described above.
• In the Join Child field, make the entry Full Outer (all rows from both).

The name of the join already describes the result: ALL data records from both queries are
displayed.

Figure 0-11: Full outer join

You can already see the result in the Merge window. For 15 of the 20 data records from
the week50 query, suitable counterparts are found in the week51 query.

• Confirm with OK.


• Click on the button in the column week51, remove only the tick from the field
Use original column name as prefix and confirm with OK.
• Give the query the name Full Outer.
• Now transfer the data to Excel with Home / Close & Load.

23
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

Figure 0-12: The data from both queries

• Rename the worksheet Full Outer.

As a result, you see all data records from both tables. Lines 2 to 16 have matching numbers
between the two queries. Lines 17 to 21 show the overhang from Week51 and the remaining
lines show the overhang from the Week50 query.

Kommunikationsfähigkeit
Finden Sie heraus, wie Sie ein besserer Kommunikator werden

Download eBooks

24
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

2.4 INNER JOIN


In this type of link between two tables, the intersection of both sets of data is formed by a
lead column. In the output, you only see the data records for which the key fields in both
tables match.

Figure 0-13: Inner join - intersection of both queries

• Switch back to the query editor and select the week50 query again.
• Perform all steps as described above.
• In the Join Child field, choose the entry Inner (only matching rows).

The name of the join already describes the result: ALL data records from both queries are
displayed with the same entries in the No field.

Figure 0-14: Only matching rows with the join type Inner

25
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

• Confirm with OK.


• Click in the column week51 on the button , deselect the field Use original
column name as prefix and confirm with OK.
• Rename the query name to Inner and transfer the data to Excel with Home /
Close & Load.

Figure 0-15: Matching rows from both tables

• Give the worksheet the name Inner.

As a result, you only see the data records that have matching values in the No field.

2.5 LEFT ANTI


With this type of link, only the data that exists exclusively in the left-hand data set is output.

Figure 0-16: The Left-Anti-Join

• Switch back to the query editor and select the week50 query again.
• Perform all steps as described above.
• In the Join Child field, select Left Anti (rows only in first).

26
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

Only the data records from the left (top) query week50 that are NOT in the query week51
are displayed.

Figure 0-17: Join Kind: Left Anti

• Click on OK.

This is the desired result. Only the records that are NOT in the week51 query.

Figure 0-18: Only five records are not in week51

27
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

• For completeness, click on the button in the week51 column, remove only the
tick from the Use original column name as prefix field and confirm with OK.
• Rename the query name to Left Anti.
• Now transfer the data to Excel with Home / Close & Load.

Figure 0-19: The result in Excel

Only the records without a matching counterpart in the week51 query. Therefore, the
columns H to N are empty.

2.6 RIGHT ANTI-JOIN


The right anti-join, like the right outer join, depends only on the direction from which
you are looking at the data. With this type of join, you start from the right (lower) table
and take only the records that only exist on the right side. Matching data records and the
overhang of the left table are not included in the result.

Figure 0-20: Right Anti-Join

• Switch back to the query editor and select the week50 query again.
• Perform all steps as described above.
• In the Join Child field, make the entry Right Anti (rows only in second).

28
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

The name of the join already describes the result: Only the data records from the right
(lowest) query week51 are displayed that are NOT in the query week50.

Figure 0-21: Settings in the Right Anti-Join dialog

• Confirm with OK.

This is the desired result. You will now see the fields from the Week50 query. Do not be
surprised that only an empty line is displayed.

Figure 0-22: Die empty result of query week50

• Make sure you click on the button in column week51, remove only the check
from the Use original column name as prefix field and confirm with OK.
• Give the query the name Right Anti.
• Now transfer the data to Excel with Home / Close & Load.

29
EXCEL: POWER QUERY FOR ADVANCED Understanding and using Join types

Figure 0-23: The data from query week51

All records of the week51 query that do not have a matching counterpart in the week50
query.

Info: You could also have selected week51 as the first query and week50 as the second
query in the Merge window. If you were a join child, you could have selected Left Anti.
Then the overhang of week51 would be output immediately.

30
EXCEL: POWER QUERY FOR ADVANCED Unpivot data – convert results back to a list

3 
UNPIVOT DATA – CONVERT
RESULTS BACK TO A LIST
This chapter explains how to convert data in the form of a pivot table or cross table into
a list. The following figure shows the desired result.

Figure 0-1: Our goal: Create a data list from a pivot table.

A data list is to be generated from the table in the left part in the columns A to I, as it is
displayed in the columns L to O. The data list is to be displayed in the columns L to O.

Open the file Adv_Chapter3.xlsx.

3.1 CREATING A LIST FROM A PIVOT RESULT


Imagine you receive a copy of values from a pivot table. Now you want to create a list from
the data in order to perform new evaluations.

• Turn the table into a dynamic table with >&WUO@+ >7@.


• Change the name of the table to, for example Unpivot.

31
EXCEL: POWER QUERY FOR ADVANCED Unpivot data – convert results back to a list

Figure 0-2: The former pivot table

• Activate the Data tab and start the From Table/Range command.

Figure 0-3: The table in Power Query

First, the empty cells of the Year column must be filled. Power Query provides a command
that writes the value of the cell above into all empty cells.

Info: If you want to populate multiple columns in your table, each column must be edited
individually with the following step.

• Select the Year column, activate the Transform tab, and start the command
sequence Fill / Down.

32
EXCEL: POWER QUERY FOR ADVANCED Unpivot data – convert results back to a list

Figure 0-4: All empty cells are to be filled in this column

In the column Year, all cells with null are filled with the value of the cell above.

Info: The Fill button-list also contains the Up command. Then the empty cells are filled
with the value that is in the cell below.

• Remove the Sum column. Right-click on the column header and start the
Remove command.
• In the Year column, filter out the Total row.

Figure 0-5: Hide the row with the overall result (Total)

• Select the columns from Apas-1 to Ugga-4. These are the columns with the values.

33
EXCEL: POWER QUERY FOR ADVANCED Unpivot data – convert results back to a list

Figure 0-6: Unpivoting data

• Select the command sequence Transform / Unpivot Columns. .

Figure 0-7: The Result: The Data List

Info: In the last step, you could also have selected the two columns Year and Month and
then obtained the same result with the command sequence Transform / Unpivot Columns
/ Unpivot Other Columns. The advantage of this procedure is that fewer columns have
to be selected.

Now you are done with your work in Power Query.

3.2 TRANSFERRING THE DATA TO EXCEL


Now all you have to do is move the data to Excel.

• Select Home / Close & Load.

34
EXCEL: POWER QUERY FOR ADVANCED Unpivot data – convert results back to a list

Figure 0-8: The data list in Excel

A data list has now been created from the pivot table.

3.2.1 CREATE PARTIAL RESULTS

In this example, the data is to be processed further with the Subtotal command. There is
a small problem here. As long as this list is a dynamic table, the Subtotal command is not
available on the Data tab.

Figure 0-9: The Subtotal command is currently inactive.

To work with this command, you must turn the dynamic table into a normal table.

• Select the command sequence Design / Convert to Range.

35
EXCEL: POWER QUERY FOR ADVANCED Unpivot data – convert results back to a list

Figure 0-10: Convert the dynamic table to a normal table

• Confirm with OK.

Now the query was automatically converted into a connection.

Figure 0-11: The query remains valid

Before using the Subtotal command, sort the data by the column you want to group.

• Right-click a cell in the C column and select the command sequence.


• Select the Sort / Sort A to Z command sequence.

Info: You could also have performed the sorting in Power Query.

• Leave the cursor in the list and choose the command sequence Data / Subtotal.
• In the At each changes in field, choose Attribute.
• Leave the Sum function in the Use function field.
• Leave the Value checkbox in the Add subtotal to field.

36
EXCEL: POWER QUERY FOR ADVANCED Unpivot data – convert results back to a list

Figure 0-12: The setting for the sub-totals

• Confirm with OK.

The data is grouped according to the Attribute column and the contents of the Value
column are addes.

Figure 0-13: The subtotal-buttons

By clicking on the icons you can show and hide rows. If you click on them, for
example, you will only see the partial results and the total.

37
EXCEL: POWER QUERY FOR ADVANCED Unpivot data – convert results back to a list

Figure 0-14: Show only totals

Info: If you want to see the result elsewhere, select the cells. Select the command sequence
Home / Find & Select / Go to Special, activate the option Visible cells only and confirm
with OK. Now copy the cells and paste them into another place.

38
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

4 MANY USEFUL TOOLS


IN POWER QUERY
Power Query offers you many more useful commands for data transformation. In this
chapter we will show you how to recognize and remove duplicates.

In Power Query, you can output not only results in list form, but also cross tables.

A highlight is the Distinct Count command, which counts unique rows. You can use this
function to determine how many customers buy from you per month. It does not matter
whether a customer orders more than once, he is counted only once.

The following CSV file is used for the next examples.

Figure 0-1: The CSV file with the data

4.1 RECOGNIZING DUPLICATES


Duplicate values can occur again and again. Someone makes an error when exporting the
data and you receive twice as many data records as expected. This may not be noticeable
in the beginning in the case of work stress. Power Query offers you a very simple solution
to remove duplicates.

• Take a new empty workbook.


• Choose the command sequence Data / From Text/CSV. Activate the file Adv_
Chapter4_Double.csv.
• In the Data Type Detection field, activate the entry Based on entire dataset.

39
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

The CSV file contains over 4,200 data rows. If you selected Based on entire dataset, Power
Query checks all cell contents per column for data type detection.

Figure 0-2: Select the CSV file

• Click on the Load button as you assume that everything is fine with the data.

This will transfer all the data to Excel.

Figure 0-3: The data in Excel

Now you want to create a pivot table from this data that is grouped by years.

• Activate the Design tab and start the Summarize with PivotTable command.
Excel already offers you the correct cell range in the Create PivotTable window.
Confirm with OK.
• Drag the Gross_profit field to the Values area. Design the result in $-format.
• Drag the Year field to the Rows area.

40
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

Figure 0-4: The pivot table from the currently imported data

Now you can see that there is something wrong with the sales data. Sales in 2018 are
significantly higher than expected. You can check directly in Excel whether duplicates appear
in the data list.

4.2 RECOGNIZE DUPLICATE VALUES IN EXCEL


The numbers in column A should actually be unique. With the help of conditional formatting,
you can quickly see in Excel whether duplicate values exist and, if so, in which row.

• Switch back to the data list and select all the cells in the first column.
• Select the command sequence Home / Conditional Formatting / Highlight
Cells Rules / Duplicate Values.
• Set the desired color in the second list box.

Figure 0-5: Highlight double values

41
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

• Confirm with OK.

Now all duplicates are highlighted in color. If you only want to see the duplicate values,
use the filter.

• Click on the filter in the Number column and point to the Filter by Color entry.
• Select either the Fill or the Font color.

Figure 0-6: Display only the duplicates with the filter

• Deactivate the filter again.


Info: You could now delete the duplicate records using the Data / Remove Duplicates
command sequence. In this example, you want to do this work in Power Query because
you suspect that you will have to perform these steps more frequently in the future.

4.3 DELETING DUPLICATE VALUES IN POWER QUERY


If you know that you receive this data more often and you have to expect duplicates again
and again, carry out the following steps in Power Query.

• Select the command sequence Query / Edit.

42
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

Now you can directly start the command sequence Home / Remove Rows / Remove
Duplicates. However, in Power Query you first want to see which data is duplicated.

• Give your query the name Remove Duplicates.

4.3.1 GROUP THE DATA

In Power Query, you can display which document numbers occur twice.

• Start the command sequence Home / Group by.


• Set the Advanced option.
• In the first field Group by, choose the entry Number.

In this example, the year is also to be shown.

• Click on the Add grouping button and select the Year field.
• In the lower part of the window, you decide which calculation step is to be used
to summarize the data.
• In the Operation field, select Count Rows.

Figure 0-7: Group the data

43
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

Info: In this example, you already know that each row from the year 2018 occurs twice. If
you are not sure, you can use the Add grouping button to add all fields to the table. Then
all values in the record are checked.

Note the scroll bar in the Group by window. For many fields, the display often does not
scroll down.

• Confirm with OK.

All unique values are given a 1, the rest a 2. If you only want to see the rows with the 2,
use the filter.

• Note that Power Query may not display all data at the beginning.

Figure 0-8: The hint that not all values are visible in the filter

• Click on the Load more link.

44
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

Figure 0-9: Only the document numbers that are contained more than once.

The last two steps are not necessary for this example. They were used to show you how to
display duplicate values in Power Query.

• Delete the last two steps Filtered Rows and Grouped Rows by clicking
on again.
• Now select the Number column.
• Select the command sequence Home / Remove Rows / Remove Duplicates.

Figure 0-10: Remove duplicate values

45
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

Power Query has now removed all rows from the list that contain duplicate values in the
Number column.

• Click the Close & Load button on the Home tab.


• Switch to the worksheet with the pivot table you just created, right-click the
pivot table and choose Refresh.

Figure 0-11: The corrected data in the pivot table

• Give the worksheet the name Remove Duplicates.

If there are duplicate rows in your CSV file the next time, you can use this query to ensure
that only correct data is evaluated.

4.4 CREATE CROSS TABLE


A cross table is a simple table. The term table is used ambiguously in Excel. First of all,
everything in Excel is a table. However, you can distinguish between lists and tables. A list
has only column headers and below them the data records row by row. A cross table consists
of rows and columns. Each cell is uniquely assigned to a row header and a column header.

46
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

• Perform the steps in the current workbook.


• Choose the command sequence Data / From Text/CSV. Activate the file Adv_
Chapter4_Crosstab.csv.
• In the Data Type Detection field, activate the entry Based on entire dataset.
• Click the Edit button.

Power Query has already recognized the first row as the heading and assigned a data type
to all number columns.

• Give the query the name Crosstab.

Figure 0-12: The new query with which a cross table is created

• Select the command sequence Home / Group by.


• Activate the Advanced option.
• In the Group by field, set the Material entry.
• Click on the Add grouping button and select the Year entry here.
• Give the new column the name Total. In the Operation field, select the Total
entry and select the Gross_profit column in the field next to it.

Figure 0-13: summarize data

47
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

• Confirm with OK.

Figure 0-14: The interim result shows sales per material and year.

In the next step, bring the years into the column headers.

• Select the Year column and start the command sequence


Transform / Pivot Column.
• In the Value Column field, choose Total.
• Click on the Advanced Options entry and choose the entry Don´t Aggregate
for the Aggregate Value Function field because the total was already formed in
the previous step.

Figure 0-15: The data pivoting

• Confirm with OK.

48
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

Figure 0-16: The cross table

Now you have created a cross table. You can see the turnover per material and year.

Info: You could also create this evaluation in Excel with a pivot table. The advantage of
Power Query is the small file size of the workbook.

• In the last step, select the command sequence Home / Close & Load.
• Give the worksheet the name Crosstab.

Figure 0-17: The Result: A Cross Table

4.5 DISTINCT COUNT - COUNT UNIQUE VALUES PER MONTH


You want to know how many customers order goods from you each month.

• Perform the steps in the current workbook.


• Choose the command sequence Data / From Text/CSV. Activate the file Adv_
Chapter4_Distinct.csv.
• In the Data Type Detection field, activate the entry Based on entire dataset.
• Click the Edit button.

Power Query has already recognized the first row as a heading and assigned a data type to
all number columns.

49
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

• Give this query the name Distinct Values.

You only need three columns for the next steps. Therefore, remove the remaining columns.

• Select the columns Year, Month and Cust_No while holding down [Ctrl].
• Right-click the marker and select Remove Other Columns.,

Info: Of course, you could also have selected the five columns you want to delete and started
the Remove command. But marking so many columns is sometimes a bit difficult. Note
in this context that if you delete multiple columns in succession, Power Query combines
these commands in one step.

Figure 0-18: These three columns are required

Info: If you want to know how many customers you have in this list, select the column
Cust_No. Select the command sequence Transform / Statistics / Count Distinct Values.
The result is 70, which means that you are dealing with 70 customers. Delete this step
from the list of applied steps.

Now you want to know how many customers have bought from us in January 2016, how
many in February 2016, etc.

• Select the command sequence Home / Group by.


• Select the Advanced option.
• In the Group by field, set the Year entry.
• Click on the Add grouping button and select the Month entry here.
• In the Operation field, choose Count Distinct Rows .

50
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

Figure 0-19: The grouping levels for the discrete number

• Confirm with OK.

Figure 0-20: The result currently still in the form of a list

Now you can see that 52 of your customers bought from us in January 2016. The representation
is currently still available in list form. If you want to turn it into a cross table again, you
only have to pivote the list.

• Select the Month column.


• Select the command sequence Transform / Pivot Column.
• In the Values Column field, choose Count.
• Click on the Advances options entry and check whether the Aggregate Value
Function field contains the Sum entry.

51
EXCEL: POWER QUERY FOR ADVANCED Many useful tools in Power Query

Figure 0-21: The column Month pivoting

• Click on OK.

Figure 0-22: Customers are uniquely counted

You can see that an average of 50 of your 70 customers order from you each month. Even
if a customer has placed several orders in one month, he will only be counted once.

• In the last step, select the command sequence Home / Close & Load.
• Give the worksheet the name Distinct Count.

Figure 0-23: The result in Excel

52
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

5 CALCULATIONS AND MORE


More than 300 functions for calculation and evaluation are available in Excel. The best
known are the text functions. The If function is often used for control purposes. There are
also several 100 functions in Power Query.

This chapter shows examples of text and date functions and how to use the If function.

• Take a new empty folder.


• Choose the command sequence Data / Get & Transform Data / Get Data/ From
File / From Workbook. Select the file Adv_Chapter5_Functions.xlsx.
• Select the Text Function table.

Figure 0-1: The data of the table Text functions

• Click the Edit button.

Figure 0-2: The data in Power Query

Info: If you have created formulas, you should not change the column headers in the original
file. Power Query does not automatically change formulas other than in Excel.

53
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

5.1 THE TEXT START FUNCTION


In the first step you want to separate the number and the material name in the column
Material. In Chapter 3 we have introduced the Split Colunm / By Delimiter command.
However, you cannot use this command now because there are several different delimiters
in the Material column.

To separate the contents in one step, you must use a text function.

• Select the command sequence Add Column / Custom Column.

In the Custom Column window, enter the required formula. To get help, you must go to
the Internet.

Figure 0-3: Start help over the Internet

• Click on the Learn about Power Query formulas link.

A Microsoft Web page appears.

• Find the link Power Query formula categories and click on it.
• Click on the Text functions entry to see all text functions directly.

54
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Figure 0-4: The list of all text functions

The function to extract a certain number of characters is called Text.Start.

• In the Extraction group, click the Text.Start link.


• Note the syntax of the function. Since the syntax of many functions does
not resemble the structure of the corresponding Excel functions, we use the
following trick: Select the syntax up to and including the closing parenthesis and
copy the text.

Figure 0-5: Information about the Text.Start function

55
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Info: Copying the syntax and pasting it in the Custom Column window saves you a lot
of work.

• Switch back to Power Query.


• Click in the Custom Column formula field and paste the copied text after the
equals sign.

Figure 0-6: The syntax of the function is available

• Select the first parameter string as nullable text and double-click the Material
entry in the Available columns field on the right.
• Remove the parameter count as number and enter 6 there.
• In the New column name field, enter the name Material_no.

Figure 0-7: The function is finished

• Click the OK button.

56
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Figure 0-8: The new column with the material number

The first six characters of the Material column are displayed in the new Material_no column.

5.2 THE FUNCTION TEXT.RANGE


In the next example, the material name in the Material column are to be read out. To do
this, use the Text.Range function.

The syntax of the function Text.Range::

=Text.Range(text as nullable text, offset as number, optional


count as nullable number)

In the first parameter text as nullable text, specify the column whose contents you want
to read. In the second parameter offset as number, specify the start value, i.e. the number
of characters from which you want to start. The third parameter is optional and specifies
how many characters should be taken. If the parameter remains empty, all characters are
taken from the start value.

• Choose the command sequence Add Column / Custom Column.


• Give the column the name Material_Name.
• Click behind the equals sign and enter the following formula:

=Text.Range([Material],9)

57
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Figure 0-9: The Text.Range function

Starting with the ninth character, all characters in the Material column should be transferred
to the new column.

• Click on the OK button.

Figure 0-10: Die Inhalte der Spalte Material auf zwei Spalten verteilt

The contents of the Material column were distributed to two columns using two functions.

5.3 THE FUNCTION TEXT.POSITIONOF


In the next example, the numbers of the material groups are to be extracted at the beginning.
The groups consist of names of different lengths.

58
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Figure 0-11 The contents of the Group_Material column are to be separated.

For example, you can search for the first space character. The Text.PositionOf function
displays the position number of a particular character. In Excel these would be the functions
SEARCH() or FIND().

The syntax is:

Text.PositionOf(string as nullable text, substring as text,


optional occurrence as nullable number, optional comparer
as nullable function)

• Select the command sequence Add Column / Custom Column.


• Give the column the name First space character.
• Click behind the equals sign and enter the following formula:

=Text.PositionOf([#“Material_Group „],“ „)

Figure 0-12: The formula for finding the first space

59
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

• Confirm with OK.

Figure 0-13: The position number of the first space character

You can’t do much with this result alone in this example. However, you can use it in a
different formula.

• Create a new custom column.


• Name it Group and enter the following formula:

=Text.Start([#”Material_Group “],[First space character])

Figure 0-14: The formula for extracting the material group

• Confirm with OK.

60
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Figure 0-15: The Material Group

You have now read the material group. You can use the two functions to read values of
different lengths.

5.4 NESTED FUNCTIONS


You can also execute the last example in one step with only one column. To do this, you
nest the two functions.

5.4.1 THE TEXT.START AND TEXT.POSITIONOF FUNCTIONS TOGETHER

The nesting of functions is performed as in Excel.

• Insert a new custom column.


• Name it Group2 and enter the following formula:

=Text.Start([#”Material_Group “],
Text.PositionOf([#”Material_Group “],” “))

61
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Figure 0-16: A nested function

• Confirm with OK.

The Text.PositionOf function finds the position of the first blank character. This value is
used as the second value in the Text.Start function. The Text.Start function extracts all
characters from the left up to the determined value of the Text.PositionOf function.

Now you have read out the material groups in one step.

Figure 0-17: Reading the Material Groups with a Formula

5.4.2 THE FUNCTIONS TEXT.RANGE AND TEXT.POSITIONOF

In the last step, you want to read the material name. The best way to do this is to search
for the »-« character.

62
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

• Create a new user-defined column.


• Name it Material_Name2 and enter the following formula:

=Text.Range([#”Material_Group “],
Text.PositionOf([#”Material_Group “],”-”)+2)

Figure 0-18: The formula for the material name

The formula searches for the -. The last parameter of the Text.Range function remains empty
because everything is to be displayed from the position number found.

Figure 0-19: The extracted material name

63
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

5.5 CHANGING THE DATA TYPE FOR THE EAN COLUMN


When importing the EAN column, Power Query has assigned the data type Integer to the
column. Since the numbers are very large, they are displayed in exponential notation.

• Click the symbol in the EAN column.


• Select the Text entry.

Figure 0-20: Change the data type

By changing the data type, you can now recognize the EAN numbers.

If you want to transfer the data to Excel now, you can first remove the columns that you
do not need for your work. With the command sequence Start / Close & Load you enter
your transformed data into Excel and evaluate your data.

Info: The formulas are not transferred to Excel. The Workboook only receives a copy of
the values.

5.6 THE IF FUNCTION IN POWER QUERY


In the next example, you want to divide your materials into material groups according to
the sales price.

• Stay in the current Excel folder and choose the command sequence Data / Get
& Transform Data / Get Data/ From File / From Workbook.
• Select the file Adv_Chapter5_Functions.xlsx and select the table IF.

64
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Figure 0-21: The contents of the second spreadsheet

• Click the Edit button.

5.6.1 INSERTING A CONDITIONAL COLUMN

In the first step, you want to automatically divide your materials into material groups. In
this example, all materials whose sales price is less than 3 dollars are to be distributed to
group ALPHA and the rest to group BETA.

• Choose the command sequence Add Column / Conditional Column.


• Give the new column a name and formulate your conditions.

If the Price field contains a value less than 3, the output


should be ALPHA. Otherwise, the output should be BETA.

• Fill in the fields as shown in the figure below.

65
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Figure 0-22: The first conditional column

• Confirm with OK.

Figure 0-23: The result of the conditions

All materials whose selling price is less than 3 euros are automatically included in the
ALPHA group. All other materials end up in the BETA group.

5.6.2 A NESTED IF FUNCTION

In this example, all materials whose sales price is less than 3 euros are to be returned to
group ALPHA. All materials whose sales price is less than 10 euros are to be transferred
to the material group BETA. All other materials are to be returned to the material group
GAMMA.

66
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

• Choose the command sequence Add Column / Conditional Column.


• Give the new column a name, for example Group2, and formulate your first
condition: If the price is less than 3, then ALPHA.
• Click the Add rule button and formulate the second rule: If the price is less
than 10, then BETA.
• Enter the output value GAMMA in the Otherwise field.

Figure 0-24: A Nested What-If Function

• Confirm with OK.

Figure 0-25: The Result of the Nested IF-function

Move the data to Excel and evaluate it further there.

67
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

5.7 EVALUATIONS WITH DATE VALUES


Power Query offers you many functions for evaluating dates. And best of all, you don’t
have to write a formula in this section! There are a lot of functions hidden in the context
menu of the right mouse button.

There are also some functions that are not ready in Excel. For example, you can determine
the quarter directly from a date.

Figure 0-26: The example data for the date functions

For later evaluation, you need information on the date from the Ordered on column.

• Close the folder, take a new empty folder, and choose the command sequence
Data / Get & Transform Data / Get Data/ From File / From Workbook.
• Select the file Date.xlsx and click Edit.

Figure 0-27: The imported table

• Assign the data type Date to the Ordered and Supplied columns.

68
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

5.7.1 READ THE YEAR, THE MONTH AND THE DAY

In this example, you want to read the year, month and day from the »Ordered date« into
individual columns.

• Click with the right mouse button on the column header Ordered and select
the command Duplicate Column.

A copy of the Ordered column is displayed on the far right.

• First change the column header to Year with a right click.


• Right-click on the column header Year again and select the command sequence
Transform / Year / Year.

Figure 0-28: Calling the function to extract the year

In the new column, you only see the year. You can see the function in the editing bar:

Date.Year

Figure 0-29: Year of order

69
EXCEL: POWER QUERY FOR ADVANCED Calculations and more

Perform these steps twice and also extract the month and day from the Ordered column.
Your table now looks like this:

Figure 0-30: The extracted data

With the help of the right mouse button, you can quickly split dates into their individual
parts and convert them.

Info: If you prefer to receive the name of the month, choose Transform / Month / Name
of Month.

Move the data to Excel and evaluate it further there.

70
EXCEL: POWER QUERY FOR ADVANCED Data from the Web

6 DATA FROM THE WEB


This chapter explains how to retrieve information from the Internet. After you specify a
URL to retrieve data, Power Query analyzes the contents of the Web page and displays the
data that can be imported and transformed.

6.1 ANALYZING THE WIKIPEDIA WEBSITE


In this example you want to evaluate the population of New York City. You get the necessary
numbers from Wikipedia.

• Start your browser, open the Wikipedia page and search for the keywords New
York City Population.

Figure 0-1: The data of this website will be analyzed

• Copy the URL.


• Choose the command sequence Data / Get & Transform Data / From Web.
• Paste the contents of the clipboard into the URL field.

71
EXCEL: POWER QUERY FOR ADVANCED Data from the Web

Figure 0-2: The URL-address of the website

• Click OK.

Info: Please note the license and terms of use on the respective websites.

Figure 0-3: Information about the website

• Select the Population table and choose Edit.

You have now dragged the data to Power Query.

72
EXCEL: POWER QUERY FOR ADVANCED Data from the Web

Figure 0-4: The data in Power Query

The first step is to change the contents in the Pop. column. However, you cannot assign a
finished data type, such as Whole Number, as before. You must name the locale Power Query.

• Remove the first column.


• Assign the data type Text to the Year column. Click Replace current.
• Right-click the Pop. column header and choose Change Type / Using locale.

Figure 0-5: Change the data type

• In the Data Type field, choose Whole Number.


• In the Locale field, set the entry to English (United States).

73
EXCEL: POWER QUERY FOR ADVANCED Data from the Web

Figure 0-6: Change data type with local options

Info: You must select the area schema that the column currently contains.

• Click on OK.

Figure 0-7: The Pop. column now contains numbers.

In the next step the contents of the column ±% should be edited. The Replace values
command provides help here.

• Click in the first cell of the column ±%.


• Select Home / Replace values.

By selecting the first cell, the content is already written into the Value to search field.

• Leave the Replace with field empty.

74
EXCEL: POWER QUERY FOR ADVANCED Data from the Web

Figure 0-8: Replacing the line with nothing

• Confirm with OK.

Unfortunately, the negative sign is not recognized as such during import, so you must also
use the Replace Values command here.

• Select a cell that contains a negative sign.


• Choose Home / Replace Values.
• Delete the number in the Value to search field so that only the character remains.
• In the Replace with field, type »-«.

Figure 0-9: Correct the sign

• Confirm with OK.


• Replace empty fields with a 0.
• Right-click the ±% column header and choose Change Type / Using locale.
• In the Data Type field choose Percentage. In the Locale field, set the entry to
English (United States).
• Delete the Header column.

75
EXCEL: POWER QUERY FOR ADVANCED Data from the Web

Figure 0-10: Now you are done

6.2 TRANSFER THE LIST TO EXCEL


Now the data should be moved to Excel and presented in a line chart.

• Select the command sequence Home / Close & Load.

Figure 0-11: The data from Wikipedia in Excel

• Select the cells from A1 to B32 and create a Line with Markers diagram.

76
EXCEL: POWER QUERY FOR ADVANCED Data from the Web

Figure 0-12: A line chart from the imported data

The following changes have been made for the next figure:

• Line Width to 3.5 pt.


• Marker size to 9, fill color to white, Border Width to 2.5 pt.
• Enter a chart title.

Figure 0-13: The Result: The Population of New York

You can update the diagram and the evaluation by updating the Query.

77
EXCEL: POWER QUERY FOR ADVANCED Troubleshooting

7 TROUBLESHOOTING
If Power Query is running smoothly and you do not receive an error message: Congratulations.
In this chapter we have logged all the pitfalls we noticed when creating this book.

7.1 ERROR MESSAGES IN QUERIES

7.1.1 COLUMN HEADER WAS CHANGED

One of the most common error messages in queries is caused by changed column captions.
Someone has thoughtlessly changed a column header in the source file and your query
reports an error.

Figure 0-1: The error message in Excel

• Double-click the query.


• Click the Refresh Preview button.

Figure 0-2: The error information in Power Query

In the best case, you know the new column name.

• Choose Home / Advanced Editor and enter the new column name in all places.
• Click on the Done button.

Your query will then work again.

If you do not know the new column name and do not recognize it, the display in Excel
remains in the last display despite errors.

78
EXCEL: POWER QUERY FOR ADVANCED Troubleshooting

7.1.2 ORIGINAL FILE WAS MOVED OR RENAMED

Your queries also report an error if the source file has been renamed or moved.

• In the Query Editor, select File / Options and Settings / Data source settings.
• Select the desired file and click the Change Source button.
• Enter the new path and/or file name, click OK, and then on Close.
• Click the Refresh Preview button.

Again, Power Query has a good feature: the dynamic Excel table on which the query is
based does not change. You can continue working with the old version.

7.1.3 THE QUERY CANNOT BE UPDATED

There is an option in the settings in the Excel options that you can use to deactivate the
connection of your data to Power Query.

If you want to update your query and a window appears informing you that the connection
is not active, you must perform the following steps:

• Select Excel File / Options / Trust Center.


• Click on the button Trust Center Settings.
• Select the External Content category and change the option in the Security
Settings for Data Connection area.

79
EXCEL: POWER QUERY FOR ADVANCED Index

INDEX
C P
Count Distinct Rows 50 pivot table 13, 15, 31, 32, 35, 40, 41, 46, 49

D R
Distinct Count 39, 49, 52 right anti 28
Right Anti 28, 29
E
Right Anti-Join 28, 29
Error Messages 78 Right Outer 20, 21, 22
F T
file was moved 79 Text.PositionOf 58, 59, 61, 62, 63
Full Outer 22, 23, 24 Text.Range 57, 58, 62, 63
I Troubleshooting 78

Inner Join 25 U

J Unpivot 31, 34, 78

Join V
Right Anti 28
VLOOKUP 6, 7, 9, 11, 17, 20, 78
L
W
Left Anti 26, 27, 28, 30
Wikipedia 71, 76
Left Outer 9, 17, 18, 19, 20, 22

80

You might also like