Excel Power Query Course Notes: Leila Gharani (Microsoft Excel MVP)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43
At a glance
Powered by AI
The key takeaways from the document are that it provides course notes for a Master Power Query online course, covering topics such as connecting to data sources, transforming data, and building dashboards and reports with Power Query and Power BI.

The main topics covered in the course include connecting to and transforming data with Power Query, building data models with Power Pivot, creating reports and visualizations with Power BI, and publishing reports.

Some key Power Query concepts explained include proper data formatting, updating query sources, the differences between transform and add column functions, query folding, and different bracket usages in Power Query.

Excel Power Query

Course Notes
Leila Gharani (Microsoft Excel MVP)

©Note: Copyright 2020, XelPlus.com, Leila Gharani,XelPlus.com


MA
Course Notes for Power Query Online Course
Course Notes for Master Power Query: Beginner to Advanced

These course notes are accompanying documentation for my online course Master
Power Query: Beginner to Advanced Course. Please do not reproduce or transmit in
any form without permission.

We (XelPlus e.U.) have taken every effort to ensure the accuracy of this manual. In case
you discover any discrepancies, please send us a quick email to: [email protected].

About Leila Gharani

Leila Gharani is a Microsoft Excel MVP & a bestselling online course instructor. She runs XelPlus.com an
Excel resource site to help people gain the knowledge they need so they can create useful tools, solve
problems and get more done. She has a YouTube channel under her name with more than 400k subscribers.

Her background is: Masters in Economics, Economist, Consultant, Oracle HFM Accounting Systems Expert &
Project Manager. Find out more here.
Course Roadmap
Power Pivot
ESSENTIALS & Data Model
Project:
HR Report
13
Important Tips 11
& Tricks
Helpful 12
Transformations
4 Online Data
Merge Options
Date & Time Sources
10 Explained
5 Transformations
3 9 Combine &
7 Append Data
Power of Power
Query 6 8
Powerful Custom Column
Transformations & Basic M
ADVANCED

Interface
Advanced
Project: Business
Dashboard
16 Flexible Data
Project: Power BI
List and Table Import
17 Functions Dashboard

15 21
19 22
Complex Solutions
with Merge 18 20
Understanding Custom
M Formulas Functions
What is a Proper Data Set?
A proper data set is a table of data
In the top row is has unique column headers (field names) followed by values (records)
There are no gaps or empty columns in between
Each column has a specific data type (e.g. numbers, text, date)
There are no subtotals in rows

Not Proper Proper


Quantity Price

2019 Product A 20 10.50 Year Product Quantity Price


2019 Product A 20 10.50
Product B 30 15.00
2019 Product B 30 15.00
Total 50
2020 Product A 40 11.50
2020 Product A 40 11.50
2020 Product B 20 16.00
Product B 20 16.00
Total 60
PQ
Editor
Updating the Source Location / Name
If the connection to the source data can’t be resolved, you will get an error message
To fix this you need to:

1. Open the query in Power Query


2. Update the file location/name reference by clicking the gear icon next to the Source
step and edit the location/name

The advantage to the gear icon is that you are presented with a user-friendly way
to browse to the new location, thus avoiding typographic errors.
Transform Versus Add Column
The tabs labeled Transform and Add Column have many of the same features. This can
be confusing for beginners as to which is the correct feature version to use.

Transform will replace the original data with the transformed version of the data.
Add Column will create a new column to hold the results of the transformation.
Data Types Description
Decimal Number Max 15 digits
Currency 4 digits to the right of decimal
Whole Number Integer value (no decimals)
Percentage Shown as decimal type when loaded
to the workbook.
Date/Time Date & Time in one column (PQ
stores this as decimal number type)

Data Date Dates from 1900 to 9999 are


supported

Types
Time Time only (stored as decimals)
Date/Time/Timezone UTC Date/Time
Duration Length of time shown as days, hours,
minutes & seconds (stored as
decimals)
Text Text, can be numbers as well
True/False Boolean value
Binary Sequence of bytes (e.g. when loading
from a folder)
Using Locale… Important if you’re importing data
from sources that have different
regional settings
Correcting Date Formats from Other Regions
Date and number errors could be due to different regional settings of the source file
and your Power Query settings.

To account for this:

1. Select the Foreign Date column.


2. Select the button next to the “Foreign Date” heading and select “Using Locale…”
3. In the Change Type with Locale dialog box, set the Data Type to “Date” and the
Locale to choice that best represents the country the data originated, such as
Germany (Germany), and click OK.

Alternative: Update PQ Options


Go to Data > Get Data > Query
Options. Under Current
Workbook, update the regional
settings to match the one from
the imported file.
Duplicate or Reference a Query?
Duplicate creates a second copy of your existing query independent to the existing query.
Reference creates a new query that’s dependent on the existing query.

Reference when… Duplicate when…

You’d like the starting point of You’d like to create a separate


your new query to be the view of the existing query.
ending step of your existing
query
Error Handling
In case your query loads but with errors:

Check the Power Query Error Report or use a transformation to check for errors:
Home > Keep Rows > Keep Errors (Analyze and then remove the step)

To remove the errors:


Correct errors in source file
Remove error lines in Power Query Editor: Home > Remove Rows > Remove Errors
Replace errors with another value: Transform > Replace > Replace Errors
Manage Queries
Copy & Paste Queries
You can copy a query from the Queries & Connections box (right-mouse click and copy). Open a new
Workbook, go to Data > Queries & Connections, right-mouse click on the pane and select paste.

Group Queries
Select the queries you want to group together from the Queries & Connections box by holding down the
Control key and clicking on the query, right-mouse click and select New Group. Give the group a name. The
remaining queries will be automatically grouped in an “Other Queries” Folder.
To ungroup the queries, you can right-mouse click and select ungroup.

Backing up Your Query Results


After loading the data as a table, right-mouse click and delete the query. The results table can no longer be
refreshed. To remove all queries at once, save the file under a different name first, then go to File > Info >
Check for Issues > Inspect Document > Custom XML Data > Remove All. This will delete all the queries
and connections in the current file.
Power Query Shortcuts
Shortcut Task

F2 Edit the name of a column or query

Arrow keys (L & R) Navigate left or right through columns

CTRL key Select multiple, non-contiguous columns

Shift key Select contiguous columns

CTRL-A Select ALL columns

CTRL-Space Select the entire column of a selected cell

ALT (while opening Open a second, unrelated instance of


Excel) Excel
Common Time Saving Features
Selecting / Deselecting Columns
To select or deselect multiple columns, a selectable list of columns can
be displayed by clicking Home (tab) → Manage Columns (group) →
Choose Columns.

Detecting Data Types


It’s not uncommon to delete the automatically applied type detection step from the Applied
Steps list in Power Query.

When it comes time to perform data type detection, you can select the column(s) you want to
data type and click Transform (tab) → Any Column (group) → Detect Data Type.
PQ Best Practice
1. Source data is in the structure you need.
2. Name the Query (avoid spaces).
3. Use column profiling on the entire data to define data types and check for errors.
4. Update step names and add descriptions to steps (good for documentation).
5. Activate the formula bar and keep your eye on hard-coded field names.
6. Test different versions of the solution by duplicating the query.
7. Take backup of results by deleting the Query connection and saving a copy of the file.
8. Ensure you have correct data types before loading to workbook.
9. In case you need to leave the query, but you’d like to save it, load it as “connection
only”. Later you can go back, adjust and load it to another destination.
10. Keep an open communication with the provider of your source data. You need to be
informed in case something changes on the source side.
Text Transformation
In Power Query we get access to useful text transformations such as:
• Split text by delimiter
• By Number of characters,
• By Positions
• By Lowercase to Uppercase & vice versa
• By Digit to Non-Digit & vice versa

This saves us from having to write complex formulas to transform our data.
Number Transformation
You have an extensive set of number transformations in Power Query. Here’s what
you need to watch out for:
Null Result → If you add or multiply two columns with one another and one column
includes null values, the result will be null. If you’d like to treat null values as zeros,
you can either:
1. Replace null with 0 values
2. Add a third dummy column and apply your transformation. The moment you
have three or more columns, Power Query will use an inbuilt function instead of
the mathematical operator. For example, it will use List.Sum instead of + and
List.Product instead of *. The functions will ignore null values.
Useful Time Transformations
Feature Use

Time Only Extract the Time component from the Date/Time values

Local Time Return the Date/Time/Timezone values

Parse Return the Time value parsed from the text

Hour Extract the Hour component from the Date/Time values

Start of Hour Return the start of the hour corresponding to each Time value

End of Hour Return the end of the hour corresponding to each Time value

Minute Extract the Minute component from the Date/Time values

Second Extract the Second component from the Date/Time values

Subtract Contains the duration between the values in the first and second selected columns

Combine Date and Time Merge the selected columns into a new column containing both Date and Time data

Earliest Return the earliest Time value

Latest Return the latest Time value


Useful Date Transformations
Feature Use
Age Return the duration between the current local time and the values in the selected columns

Date Only Extract the Date component from the Date/Time values
Year Extract the Year component from the Date/Time values
Start of Year Return the first day if the year corresponding to each Date/Time value
End of Year Return the last day if the year corresponding to each Date/Time value
Month Extract the Month component from the Date/Time values
Start of Month Return the first day if the month corresponding to each Date/Time value

End of Month Return the last day if the month corresponding to each Date/Time value

Days in Month Return the number of days in the month corresponding to each Date/Time value

Name of Month Return the name of the month corresponding to each Date/Time value

Quarter of Year Return the quarter corresponding to each Date/Time value


Start of Quarter Return the start of the quarter corresponding to each Date/Time value
End of Quarter Return the end of the quarter corresponding to each Date/Time value
Week of Year Return the week of the year corresponding to each Date/Time value
Week of Month Return the week of the month corresponding to each Date/Time value
Start of Week Return the start of the week corresponding to each Date/Time value
End of Week Return the end of the week corresponding to each Date/Time value
Day Extract the Day component from the Date/Time values
Day of Week Return the day of the week corresponding to each Date/Time value
Day of Year Return the day of the year corresponding to each Date/Time value
Start of Day Return the start of the day corresponding to each Date/Time value
End of Day Return the end of the day corresponding to each Date/Time value
Name of Day Return the name of the day corresponding to each Date/Time value
Subtract Days Return the number of days between the values in the first and second selected columns

Combine Date and Time Merge the selected columns into a new column containing both Date and Time data

Earliest Return the earliest Date value


Latest Return the latest Date value
Useful Duration Features
Feature Use
Days Return the days component corresponding to each Duration value
Hours Return the hours component corresponding to each Duration value
Minutes Return the minutes component corresponding to each Duration value
Seconds Return the seconds component corresponding to each Duration value
Total Years Return the total number of years in each Duration value
Total Days Return the total number of days in each Duration value
Total Hours Return the total number of hours in each Duration value
Total Minutes Return the total number of minutes in each Duration value
Total Seconds Return the total number of seconds in each Duration value
Subtract Contains the duration between the values in the first and second
selected columns
Multiple Multiplies each value in the selected columns by a specified value
Divide Divides each value in the selected columns by a specified value
Statistics (Sum, Min, Sum, Min, Max, Median, or Average of all the Durations
Max, Median,
Average)
Unpivot Columns
Create rows from columns
Bring numbers that track a specific Attribute together in one column
UnPivot Data with Multiple Headers
Steps to Unpivot Data with Multiple Column & Row Headers:
1. Identify anchor columns – these are the columns you want to keep
2. Fill down the anchor columns in case there are gaps
3. Merge anchor columns to create one column
4. Transpose the Table
5. Fill down the columns that have gaps
6. Promote first row to header
7. Select the new anchor columns and Unpivot Other Columns
8. Split the attribute column by delimiter
9. Continue with anything else that needs cleaning – for example filtering out total
values
Pivot Column
Create columns from names in rows
Pivot Column allows for data aggregation
Pivot Flat Data into Multiple Columns
Steps to Pivot Flat Data into Multiple Columns:
1. Add an index column.
2. Think of a logic to separate each group of data the belongs to the same row:
a) If you have a consistent number of columns, you can use a transformation on the
index column and use integer divide. The value will be the number of columns.
b) If you have an inconsistent number of columns, you can use a conditional column
and extract the index number based on the first column (this assumes the first column
is always present). Then Fill Down and remove the original index column.
3. Pivot the column that has the column headers. Select Don’t Aggregate from
Advanced Options.
4. Remove the column you created in step 2.
Merge Queries
The columns
Merging Queries is like VLOOKUP in Excel matched should be
of the same type
Overview of Merge Options
Advantage of merging in PQ:
• Can merge data from external sources
• Is faster on large data sets
• No need to write formulas

Aside from the Default we get access to the below merging options:
Merge Join Options

Instead of
Left & Right
we have
First & Second
Join Kinds Explained
Left Outer Right Outer
(All from first, matching Full Outer
(All from second,
from second) (All rows from both)
matching from first)

Left Anti Right Anti


(Rows only in first) Inner
(Rows only in second)
(Only matching rows)
Examples of Join Kinds

(Left Outer – all from 1st matching from 2nd) (Right Outer – all from 2nd matching from 1st)
Everyone who registered matched to Everyone who attended matched to
Registered Attended attended registered

Sara Norma Sara Norma Norma


Norma Peter Norma Norma Peter Peter
Carl Tom Carl
Tom
Peter Peter Peter

(Left Anti – rows only in 1st) (Right Anti – rows only in 2nd) (Inner Join – only matching rows) (Full Outer – all rows from both)
Registered but did not Attended but did not Registered AND attended? Registered OR attended
attend register

Tom Norma Sara Tom


Sara Norma
Peter
Peter
Carl Carl
Appending Data
Brackets ( )
Square brackets [ ]
When Appending files remember:
Curley brackets { }

1. All column headers should be identical in both spelling and letter case.
2. The order of the columns does not matter – it’s the column headers that are
matched.
3. It is good practice to apply a change type step in the appended version and
remove the change type step from the individual queries (unless you need
the right type to apply transformations before appending).
Appending a Few Files
Brackets ( )
To append a Few Files: Square brackets [ ]
Curley brackets { }
1. From Data > Get Data > From Workbook (or any other source as needed)
2. Apply individual transformations for each file as needed
3. For context (to know which row of data is from which query) add a custom
column with information as required.
4. Load each query to the workbook by only creating a connection to the files.
5. From Excel, right-mouse click on any query and select Append.
6. Select the first and then the second query. If you have more, select “Three
or more Tables”.
7. Give the new appended query a name and load to the workbook either as
table or as Pivot Table.
Appending All Sheets from a File
Brackets ( )
To append All Sheets from a Workbook: Square brackets [ ]
Curley brackets { }
To append all sheets from an external workbook and create a consolidated table or Pivot Table,
you need to follow these steps:
1. From Data > Get Data > From File > From Workbook
2. Select the file and import. To import individual sheets, you can enable multi select and
place a check mark beside the sheets you’d like to import. To import all sheets, select the
folder icon and then Transform.
3. Keep the column metadata you need. Common selections are Name & Data columns.
4. When appending sheets, we no longer get the “combine” button and a sample query that’s
automatically created. Instead we get the “expand” button which automatically appends the
content of each sheet below one another. This means we can do the cleaning up after
appending. [Once we learn about Power Query functions in the advanced section, we’ll also
learn how to create our own custom function that transforms and prepares the data before
appending.]
5. Transform the data by filtering out null values and the headers.

Remember: Double-check your steps to ensure you don’t have any hard-coded values in the
column headers. If yes, try and apply other steps to see if there are ways around this.
Appending All Sheets from a File
Brackets ( )
To append All Sheets from the Current Workbook: Square brackets [ ]
Curley brackets { }
Follow these steps To append all sheets in the current workbook and create a consolidated
table or Pivot table:
1. Use tables to collect the data in each sheet.
2. Instead of creating a connection to each table individually, you can set it all up at once. Go
to Data > Get Data > From Other Sources > Blank Query.
3. In the formula bar type in = Excel.CurrentWorkbook()
4. You see all the objects (tables and named ranges) inside the workbook.
5. Add a filter to exclude or include only specific table names
6. Expand the content column and apply any transformations needed
7. Load as a table or Pivot Table.

Note: If your end result is a table, make sure you exclude the final query from your source list,
otherwise the data will be included in the append process every time your refresh.
Appending Files from a Folder
Append All Files in Folder:
From Data > Get Data > From File > From Folder
Power Query creates two queries. One is a “sample query” that is applied to each file and the
other is final appended result.

When transforming the query, think about which transformations can apply to each single
file before appending and apply these directly to the sample query.

Remember - When appending from a folder, make sure:


• You only include the files you need.
• The content of sub folders will also be included unless the sub folder name is excluded
by filtering the name from the folder path column.
• The final columns that will be included depend on the sample file (first file in the
folder).
The Excel Data Model
Brackets ( )
When do you need the data model? Square brackets [ ]
Curley brackets { }
Use Excel’s data model when your final report is based on multiple lookup tables.

Do you need to use Power Query before?

You don’t need to use Power Query to load your data to the data model if your data is in Excel
tables and if they don’t need any extra transformation steps before you create the
relationships. You can directly go to the relationships button in the data tab and select your
tables. The moment you create a relationship between tables in your current workbook, they
are automatically added to the data model.
Use Power Query if you need to transform the data before you load to the data model and if
you’re importing data from different sources.

How to create relationships

Relationships between tables or queries are done from Data > Relationships or from Data >
Manage Data Model > Diagram View.
Power Query Merge Vs. Data Model
Brackets ( )
Can you use Power Query Merge instead of Data Model? Square brackets [ ]
Curley brackets { }
Yes.

If you just have two tables that need to be imported, transformed, and then merged, you can
also stick with Power Query for the entire process.

The difference between Power Query Pivot Table and a data model Pivot Table is:

• With Power Query you have all the fields you need in one place.
• With the Data Model you have the fields in multiple tables.
3 Important Power Query Rules
Brackets ( )
1. Power Query is Case Sensitive Square brackets [ ]
Curley brackets { }
2. Power Query is Type Sensitive

3. Power Query is zero-based (Position count starts at zero)

Query’s Result:
A Query’s result is what you see in the last step of the query. This is what the
query returns.

Step Identifiers:
If step names have spaces in the name you get the hash sign and quotation
marks around the name. For example #”Changed Type”. If step names don’t
have a space in the name, you can directly reference the name: ChangedType.
Brackets in Power Query
Brackets ( )
Functions Use Brackets: Square brackets [ ]
Curly brackets { }
Text.Contains("Finance Manager","Manager")

Square Brackets:

Column Selection → Text.Contains([Department],"Manager")


Field Selection (Records) → [Employee Name = "West Kim" ]
Projection Operator → [[Employee Name],[Position]]

Curly Brackets:

Selection / positional operator → #”Changed Type”{2}


Holds list of values (one column & can be of different types) → {"Department", "Position"}
Query Folding
Whenever possible, Power Query translates transformation steps to the native
language of the data source system.

Processing work is sent back to source.

Query folding applies to relational databases, OData feed or exchange.


Query folding does not apply to flat files like csv files or Excel files.

Does Not support query folding


Pivot Columns
Adding Index columns
Complex custom functions
Power BI

Power Query: Get to connect to different data sources

Power Pivot: Get to create relationships and create data models

Interactive Reports: Create state of the art reports and visualizations

Publish: Share the reports with others

You can download Power BI Desktop for free from PowerBi.Microsoft.com


THANK YOU!
Please take a few seconds to
leave a review for the course.
Your support is very much
appreciated.
More Learning
If you’d like to improve your Excel knowledge, check out my
other courses:
Excel Essentials for the Real World
Excel Dynamic Arrays Beginner to Expert
Unlock Excel VBA & Excel Macros
Visually Effective Excel Dashboards
Advanced Excel: Top Excel Tips and Formulas
Excel Charts: Visualization Secrets for Impressive Reports
Ultimate Excel Waterfall Chart Course

I share free content on YouTube every Thursday. My


Channel is under my name.

You’ll find detailed blog posts and articles related to Excel


and PowerPoint on my Website at www.XelPlus.com

You might also like