Вы находитесь на странице: 1из 65

Power Query Recipes

Master Your Data in


Excel & Power BI

© www.powerquery.academy
About Power Query Recipes
These cards are a set of ready-to-use Power Query Recipes to lead you step-by-step through a
variety of data transformations and techniques. These cards are a useful reference where you
can look up the exact steps needed for a particular data transformation pattern. We hope you
find them as handy as we do ourselves!

Using the Recipe Cards


The Table of Contents and the recipe cards are fully linked so you can easily navigate between
them. Clicking on the recipe number or name on the Table of Contents will take you directly to
that recipe. Likewise, if a recipe card references another recipe, clicking that reference will take
you directly to the referenced recipe.

Many of the recipes are also linked to corresponding videos in our Power
Query Academy course and are indicated with the video icon shown here:

If you are enrolled in the full Power Query Academy course (not in the free trial or any of
our other courses), clicking the video icon will take you to that video on the course site. Please
note that you will first need to login to the course site before being able to watch the video.

If you are on the ongoing subscription of these cards, we will let you know when new cards
have been added to the set you so can download your updated copy.

© www.powerquery.academy
Power Query Recipes – Table of Contents
00. Best Practice Architectures
0.100.1902 Files as a Data Source n/a
0.110.1908 Creating a Staging Query Module 2, Video 2
0.120.1908 Modifying Query Load Destinations Module 2, Video 2

10. Splitting Data


10.100.1809 Split Records into Columns Module 3, Video 3
10.105.1809 Split Records into Rows Module 3, Video 4

20. Reshaping Data


20.100.1809 Pivoting Data Module 3, Video 2
20.105.1809 Unpivoting Data Module 3, Video 1
20.106.1906 Keep nulls During Unpivot n/a
20.110.1809 Transposing Data Module 9, Video 3
20.115.1902 Unpivot Subcategorized Data Module 9, Video 3
20.116.1902 Unpivot Subcategories – Big Data Module 9, Video 3
20.120.1809 Pivoting Stacked Data Module 9, Video 2

TOC: 00.001.1908
Look for the No Lo Pro
Pattern Code Code Code
© www.powerquery.academy
Difficulty scale:
Power Query Recipes – Table of Contents
30. Combining Data
30.100.1809 7 Ways to Join Your Data Module 5, Video 2
30.105.1809 Exact Match Module 5, Video 1
30.110.1809 Left Outer Join Module 5, Video 2
30.111.1809 Right Outer Join Module 5, Video 2
30.112.1809 Full Outer Join Module 5, Video 2
30.113.1809 Inner Join Module 5, Video 2
30.114.1809 Left Anti Join Module 5, Video 2
30.115.1809 Right Anti Join Module 5, Video 2
30.116.1810 Full Anti Join Module 5, Video 2
30.120.1809 Cartesian Product (Many to Many Join) Module 5, Video 3
30.125.1809 Approximate Match Module 5, Video 4

35. Appending Data


35.100.1906 Basic Append Module 4, Video 1
35.400.1908 Appending Data Recipe Navigation n/a
35.410.1811 Local/Network Drive “FilesList” Query Module 4, Video 4
35.412.1901 SharePoint/Office 365 Groups “FilesList” n/a
Query
TOC: 00.002.1908
Look for the No Lo Pro
Pattern Code Code Code
© www.powerquery.academy
Difficulty scale:
Power Query Recipes – Table of Contents
35. Appending Data Continued…
35.414.1901 OneDrive for Business “FilesList” Query n/a
35.420.1901 Combine All “Flat” Files in a Folder Module 4, Video 4
35.500.1908 Appending a Named Worksheet or Table from Module 4, Video 5
Multiple Excel Files
35.510.1907 Appending Contents of xth Table or Worksheet n/a
from Multiple Excel Files
35.520.1907 Appending Contents and Name of xth Table n/a

40. Conditional Logic


40.100.1809 Extract to New Column Based on Prior Row Module 11, Video 6
40.105.1809 Extract to New Column Based on Next Row Module 11, Video 6

50. Ranking
50.100.1809 Ordinal Ranking n/a
50.105.1905 Standard Competition Ranking n/a
50.110.1905 Modified Competition Ranking n/a
50.115.1905 Dense Ranking n/a
50.120.1905 Fractional Ranking n/a
50.125.1903 Number Grouped Rows n/a
TOC: 00.003.1908
Look for the No Lo Pro
Pattern Code Code Code
© www.powerquery.academy
Difficulty scale:
Power Query Recipes – Table of Contents
60. Date & Time Techniques
60.100.1809 Dynamic Calendar Boundaries Module 15, Video 2
60.105.1809 Dynamic Calendar: Start Date to End Date Module 15, Video 2
60.110.1809 Dynamic Calendar: Start Date + x Days Module 15, Video 3
60.900.1903 Convert a Unix Timestamp to a Valid Date n/a
60.910.1903 Convert UTC DateTime to Local DateTime n/a
60.920.1903 Switching DateTimes Zones n/a

70. Miscellaneous
70.100.1809 Add a Random Number to all Table Rows n/a
70.110.1908 Difference from Prior Row n/a
70.120.1908 Import the most Recent File in a Folder n/a

TOC: 00.004.1908
Look for the No Lo Pro
Pattern Code Code Code
© www.powerquery.academy
Difficulty scale:
0. Best Practice
Architectures

© www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Architecture Recipes: Options when using Files as a Data Source

Individual Files All Files in a Folder The Benefits


Build your solution once and then
Save the new Save the new Save the new data file re-use your existing business
data file with a data file over under a new name intelligence reports and logic!
different name the original (in the same folder)

* If you are using Power BI, you may


be able to schedule the refresh
Open the Excel Open the
Workbook/ Excel Workbook/
Power BI Report Power BI Report

Edit the Query

Update the Close the


Source path to Power Query Refresh Update the Visuals
the new file Editor the Data*
Recipe: 0.100.1902
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Architecture Recipes: Creating a Staging Query

Power BI Desktop Excel


Step 1: Prepare the Source Data Step 1: Prepare the Source Data
Connect to your Data source Connect to your Data source
Perform any desired transformations Perform any desired transformations
Name your query Name your query
Step 2: Load as a Staging query Step 2: Load as a Staging query
Right click the Query name Go to Home → Close & Load → Close & Load To…
Uncheck Enable Load Choose Only Create Connection
Uncheck “Add this data to the Data Model”

Staging queries do not consume any processor


or RAM until they are called by another query.
They can be very helpful in order to act as a
starting point for other queries!

Recipe: 0.110.1908
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Architecture Recipes: Modifying Query Load Destinations

Excel Power BI Desktop


Step 1: Display the Queries & Connections Pane Go to Home → Edit Queries
Excel 2010/2013 Power Query → Show Pane
Right click the Query name
Excel 2016 Data → Show Queries
Excel 2019/365 Data → Queries & Connections Click Close & Apply
Step 2: Modify the Load Destination Go to Home → Refresh
Right click the desired query → Load To…
Modify the load behavior as desired

Excel Tips!
1) If you accidentally loaded a query to a table, you
can convert it to a Connection Only query by
simply deleting the worksheet!
2) Change your default Load Behaviour by modifying
the Query Options found here:
Excel 2010/2013 Power Query → Options
Excel 2016 Data → New Query → Query Options
Excel 2019/365 Data → Get Data → Query Options

Recipe: 0.120.1908
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
10. Splitting Data

© www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Split Records Into Columns Recipe
Right click the Values column
Choose to Split Column → by Delimiter
Original Choose appropriate Delimiter
Click OK
Caution
It is a good idea to remove the
Where this works well:
auto-generated Changed Type
• Desired output is Pivoted step in case the number of
• Number of elements never changes columns changes on update!

Did you know?


You can use the Unpivot Other Columns command
in order to unpivot the results of this table
Output

Recipe: 10.100.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Split Records Into Rows Recipe


Right click the Values column
Original
Choose to Split Column → by Delimiter
Choose appropriate Delimiter
Click Advanced → Split to Rows → OK

Where this works well:


• Desired output is Unpivoted
• Number of elements may change

Desired Output

Did you know?


You can pivot the Records column to ensure
that you’ll always get all columns if the number
of elements per record may change

Recipe: 10.105.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
20. Reshaping Data

© www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Headers Fact
Pivoting Data Recipe
Select the [Headers] column
Go to Transform → Pivot Column
Change the Values drop-down to the [Fact] column
Click OK

Source Table
Headers

Output Table

Fact

Expanding the Advanced Options will allow


you to change the aggregation function
used from SUM to something different

Recipe: 20.100.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Unpivoting Data Recipe


Pivoted Data Prepare Source table
Remove the Changed Type Step *
Filter out Total and Subtotal rows
Delete any Total and Subtotal columns

Unpivot the Data


Select all columns to keep as is
Right click → Unpivot Other Columns
Rename the [Attribute] column
Rename the [Value] column
Pivoted Data (Transposed) Unpivoted Data
Set data types
* Only required if your source data’s column
names will change

Recipe: 20.105.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Keep nulls During Unpivot Recipe Pivot Data With null Values
Prepare Source table
Filter out Total and Subtotal rows
Delete any Total and Subtotal columns
Select columns with null values to unpivot
Right click any column header → Replace Values
Replace null with 0 Unpivoted With null Values
Unpivot the Data
Select all columns to keep as is
Right click → Unpivot Other Columns
Rename the [Attribute] column
Right click the [Value] column
Replace Values → 0 with null
Rename the [Value] column
Set data types

Be aware that this can significantly increase This recipe uses hard-coded column names. To add new
the number of rows in your data set! columns to the data set, modify the “Replaced Value” step

Recipe: 20.106.1906
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Transposing Data Recipe


Prepare the Source table
Filter out Total and Subtotal rows 1
Delete any Total and Subtotal columns 1
Data View
Transpose the Data
Demote headers if you wish to keep them 2
Go to Transform → Transpose
Promote First Row to Headers
Set data types

1. We recommend removing all totals and


subtotals as they can easily be rebuilt via
a PivotTable or PowerBI Matrix

2. To Demote headers, go to
Home → Use Headers as First Row → Use
First Row as Headers Alternate Data View

Recipe: 20.110.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Source Table
Unpivot Subcategorized Data Recipe
1. Demote the headers (if required)
2. Transpose the data
3. Fill up/down columns where needed
4. Merge original “headers” with a delimiter *
5. Transpose the data (back to original form)
6. For columns where row 1 shows only the delimiter,
replace values with text for column headers
7. Promote row 1 to headers
8. Perform any cleanup that can be done before unpivoting
Output Table
9. Unpivot columns
10.Split the [Attribute] column by the delimiter
11.Perform final cleanup
* Pick a delimiter that doesn’t exist elsewhere in
your data!

Recipe: 20.115.1902
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Unpivot Subcategories – Big Data Recipe
This recipe refers to the steps used in the regular
Unpivot Subcategorized Data recipe (Recipe 20.115.x)

LEGEND:
Connection Only Query
Headers
Query
Query Steps
Load to Table/Model
Reference RawData
Keep top x rows
RawData Recipe steps 2 to 6
Data
Query Query Steps
Source
Connect to Data Output Query Steps
Recipe step 1 Query

Set data types Reference Headers Query


Append Data Query
Recipe steps 7 to end

Data
Query
Query Steps
Reference RawData
Remove top x rows

Recipe: 20.116.1902
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! ©www.powerquery.academy
www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Source Table Pivoting Stacked Data Recipe
Add a columns for transaction and line IDs
Add Column → Index Column → From 0
Select [Index] → Add Column → Standard → Integer-Divide*
Select [Index] → Transform → Standard → Modulo*

Pivot the data


Select [Index] → Transform → Pivot Column
Set the Values field to your data column
Go Advanced and “Don’t Aggregate”

Right click the [Integer-Divide] column → Remove


Rename the newly created columns Output Table

Set your data types

* Modulo and Integer Divide always


target the number representing the
1st row of the 2nd transaction

Recipe: 20.120.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
30. Combining Data

© www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

7 Ways to Join your Data

Inner Join Full Outer Join Full Anti Join


Only records All records Records with
which match from both no match
(no red/yellow) (red + yellow)
Recipe 30.112.x
Recipe 30.113.x Recipe 30.116.x

Left Outer Join Right Outer Join


All records All records
from left from right
(no yellow) (no red)
Recipe 30.110.x Recipe 30.111.x

Left Anti Join Right Anti Join


Mismatches Mismatches
from left from right
(only red) (only yellow)
Recipe 30.114.x Recipe 30.115.x

Recipe: 30.100.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Source Table
Exact Match Recipe
Prepare Source table
Set data types
Load as Connection Only
Key Prepare Lookup table
Column
Set data types
Right click [Key] column → Remove Duplicates
Key Load as Connection Only
Column
Perform the match
Reference the Source table
Go to Home → Merge → Lookup table
Select the [Key] column on both tables
Use a Left Outer Join
Expand the new column
Lookup Table Uncheck the [Key] column and prefix option

Recipe: 30.105.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Left Outer Join:
All records from Left (top) &
Matches from Right (bottom)
Source Table
Left Outer Join Recipe
Pink records have no
match in Lookup so are For each table
included in result Create a staging query (load as Connection Only)
Merge the Data
Reference the Source table
Key Go to Home → Merge → Lookup table
Columns
Select the [Key] column(s) on both tables*
Use a Left Outer Join
Key Expand the Data
Columns
Expand the new column
Yellow records have no Uncheck the [Key] column(s)
match in Source so are
Consider unchecking Preface option
excluded from result
* Use multiple columns for your
[Key] column by holding down
the CTRL key while selecting them

Lookup Table

Recipe: 30.110.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Right Outer Join:
All records from Right (bottom)
& Matches from Left (top)
Source Table
Right Outer Join Recipe
Pink records have no
match in Lookup so are For each table
excluded from result Create a staging query (load as Connection Only)
Merge the Data
Reference the Source table
Key Go to Home → Merge → Lookup table
Columns Select the [Key] column(s) on both tables*
Use a Right Outer Join
Key Expand the Data
Columns
Expand the new column
Yellow records have no Uncheck the [Key] column(s)
match in Source so are
Consider unchecking Preface option
included in result
* Use multiple columns for your
[Key] column by holding down
the CTRL key while selecting them

Lookup Table

Recipe: 30.111.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Full Outer Join:
All records from both tables

Source Table
Full Outer Join Recipe
Pink records Yellow records
For each table
are included in are included in
result result Create a staging query (load as Connection Only)
Merge the Data
Reference the Source table
Key Go to Home → Merge → Lookup table
Columns
Select the [Key] column(s) on both tables*
Use a Full Outer Join
Key Expand the Data
Columns
Expand the new column
Uncheck the [Key] column(s)

[Account] & [Dept] Consider unchecking Preface option


shown here are from
* Use multiple columns for your
the Source table only [Key] column by holding down
the CTRL key while selecting them

Lookup Table

Recipe: 30.112.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Inner Join:
Only records with matches

Source Table
Inner Join Recipe
For each table
Pink records have no match in Create a staging query (load as Connection Only)
Lookup so are excluded from result
Merge the Data
Reference the Source table
Key Go to Home → Merge → Lookup table
Columns Select the [Key] column(s) on both tables*
Use an Inner Join
Key Expand the Data
Columns Expand the new column
Yellow records have no
match in Source so are Uncheck the [Key] column(s)
excluded from result Consider unchecking Preface option

* Use multiple columns for your [Key]


column by holding down the CTRL
key while selecting them

Lookup Table

Recipe: 30.113.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Left Anti Join:
Only records from Left (top)
with no match in Right (bottom)
Source Table
Left Anti Join Recipe
For each table
Pink records have no match in
Lookup so are included in result Create a staging query (load as Connection Only)
Merge the Data
Reference the Source table
Key Go to Home → Merge → Lookup table
Columns Select the [Key] column(s) on both tables*
All records that match
between the two tables Use a Left Anti Join
Key are excluded from result Expand the Data
Columns
Expand the new column
Yellow records have no
Uncheck the [Key] column(s)
match in Source and are
excluded from result Consider unchecking Preface option

* Use multiple columns for your


[Key] column by holding down
the CTRL key while selecting them

Lookup Table

Recipe: 30.114.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Right Anti Join:
Only records from Right (bottom)
with no match in Left (top)
Source Table
Right Anti Join Recipe
For each table
Yellow records have no match in
Source so are included in result Create a staging query (load as Connection Only)
Merge the Data
Reference the Source table
Key Go to Home → Merge → Lookup table
Columns Select the [Key] column(s) on both tables*
All records that match
Use a Right Anti Join
between the two tables
Key are excluded from result Expand the Data
Columns
Remove the [Key] column(s)
Pink records have no Expand the new column
match in Lookup and
Consider unchecking Preface option
are excluded from result
* Use multiple columns for your
[Key] column by holding down
the CTRL key while selecting them

Lookup Table

Recipe: 30.115.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Full Anti Join:
Show only records that do not
Source Table have matches in the other table

Full Anti Join Recipe


Only items where the Key column
doesn’t have a match will be Create Left Anti-Join query* (Recipe 30.116.x)
displayed
Create Right Anti-Join query* (Recipe 30.117.x)
Expand all fields from joined [Key] column†

Key Load as staging queries (Connection Only)


Column This join is not available
in the user interface and Reference Left Anti-Join query
must be created
Go to Home → Append → Right Anti-Join query
Key manually!
Column
Load to your destination

* Ensure names of columns are identical in both queries


† If both tables contain the same field names, rename
the Join column then expand it with the Preface option
checked

Lookup Table

Recipe: 30.116.1810
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Table 1
Cartesian Product Recipe
(Many to Many Joins)
For each table to be merged:
Create a staging query

Create a merge query


Table 2 Reference Table1
Go to Add Column → Custom Column
Name: Leave as “Custom”
Formula:=Table2 The Result
Expand the new column*

* Be sure to uncheck the Preface option

Recipe: 30.120.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Source Table

Approximate Match Recipe


Prepare Source table
Set data types

Prepare Lookup table


Key Match Output Table
Rename [Match] column to match Source table
Set data types

Match Return Perform the match


Reference the Source table
Go to Home → Append → Lookup table
Filter the [Match] column → Sort Ascending
Filter the [Key] column → Sort Ascending Key Match Return
Right click the [Return] column → Fill → Down
Filter the [Key] column → uncheck null

Lookup Table

Recipe: 30.125.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
35. Appending Data

© www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Data Source 1 Data Source 2 Basic Append Recipe
Step 1: Create a “Staging1” query
Create a query that connects to Data Source 1
Make the required transformations
Load as Connection Only
Step 2: Create a “Staging2” query
Staging 1 Staging 2 Create a query that connects to Data Source 2
Make the required transformations
Load as Connection Only
Step 3: Append the Data
Right click the “Staging1” query
Choose Reference
Final Query
Go to Home → Append
Select the “Staging2” query
Perform any additional required transformations
Load to your destination of choice
Staging queries are only executed
when the Final Query is refreshed CAUTION!
and give a very easy way to compare When combining tables with different column headers, all
results before the data is appended! columns will get created. Where no record exists for a one
of the data sources, it will be filled with the null keyword.
Recipe: 35.100.1906
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Master Your Data in Excel & Power BI
Appending Data Recipe Navigation
Create a “FilesList” Local / Network Drive: Recipe 35.410.x
query to read SharePoint / O365: Recipe 35.412.x
from folder OneDrive for Business: Recipe 35.414.x

Create a “Master”
Reference the
query to
FilesList query
append files

Perform transforms that Appending Data Recipes


Modify the
MUST be done BEFORE Flat Files (.txt, .csv, .prn) 35.420.x
Sample Transform
the files are appended Excel files:
Based on named table/sheet 35.500.x
Contents of xth table/sheet 35.510.x
Perform transforms that Contents & name of xth table 35.520.x
Clean up the
can be done AFTER
“Master” query
the files are appended

Benefits of Using a “FilesList” Query


- An easy entry point to review and sort the files list
- The file path gets hard-coded into the solution once (easier to update in future)

Recipe: 35.400.1908
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Get Files Local/Network Drive “FilesList” Query Recipe


From Folder
Step 1: Connect to the Folder
Get Data → From File → From Folder
Browse to the folder you need
Rename Choose to Edit/Transform the Data
“FilesList”
Step 2: Filter and Future-Proof the Files List
Optional: Filter [Folder Path] to the desired subfolder
Right click the Extension column → Transform → lowercase
Filter to Filter the Extension column → Text Filters → Equals
subfolders as
required Type in your desired file type*
Filter the Name column → Does Not Begin With → ~
Perform any additional sorting, filtering or modifications**

Filter to the Step 3: Finalize the Query


specific file type Load the query as Connection Only

Tips and Tricks


Load “FilesList” * If you need more than one type, consider using Contains or Does Not Contain for your filter
as “Connection” ** Can be useful if you need to use a specific file as your sample during the combine operation

Recipe: 35.410.1811
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


SharePoint/Office 365 Groups “FilesList” Query Recipe
Step 1: Connect to the Folder
Tips and Tricks Get Data → From File → From SharePoint Folder
* If you need more than Enter the URL to the root of your site (not a library or folder)†
one file type, consider Chose to Edit/Transform the data
using Contains or Does
Not Contain for your filter Step 2: Filter and Future-Proof the Files List
Filter the [Folder Path] column to the desired library or folder
** This step may be useful Right click the [Extension] column → Transform → lowercase
if you need to use a specific Filter the [Extension] column → Text Filters → Equals
file as your sample during
the combine operation Type in your desired file type*
Filter the [Name] column → Does Not Begin With → ~
Perform any additional sorting, filtering or modifications**
Load the query as Connection Only

† Locating your SharePoint Site URL:


Find the second “/” to the left of the word “Forms” and keep everything to the left:
https://<SharePointDomain>/sites/projects/rockets/Forms/AllItems.aspx
<----------- connect to this ----------->|<---- ignore all this ---->

Get Files From Filter to library


Rename Filter to the Load “FilesList”
Sharepoint or folder as
“FilesList” specific file type as “Connection”
Folder required

Recipe: 35.412.1901
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


OneDrive for Business “FilesList” Query Recipe
Step 1: Connect to the Folder
Tips and Tricks Get Data → From File → From SharePoint Folder
* If you need more than Enter the URL to the root of your OneDrive for Business site†
one file type, consider Chose to Edit/Transform the data
using Contains or Does
Not Contain for your filter
Step 2: Filter and Future-Proof the Files List
Filter the [Folder Path] column to the desired library or folder
** This step may be useful Right click the [Extension] column → Transform → lowercase
if you need to use a specific Filter the [Extension] column → Text Filters → Equals
file as your sample during
Type in your desired file type*
the combine operation
Filter the [Name] column → Does Not Begin With → ~
Perform any additional sorting, filtering or modifications**
Load the query as Connection Only

† Connect to the root of your OneDrive for Business site as follows:

https://<tenant>-my.sharepoint.com/personal/<email>
where the . and @ signs in your email are replaced with the _ character.
(The easiest way to check is to login to OneDrive for Business in the web and inspect the URL.)

Get Files From Filter to library


Rename Filter to the Load “FilesList”
Sharepoint or folder as
“FilesList” specific file type as “Connection”
Folder required

Recipe: 35.414.1901
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


What is a “Flat” File?
It’s a file with no complex metadata such as a *.TXT, *.CSV, *.PRN. Can you
open it in Notepad and see the data? Chances are good that it’s a flat file!

Combine All “Flat” Files in a Folder


Step 1: Create a FilesList query These queries
Create the FilesList query indicated by Recipe 35.400.x are created
automatically
Step 2: Create the Master query
when you
Reference the FilesList Query combine files!
Make sure to rename the query BEFORE combining!
Click the  symbol at the top of the [Content] column
Choose the File for your sample → click OK
This will create 4 new queries!
Step 3: Modify the Transform Sample from Master
Perform modifications required before the files are appended † Tip:

Return to the Master query Did you lose [FileName]


or other needed columns
Step 4: Finalize the Master query upon combining?
Delete the Changed Type step to clear any errors Click the gear next to the
Perform any additional cleanup† “Removed Other
Define the data types for each column Columns1” step to bring
them back!
Load the query to your desired destination

Recipe: 35.420.1901
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Appending contents of a named Worksheet


(or Table) from Multiple Excel Files
Step 1: Create a FilesList query These queries
Create the FilesList query indicated by Recipe 35.400.x are created
Step 2: Create the Master query automatically
when you
Reference the FilesList Query
combine files!
Make sure to rename the query BEFORE combining!
Click the  symbol at the top of the [Content] column
Choose the File for your sample → click OK
Select the Worksheet (or Table) you wish from each workbook
This will create 4 new queries!
Step 3: Modify the Transform Sample from Master
Perform modifications required before the files are appended † Tip:

Return to the Master query Did you lose [FileName]


or other needed columns
Step 4: Finalize the Master query
upon combining?
Delete the Changed Type step to clear any errors Click the gear next to the
Perform any additional cleanup† “Removed Other
Define the data types for each column Columns1” step to bring
them back!
Load the query to your desired destination

Recipe: 35.500.1908
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Appending only contents of xth Worksheet
(or Table) from Multiple Excel Files These queries
are created
Step 1: Create a FilesList query per Recipe 35.400.x automatically
Step 2: Create the Master query when you
Reference the FilesList Query combine files!
Make sure to rename the query BEFORE combining!
Click the  symbol at the top of the [Content] column
Select the Folder icon (not a worksheet) → Click OK
Step 3: Modify the Transform Sample from Master
Filter the [Kind] column to either Sheet or Table
To preserve the names of the
Go to Add Column → Add Index Column → From 1
Tables as well as the data, see
Filter the [Index] column to the item you wish to keep Recipe 35.520.x
Right click the [Data] column → Remove Other columns
Click the Table keyword to drill in
Perform modifications required before the files are appended † Tip:

Return to the Master query Did you lose [FileName]


or other needed columns
Step 4: Finalize the Master query upon combining?
Delete the Changed Type step to clear any errors Click the gear next to the
Perform any additional cleanup † “Removed Other
Define the data types for each column Columns1” step to bring
them back!
Load the query to your desired destination
Recipe: 35.510.1907
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Appending Contents & Name of xth
Table from Multiple Excel Files These queries
are created
Step 1: Create a FilesList Query (see Recipe 35.510.x)
automatically
Step 2: Create a Master Query (see Recipe 35.510.x) when you
Step 3: Modify the Transform Sample from Master combine files!
Rename the [Name] column to Table Name
Filter the [Kind] column to either Sheet or Table
Go to Add Column → Add Index Column → From 1
Filter the [Index] column to the item you wish to keep
Select the [Name] and [Data] columns → Remove Other columns
Expand the required columns from the [Data] column
Perform modifications required before the files are appended
Return to the Master query
Step 4: Finalize the Master query
† Tip:
Delete the Changed Type step to clear any errors
Perform any additional cleanup† Did you lose [FileName]
or other needed columns
Define the data types for each column upon combining?
Load the query to your desired destination Click the gear next to the
“Removed Other
Columns1” step to bring
This recipe works for Excel tables, but not worksheets. them back!
Recipe: 35.520.1907
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
40. Conditional Logic

© www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Extract to New Column Based Target Compare
on Data in Prior Row Recipe
Preparation:
Perform any possible preliminary cleanup
Add [Index] column: Add Columns→Index Column→From 0 Pattern
Add [Index.1] column: Add Columns→Index Column→From 1
Extract Data Into New Column:
Go to Home → Merge Queries As New → Merge against itself
Match [Index] (top) against [Index.1] (bottom)
Expand the [Compare] column only, creating [Compare.1]
Sort by the [Index] column (ascending)
Based on prior
Create a Conditional Column: Extract these
row values
=if [Compare.1]= "pattern" then [Target] else null
Right click the new column → Fill → up/down (as needed)
Filter the [Compare.1] column to remove the pattern And put it into
Remove the [Index], [Index.1] and [Compare.1] columns a new column:

Finish cleanup

Recipe: 40.100.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Extract to New Column Based Target Compare
on Data in Next Row Recipe
Preparation:
Perform any possible preliminary cleanup
Add [Index] column: Add Columns→Index Column→From 0
Add [Index.1] column: Add Columns→Index Column→From 1
Pattern
Extract Data Into New Column:
Go to Home → Merge Queries As New → Merge against itself
Match [Index.1] (top) against [Index] (bottom)
Expand the [Compare] column only, creating [Compare.1]
Sort by the [Index] column (ascending) Based on prior
Extract these
Create a Conditional Column: row values
=if [Compare.1]= "pattern" then [Target] else null
Right click the new column → Fill → up/down (as needed)
Filter the [Compare.1] column to remove the pattern And put it into
Remove the [Index], [Index.1] and [Compare.1] columns a new column:

Finish cleanup

Recipe: 40.105.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
50. Ranking

© www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

An ordinal rank creates a


unique number for each
row and never breaks ties Source Table

Ordinal Ranking Recipe


Initial Preparation
Perform any possible preliminary cleanup
Set data types
Ranked Table
Sort the numeric column → Descending
Sort any other columns to order ties
Add Ranking
Go to Add Column → Index Column → From 1
Rename [Index] to Rank

Learn about ranking methods:


https://en.wikipedia.org/wiki/Ranking

Recipe: 50.100.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Standard Competition
Ranking Recipe
Standard Competition Ranking
Initial Preparation records ties, leaving gaps in the
Perform any possible preliminary cleanup ranking numbers AFTER sets of
Set data types equal-ranking items
Sort the [Fact] column → Descending
Sort any [Dimension] column(s) to order ties
Go to Add Column → Index Column → From 1
Group the Data
Go to Transform → Group → Advanced Source Table Ranked Table
Group by the [Fact] column
Add new column names as follows:
Rank → Min → [Index]
Data → All Rows
Expand the [Dimension] field from the [Data] column
Re-order the columns if desired

Learn about ranking methods: Dimension Fact


https://en.wikipedia.org/wiki/Ranking
Recipe: 50.105.1905
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Modified Competition
Ranking Recipe Modified Competition Ranking
Initial Preparation records ties, leaving gaps in the
ranking numbers BEFORE sets of
Perform any possible preliminary cleanup
equal-ranking items
Set data types
Sort the [Fact] column → Descending
Sort any [Dimension] column(s) to order ties
Go to Add Column → Index Column → From 1
Group the Data Source Table Ranked Table
Go to Transform → Group → Advanced
Group by the [Fact] column
Add new column names as follows:
Rank → Max → [Index]
Data → All Rows
Expand the [Dimension] field from the [Data] column
Re-order the columns if desired

Dimension Fact
Learn about ranking methods:
https://en.wikipedia.org/wiki/Ranking
Recipe: 50.110.1905
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Dense Ranking Recipe Dense Ranking records ties,


Initial Preparation but does not leave gaps in the
Perform any possible preliminary cleanup ranking numbers before or
Set data types after sets of equal-ranking
items
Sort the [Fact] column → Descending
Sort any [Dimension] column(s) to order ties
Group the Data
Go to Transform → Group → Advanced
Group by the [Fact] column
Source Table Ranked Table
Add a new column name as follows:
Data → All Rows
Sort the [Fact] column → Descending
Go to Add Column → Index Column → From 1
Expand the [Dimension] field from the [Data] column
Rename the [Index] column to “Rank”
Re-order the columns if desired

Learn about ranking methods:


https://en.wikipedia.org/wiki/Ranking Dimension Fact

Recipe: 50.115.1905
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Fractional Ranking Recipe
Initial Preparation Fractional Ranking records ties,
Sort the [Fact] column → Descending calculating them as the mean
Sort any [Dimension] column(s) to order ties
of the values that would be
generated using ordinal ranking
Go to Add Column → Index Column → From 1
Group the Data
Go to Transform → Group → Advanced
Group by the [Fact] column with these new columns:
Source Table Ranked Table
Std → Min → [Index]
Mod → Max → [Index]
Data → All Rows
Go to Add Column → Custom Column
Name: Rank
Formula: =([Std]+[Mod])/2
Remove the [Std] and [Mod] columns
Expand the [Dimension] field from the [Data] column
Re-order the columns if desired
Dimension Fact
Learn about ranking methods:
https://en.wikipedia.org/wiki/Ranking
Recipe: 50.120.1905
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Source Table
Number Grouped Rows Recipe
Initial Preparation
Sort any [Dimension] column(s) to group the data
Sort the [Fact] column → Descending
Re-apply the column data types
Group the Data
Go to Transform → Group → Advanced Dimension Fact
Group by the [Dimension] column(s)
Add a new column called “Data” → All Rows
Table With Numbered Rows
Add Row Numbers
Go to Add Column → Custom Column
Name: Custom
Formula: =Table.AddIndexColumn([Data], "Index", 1, 1)
Right click the [Custom] column → Remove Other Columns
Expand all columns from [Custom]
Re-order the columns if desired

DBAs know this output as using SQL’s


ROW_NUMBER with PARTITION
Recipe: 50.125.1903
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
60. Date & Time Techniques

© www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Dynamic Calendar Boundaries Recipe


Determine the StartDate Determine the EndDate
Reference table with earliest date Reference table with latest date
Right click [Date]→ Remove Other Columns Right click [Date]→ Remove Other Columns
Filter [Date] → Date Filters → Is Earliest Filter [Date] → Date Filters → Is Latest
Right click [Date] → Remove duplicates Right click [Date] → Remove duplicates
Change the data type to a Date* Change the data type to a Date*
Transform → Date → Year → Start of Year Transform → Date → Year → End of Year
Right click the date cell→ Drill down † Right click the date cell→ Drill down†
Call query StartDate (no spaces) Call query EndDate (no spaces)
Load as Connection Only Load as Connection Only
StartDate
Continue with your preferred Dynamic Calendar Recipe

* This step is important † Ensure that you click the cell to drill down, not the header
if you are not forcing of the table. You need your new formula to read as follows:
to start/end of period =#"<previous step name>"{0}[Date Column Name]

Recipe: 60.100.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Dynamic Calendar Recipe:
From Start Date to End Date
Determine the Calendar boundaries
Create StartDate query (Recipe 60.100.x) Given only a
Create EndDate query (Recipe 60.100.x) StartDate and
an EndDate…
Create a Blank Query
Enter the following in the formula bar:
={Number.From(StartDate)..Number.From(EndDate)}
Go to List Tools → To Table → OK
Set [Column1] to a Date data type
Rename [Column1] to Date
Rename the table to Calendar
Fully Dynamic ‘Calendar’ Table
Add New Columns
1. Select the [Date] column
2. Go to Add Column → Date → <the format to add>
3. Repeat Steps 1-2 as many times as needed
Load the Calendar to the intended destination

To shorten Month or Day names, go to


Transform → Extract → First Characters
Recipe: 60.105.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Dynamic Calendar Recipe: * Creating a Days query or


Start Date + x Days parameter is optional. If
you choose not to, then just
Determine the Calendar boundaries substitute a number into
the formula in place of Days
Create StartDate query (Recipe 60.100.x)
Create a Days query or parameter (for number of days)*
Create a Blank Query
Given only a
Enter the following in the formula bar: StartDate
=List.Dates(StartDate,Days,#duration(1,0,0,0))
and x Days…
Go to List Tools → To Table → OK
Set [Column1] to a Date data type
Rename [Column1] to Date Fully Dynamic ‘Calendar’ Table
Rename the table to Calendar
Add New Columns
1. Select the [Date] column
2. Go to Add Column → Date → <the format to add>
3. Repeat Steps 1-2 as many times as needed
Load the Calendar to the intended destination
To shorten Month or Day names, go to
Transform → Extract → First Characters

Recipe: 60.110.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

epoch
Convert a Unix Timestamp to a Valid Date Time (UTC)

Initial Preparation
Convert the [epoch] column to a Whole Number data type
Add a Column for UTC Time
Go to Add Column → Custom Column
Name: Time (UTC)
Formula: =#datetime(1970, 1, 1, 0, 0, 0)
+ #duration(0, 0, 0, [epoch])
Set the [Time (UTC)] column to a DateTime data type
Right click the [epoch] column → Remove Column

Unix time (also known as POSIX time or UNIX Epoch time) displays
time as the number of seconds that have elapsed since 00:00:00
Thursday, 1 January 1970, minus leap seconds. All times are
tracked in Coordinated Universal Time (UTC).

Need to convert the UTC


time to another time zone?
Check out Recipe 60.910.x
Recipe: 60.900.1903
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Convert UTC DateTime to Local DateTime Recipe
Step 1: Add a UTC time zone (if your data has time zones, skip to Step 2)
Right click the [Original] column → Change Type → DateTime
Go to Add Column → Custom Column NOTE: This recipe is
Daylight Savings Time
Name: DateTimeLocal
aware! If you do not
Formula:DateTime.AddZone([Original],0) want this volatility in
Right click the [Original] column → Remove Column your transformation,
see Recipe 60.920.x to
Step 2: Convert the [DateTimeLocal] column to your local time zone
lock in a time zone.
Select the [DateTimeLocal] column → Transform → Time → Local Time
Right click the [DateTimeLocal] column → Change Type → <Date or DateTime as desired>

Original Original Converted to Local Zone Local Date

Recipe: 60.910.1903
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI


Convert with 2A
Switching DateTime Zones Recipe Does your data
already show
Step 1: Create an “Offset” parameter
time zones?
Go to Manage Parameters → New Parameter
Name: Offset Convert with 2B
Current Value: <enter number of hours time zones to offset>
Note that offset can be set as negative if required

Step 2A: If your data DOES NOT already have a time zone
Right click the [Original] column → Change Type → DateTime
Go to Add Column → Custom Column
Name: NewZone
Formula: DateTime.AddZone([Original],Offset)
Right click the [Original] column → Remove Column

CAUTION!
Step 2B: If your data ALREADY displays a time zone These DateTimeZone
Go to Add Column → Custom Column functions are NOT
Name: NewZone Daylight Savings Time
Formula: =DateTimeZone.SwitchZone([Original],Offset) aware! To correctly factor
in Daylight Savings Time
see Recipe 60.910.x.

Recipe: 60.920.1903
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
70. Miscellaneous

© www.powerquery.academy
Pattern Difficulty

Master Your Data in Excel & Power BI

Add a Random Number to all Table Rows Source Table

Prepare your data table


Go to Add Column → Add Index column *
Go to Add Column → Add Custom column
Column Name: RandomSeed
Formula: =List.Random(1){0}
Go to Add Column → Add Index column *
Select [Index] and [Index.1] → Home → Remove Columns
Set [RandomSeed] to a Decimal data type

* Yes, this step is required, as it forces Power Query to


calculate and lock in the random seeds. Without it
you’ll get a single random number on all rows!
Add Random Numbers
to each table using
Power Query

Recipe: 70.100.1809
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Master Your Data in Excel & Power BI

Difference From Prior Row Recipe


Take This… Add This:
Initial Preparation
Sort the [Dimension] column to order the data
Go to Add Column → Index Column → From 0
Go to Add Column → Index Column → From 1
Merge the query
Go to Home → Merge Queries
Merge the query to itself
Merge [Index] (top) to [Index.1] (bottom)
Dimension Fact
Expand the Prior Row
Uncheck the Use Preface option
Extract only the [Fact] column Are you using the
Sort the [Index] column → Descending classic compare to
Select the [Fact] and [Fact.1] columns (hold down CTRL) [Index]-1 pattern to
Go to Add Column → Standard → Subtraction accomplish this
Clean up result? This pattern
Remove the [Index], [Index.1] and [Fact.1] columns performs MUCH
Rename the [Subtraction] column as desired faster!

Recipe: 70.110.1908
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Master Your Data in Excel & Power BI

Import the Most Recent File in a Folder Recipe


Skipping Step 2 will Step 1: Create a FilesList query
result in a file path Create the FilesList query indicated by Recipe 35.400.x
that is hard coded Note: The important parts are the steps for connecting to the
and won’t change folder. There is no need to rename this query to FilesList
upon refresh! Step 2: Reduce the data to only the relevant file
Click the filter on the [Date Created]* column → Sort Descending
Go to Home → Keep Rows → Keep Top Rows → 1
Right click the [Content] column → Remove Other Columns
Step 3: Extract the data
Click the Binary keyword in the Content column

* There are three different columns of dates that you may wish to use for this recipe:
[Date accessed] gives the last time someone opened the file
[Date modified] provides the last time the file was changed
[Date created] tells us when the file was created
Note that these timestamps are managed by Windows and can end up with scenarios where
files are modified before they are created
Recipe: 70.120.1908
Power Query Academy
Subscribers: Click here to For more data patterns check out our Academy at:
see this recipe in action! © www.powerquery.academy
Master Your Data

www.powerquery.academy

© www.powerquery.academy

Вам также может понравиться