Motamedisedeh O. The Ultimate Guide To Functions in Power Query... 2024
Motamedisedeh O. The Ultimate Guide To Functions in Power Query... 2024
Guide to Functions
in Power Query
Turn Raw Data into
Actionable Insights
—
Omid Motamedisedeh
The Ultimate Guide to
Functions in Power
Query
Turn Raw Data into
Actionable Insights
Omid Motamedisedeh
The Ultimate Guide to Functions in Power Query: Turn Raw Data into
Actionable Insights
Omid Motamedisedeh
Brisbane, QLD, Australia
Acknowledgments�����������������������������������������������������������������������������xxi
Introduction�������������������������������������������������������������������������������������xxiii
v
Table of Contents
Table��������������������������������������������������������������������������������������������������������������31
Function���������������������������������������������������������������������������������������������������������31
Type Conversion��������������������������������������������������������������������������������������������������32
Converting Values to the Number Format������������������������������������������������������33
Converting Values to the Text Format������������������������������������������������������������34
Converting Values to the Date and Time Format�������������������������������������������35
Converting Values to the Record Format�������������������������������������������������������35
Conclusion����������������������������������������������������������������������������������������������������������36
vi
Table of Contents
vii
Table of Contents
Date.Day��������������������������������������������������������������������������������������������������������94
Date.DayOfWeek��������������������������������������������������������������������������������������������94
Date.DayOfWeekName�����������������������������������������������������������������������������������95
Date.WeekOfMonth����������������������������������������������������������������������������������������95
Date.WeekOfYear�������������������������������������������������������������������������������������������96
Date.Month����������������������������������������������������������������������������������������������������96
Date.MonthName�������������������������������������������������������������������������������������������97
Date.DayOfYear����������������������������������������������������������������������������������������������97
Date.Year�������������������������������������������������������������������������������������������������������98
Summary������������������������������������������������������������������������������������������������������������98
viii
Table of Contents
List.Select����������������������������������������������������������������������������������������������������122
List.FindText������������������������������������������������������������������������������������������������123
Removing Values in a List���������������������������������������������������������������������������������124
List.RemoveNull�������������������������������������������������������������������������������������������124
List.Distinct��������������������������������������������������������������������������������������������������125
List.RemoveFirsN and List.RemoveLastN����������������������������������������������������126
List.RemoveRange���������������������������������������������������������������������������������������127
List.RemoveItems����������������������������������������������������������������������������������������128
List.Skip�������������������������������������������������������������������������������������������������������129
List.Alternate�����������������������������������������������������������������������������������������������129
List Merging������������������������������������������������������������������������������������������������������131
List.Combine������������������������������������������������������������������������������������������������131
List.InsertRange������������������������������������������������������������������������������������������132
List.Zip���������������������������������������������������������������������������������������������������������133
List.Union�����������������������������������������������������������������������������������������������������133
List.Intersect������������������������������������������������������������������������������������������������134
List.Difference���������������������������������������������������������������������������������������������135
Mathematical Calculations on the Data of a List����������������������������������������������136
List Information Functions��������������������������������������������������������������������������������139
List.IsEmpty�������������������������������������������������������������������������������������������������140
List.IsDistinct�����������������������������������������������������������������������������������������������140
List.Contains������������������������������������������������������������������������������������������������141
List.ContainsAll��������������������������������������������������������������������������������������������142
List.ContainsAny������������������������������������������������������������������������������������������143
List.AllTrue���������������������������������������������������������������������������������������������������144
List.AnyTrue�������������������������������������������������������������������������������������������������144
ix
Table of Contents
Positions in a List����������������������������������������������������������������������������������������������144
List.Positions�����������������������������������������������������������������������������������������������145
List.PositionOf���������������������������������������������������������������������������������������������145
List.PositionOfAny����������������������������������������������������������������������������������������147
Replacing and Transforming Values������������������������������������������������������������������148
List.ReplaceValue����������������������������������������������������������������������������������������148
List.ReplaceMatchingItems�������������������������������������������������������������������������149
List.ReplaceRange���������������������������������������������������������������������������������������151
List.Transform����������������������������������������������������������������������������������������������152
List.TransformMany�������������������������������������������������������������������������������������153
Other Functions�������������������������������������������������������������������������������������������������154
List.Single����������������������������������������������������������������������������������������������������154
List.SingleOrDefault�������������������������������������������������������������������������������������154
List.Repeat���������������������������������������������������������������������������������������������������155
List.Split�������������������������������������������������������������������������������������������������������156
Summary����������������������������������������������������������������������������������������������������������157
Chapter 7: Records���������������������������������������������������������������������������159
Defining a Record���������������������������������������������������������������������������������������������159
Record Properties���������������������������������������������������������������������������������������������162
Record.FieldCount���������������������������������������������������������������������������������������162
Record.FieldNames�������������������������������������������������������������������������������������163
Record.FieldValues��������������������������������������������������������������������������������������163
Record.ToList�����������������������������������������������������������������������������������������������164
Record.Field������������������������������������������������������������������������������������������������164
Record.FieldOrDefault���������������������������������������������������������������������������������165
Record.HasFields�����������������������������������������������������������������������������������������166
x
Table of Contents
Record Correction���������������������������������������������������������������������������������������������167
Record.AddField������������������������������������������������������������������������������������������167
Record.Combine������������������������������������������������������������������������������������������168
Record.SelectFields�������������������������������������������������������������������������������������168
Record.RemoveFields����������������������������������������������������������������������������������170
Record.RenameFields����������������������������������������������������������������������������������172
Record.ReorderFields����������������������������������������������������������������������������������173
Record.TransformFields�������������������������������������������������������������������������������174
Record Conversion��������������������������������������������������������������������������������������������175
Record.FromList������������������������������������������������������������������������������������������175
Record.FromTable����������������������������������������������������������������������������������������176
Record.ToTable��������������������������������������������������������������������������������������������177
Summary����������������������������������������������������������������������������������������������������������177
Chapter 8: Tables������������������������������������������������������������������������������179
Creating a Table������������������������������������������������������������������������������������������������180
Table.FromColumns�������������������������������������������������������������������������������������180
Table.FromRows������������������������������������������������������������������������������������������182
Table.FromRecords��������������������������������������������������������������������������������������185
Table.FromValue������������������������������������������������������������������������������������������187
Table Conversion�����������������������������������������������������������������������������������������������189
Table.ToRecords�������������������������������������������������������������������������������������������190
Table.ToColumns������������������������������������������������������������������������������������������190
Table.ToRows�����������������������������������������������������������������������������������������������191
Table.ToList��������������������������������������������������������������������������������������������������192
Table.Column�����������������������������������������������������������������������������������������������193
Table.TransformRows����������������������������������������������������������������������������������194
Table.Split����������������������������������������������������������������������������������������������������195
Table.SplitAt�������������������������������������������������������������������������������������������������196
xi
Table of Contents
Information��������������������������������������������������������������������������������������������������������197
Selecting Table Rows����������������������������������������������������������������������������������������200
Table.FirstValue�������������������������������������������������������������������������������������������201
Table.First and Table.Last����������������������������������������������������������������������������201
Table.FirstN and Table.LastN������������������������������������������������������������������������202
Table.Max and Table.Min�����������������������������������������������������������������������������204
Table.MaxN and Table.MinN�������������������������������������������������������������������������206
Table.Range�������������������������������������������������������������������������������������������������208
Table.SelectRows����������������������������������������������������������������������������������������209
Table.SelectRowsWithErrors������������������������������������������������������������������������212
Deleting Table Rows������������������������������������������������������������������������������������������214
Table.RemoveFirstN and Table.RemoveLastN����������������������������������������������214
Table.Distinct�����������������������������������������������������������������������������������������������217
Table.Skip����������������������������������������������������������������������������������������������������219
Table.RemoveRows��������������������������������������������������������������������������������������221
Table.AlternateRows������������������������������������������������������������������������������������222
Table.RemoveRowsWithErrors���������������������������������������������������������������������224
Table.RemoveMatchingRows�����������������������������������������������������������������������225
Insert and Replace Table Rows�������������������������������������������������������������������������227
Table.InsertRows�����������������������������������������������������������������������������������������228
Table.ReplaceRows��������������������������������������������������������������������������������������229
Table.ReplaceMatchingRows�����������������������������������������������������������������������231
Table.Repeat������������������������������������������������������������������������������������������������232
Order of rows����������������������������������������������������������������������������������������������������234
Table.ReverseRows�������������������������������������������������������������������������������������234
Table.Sort�����������������������������������������������������������������������������������������������������235
Checking a Condition in All Rows����������������������������������������������������������������������238
xii
Table of Contents
Table.IsDistinct��������������������������������������������������������������������������������������������238
Table.MatchesAllRows���������������������������������������������������������������������������������239
Table.MatchesAnyRows�������������������������������������������������������������������������������241
Table.Contains���������������������������������������������������������������������������������������������242
Table.ContainsAll and Table.ContainsAny����������������������������������������������������244
Table.SingleRow������������������������������������������������������������������������������������������245
Column Names��������������������������������������������������������������������������������������������������246
Table.ColumnNames������������������������������������������������������������������������������������246
Table.DemoteHeaders����������������������������������������������������������������������������������246
Table.PromoteHeaders���������������������������������������������������������������������������������247
Table.HasColumns���������������������������������������������������������������������������������������249
Table.ColumnsOfType����������������������������������������������������������������������������������250
Table.PrefixColumns������������������������������������������������������������������������������������251
Table.RenameColumns��������������������������������������������������������������������������������252
Modifing columns���������������������������������������������������������������������������������������������255
Table.RemoveColumns��������������������������������������������������������������������������������256
Table.SelectColumns�����������������������������������������������������������������������������������259
Table.DuplicateColumn��������������������������������������������������������������������������������263
Table.TransformColumnTypes����������������������������������������������������������������������265
Table.TransformColumns�����������������������������������������������������������������������������267
Table.AddIndexColumn��������������������������������������������������������������������������������271
Table.AddColumn�����������������������������������������������������������������������������������������274
Table.AddRankColumn���������������������������������������������������������������������������������277
Table.SplitColumn����������������������������������������������������������������������������������������281
Table.CombineColumns�������������������������������������������������������������������������������285
Table.CombineColumnsToRecord����������������������������������������������������������������288
Table.ReorderColumns���������������������������������������������������������������������������������290
Table.Pivot���������������������������������������������������������������������������������������������������291
xiii
Table of Contents
Table.Unpivot�����������������������������������������������������������������������������������������������294
Table.Group��������������������������������������������������������������������������������������������������297
Table.Transpose�������������������������������������������������������������������������������������������301
Combining Tables����������������������������������������������������������������������������������������������303
Table.Combine���������������������������������������������������������������������������������������������303
Table.Join����������������������������������������������������������������������������������������������������308
Table.NestedJoin�����������������������������������������������������������������������������������������317
Expanding Columns������������������������������������������������������������������������������������������319
Table.ExpandListColumn������������������������������������������������������������������������������320
Table.ExpandRecordColumn������������������������������������������������������������������������322
Table.ExpandTableColumn���������������������������������������������������������������������������326
Table.AggregateTableColumn����������������������������������������������������������������������329
Replacing Column Values����������������������������������������������������������������������������������330
Table.FillDown and Table.FillUp�������������������������������������������������������������������331
Table.ReplaceErrorValues����������������������������������������������������������������������������334
Table.ReplaceValue��������������������������������������������������������������������������������������337
Finding a Value in a Table����������������������������������������������������������������������������������340
Table.FindText����������������������������������������������������������������������������������������������340
Table.PositionOf�������������������������������������������������������������������������������������������341
Table.PositionOfAny�������������������������������������������������������������������������������������344
Summary����������������������������������������������������������������������������������������������������������345
xiv
Table of Contents
Excel.CurrentWorkbook�������������������������������������������������������������������������������������357
Summary����������������������������������������������������������������������������������������������������������357
Index�������������������������������������������������������������������������������������������������383
xv
About the Author
Omid Motamedisedeh is a freelance data and
business intelligence analyst with a PhD in
Industrial Engineering. He’s currently working
towards his second PhD at Queensland
University of Technology. Omid is the author
of several articles in industry journals (Electric
Power Systems Research, Journal of Building
Engineering, Energy, Neural Computing and
Applications, and others), and seven books (all
in Farsi) namely Managing Data Entry in Excel by Data Validation, Data
Analysis by Excel’s Pivot Table, Power Query as The Magic of Excel, 222
Most Useful Functions in Excel 2016, Useful Excel Functions in Excel 2019,
Reference of Functions in Excel 2021, and From Drawing Charts to Creating
Dashboards in Excel 2016.
xvii
About the Technical Reviewer
Bo Rydobon (a.k.a. Excel Wizard) is a Microsoft Excel expert and YouTuber
from Thailand who is known for his speed and mastery of Excel. He is also
the creator of the Excel Wizard YouTube channel, where he shares tips and
tricks on how to use advanced dynamic arrays. He actively contributes
on Excelforum.com and became a Top 5 reputation within a few years.
His participation in the Excel BI LinkedIn Excel Challenge drew notable
attention due to his impressive response. In 2022, Bo Rydobon won the
Excel Esports Speedrun organized by the Financial Modeling World Cup.
Bo Rydobon is an inspiration to Excel users all over the world.
He shows that anyone can learn Excel and become an expert, regardless of
their background or experience.
xix
Acknowledgments
I would like to express my sincere gratitude to the individuals who have
played an indispensable role in supporting me throughout the process
of writing this book. My appreciation extends to both those within my
professional circle and those who hold a personal place in my life.
First and foremost, I extend heartfelt thanks to Faranak, whose
unwavering belief in me and encouragement provided the catalyst for
embarking on this writing journey.
I am equally grateful to Bo Rydobon, my esteemed technical reviewer,
whose invaluable insights and motivating feedback have significantly
enriched the quality of this work.
Furthermore, I wish to extend my thanks to the Apress team, including
Susan McDermott (Editorial Director), Shobana Srinivasan (Book
Coordinator), and Shaul Elson (Editorial Manager), for their commendable
professionalism and collaborative approach. Their contributions have
transformed working with Apress into a truly gratifying experience.
Lastly, my profound and enduring appreciation goes to Professor
Hamid Reza Irani, who imparted foundational knowledge of Excel during
my undergraduate years, and to Mike Girvin, a cherished Excel mentor.
Mike's prolific Excel tips shared on the ExcelIsFun YouTube channel have
been a wellspring of learning for all Excel fans like me.
xxi
Introduction
In today's data-driven landscape, the ability to seamlessly transform raw
data into meaningful insights is a skill of paramount importance. The
Ultimate Guide to Functions in Power Query stands as a beacon for Excel
and Power BI enthusiasts, equipping them with the tools to unlock the full
potential of their data manipulation endeavors.
In this comprehensive guide, you will delve into the world of Power
Query functions, the building blocks that empower users to effortlessly
manipulate, cleanse, and refine their data. Whether you're a seasoned
data professional or a novice user, this book is designed to elevate your
proficiency in harnessing the capabilities of Power Query functions,
ultimately enabling you to convert seemingly complex datasets into
actionable insights.
Join me on a journey through the intricacies of Power Query functions
as I demystify their functionalities, explore real-world use cases, and
provide step-by-step guidance on their application. With a meticulous
balance of theory and hands-on practice, The Ultimate Guide to Functions
in Power Query is not just a book; it's your indispensable companion in
mastering the art of data transformation.
Chapters include
xxiii
Introduction
xxiv
Introduction
xxv
CHAPTER 1
Introduction to
Power Query
In the dynamic landscape of modern data analysis, the ability to turn
raw information into meaningful insights is a cornerstone of success.
Welcome to the world of Power Query, an invaluable tool that empowers
data professionals to tame, reshape, and amplify data, unveiling its hidden
potential. This chapter serves as your gateway to understanding the
essence of Power Query, its significance, and the journey you are about to
embark upon.
In a data-driven era, information is abundant, sprawling across
spreadsheets, databases, cloud storage, and beyond. But this information
is often disparate, inconsistent, and unstructured. This is where Power
Query steps in as a guiding light, enabling you to navigate through
these complexities and emerge with knowledge that fuels smarter
decision-making.
Your exploration begins with a foundational understanding of what
Power Query truly is: a powerful data transformation tool embedded
within familiar environments such as Microsoft Excel and Power BI. I’ll
unveil its intuitive interface, its versatile functions, and its innate ability to
streamline even the most intricate data manipulation tasks.
This tool was introduced as an add-in for Excel 2010 and later for 2013.
In these versions, by installing the add-in, a new tab called Power Query
was added to Excel, as shown in Figures 1-1 and 1-2.
2
Chapter 1 Introduction to Power Query
In Excel 2016 and later, this tool was added to the main Excel
commands in the Data tab. In Excel 2021 and also 365, the commands
related to Power Query are available in the Get & Transform Data group
under the Data tab, and users can perform data extraction, modification,
and loading processes using these commands (Figure 1-3).
In Power BI, the commands related to Power Query are located in the
Data section of the Home tab, as shown in Figure 1-4.
3
Chapter 1 Introduction to Power Query
4
Chapter 1 Introduction to Power Query
As you can see in Figure 1-5, different options are provided to extract
the tables from different data sources. To extract values from another Excel
file, select From File, and then From Excel File. The window shown in
Figure 1-6 will appear and you can navigate the desired Excel file.
5
Chapter 1 Introduction to Power Query
You’ll see a list of all tables and sheets in the selected Excel file on the
left side of this window. Depending on the user’s needs, one or more tables
can be selected and called in Power Query for the cleaning process. In this
example, by selecting the 05-Inventory table and selecting the Transform
button, the data is loaded in Power Query, as shown in Figure 1-8. (Now
the data extraction process is completed.)
6
Chapter 1 Introduction to Power Query
Now you can perform the cleaning process via various commands. For
example, to remove the first row of the table (empty row), simply select the
Remove Top Rows command from the Reduce Rows section of the Home
tab. The window shown in Figure 1-9 will appear to determine the number
of rows that you want to remove from the top of the table.
7
Chapter 1 Introduction to Power Query
Enter 1 in the field and confirm it. As you can see in Figure 1-10, the
first row of the table gets removed.
In the next step, to replace the column headers with the first row,
select the Use first row as header command from the Transform tab. See
Figure 1-11 for the result.
8
Chapter 1 Introduction to Power Query
Figure 1-11. Replacing column headers with the first data row
9
Chapter 1 Introduction to Power Query
10
Chapter 1 Introduction to Power Query
11
Chapter 1 Introduction to Power Query
You’ve reached your desired result, so at this stage, the data cleaning
process in this example is complete (the data cleaning process is not
limited to the mentioned commands and will be explained further). For
the final step, load the data as a table in Excel (or Power Pivot, which is
considered the data model). Select the Close & Load To command from
the File tab, and the loading window will appear as shown in Figure 1-15.
In this window, you can specify how the final table should be loaded
into Excel. If you select Table, the result will be visible in Excel, as seen in
Figure 1-16.
12
Chapter 1 Introduction to Power Query
So, in this example, values from other Excel files were extracted,
cleaned, and finally loaded as a table in the Excel file.
13
Chapter 1 Introduction to Power Query
In this window, by selecting the desired table(s) from the left window
and selecting the Transform Data command, the data will be transferred to
Power Query, as you can see in Figure 1-18.
14
Chapter 1 Introduction to Power Query
15
Chapter 1 Introduction to Power Query
M Code
To reopen the Power Query window and modify the created query in the
Excel file, the Queries & Connections mode from the Data section of the
Home tab in the Excel should be activated. A list of active queries will be
16
Chapter 1 Introduction to Power Query
shown in the Queries and Connections section (Figure 1-20). The queries
can be modified by right-clicking them and selecting the Edit option.
The result of query will be shown, as you can see in Figure 1-21.
17
Chapter 1 Introduction to Power Query
The query can be modified step by step in the Applied Step section or
can be modified altogether by selecting the Advanced Editor command in
the Home tab, which will lead to the result shown in Figure 1-22.
The Advanced Editor window contains all the steps taken in extracting
and cleaning the data, written in the M language, which is shown below.
(If it is not clear to you, don’t worry. You are going to learn about these
commands and be able to write them yourself by the end of this book)
Let
Source = Excel.Workbook(File.Contents("C:\Users\omid_\
OneDrive\Desktop\Data\05-Inventory.xlsx"), null, true),
#"06-Inventory_Sheet" = Source{[Item="06-
Inventory",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(#"06-Inventory_
Sheet", [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted
Headers",{{"last update on 2018/02/19", type text},
{"Column2", type text}, {"Column3", type any}}),
#"Removed Top Rows" = Table.Skip(#"Changed Type",1),
18
Chapter 1 Introduction to Power Query
In this example, the Table.Skip function receives the table with the
name #"Changed Type" (which was calculated in the previous step) and
removes the first row. Then it displays the new table as output. Therefore,
the value of the variable #"Removed Top Rows" is equal to a table.
19
Chapter 1 Introduction to Power Query
Table.Skip(
table as table,
optional countOrCondition as any
) as table
This function has two inputs (separated by a comma). The first input
is a table as table, which means that the first input is in the table format
which is called Table (the input type is specified after the as), and the
second input is optional (because of the use of the word Optional) and
is named countOrCondition with a format of any. Finally, since the table
expression is registered at the end of the function, the output of this
function is always a table.
In another line of this code, the variable #"Changed Type2" is defined
as follows:
20
Chapter 1 Introduction to Power Query
Conclusion
In this chapter, you were introduced to Power Query, an essential tool
for data transformation and manipulation in various data analysis and
visualization contexts. This is the beginning of your Power Query journey.
In this chapter, a simple example of a Power Query application was
provided so you could get familiar with the basic features of Power Query
and the M language. You also learned the role of the M language in data
transformations. You also saw the use of commands to clean the values in
a table. In the rest of this book, all of the main functions will be explained
in depth.
The next chapter delves deeper into the types of data in Power Query
including, null, logical values, number, text, list, records, and binary.
21
CHAPTER 2
Data Types
Like any other software, Power Query supports different data types
(formats) and provides a wide range of functions for modifying values
based on their types. Power Query supports simple data types such as
numbers, text, and time/date as well as complex data types like lists, tables,
and binary. This chapter focuses on understanding the fundamental data
types used in Power Query and their characteristics plus handling type
conversions. By the end of this chapter, you will have a solid understanding
of how to effectively manage and manipulate different data types within
Power Query, setting the stage for more advanced data transformation and
analysis in subsequent chapters.
=Text.Start(“Hello”,3) “Hel”
=Text.Start(“1232”,2) “12”
=Text.Start(1232,2) Error
Null Null
true/false Logical
0 1 -1 1.5 2.3e-5 Number
#time(09,15,00) Time
#date(2013,02,26) Date
#datetime(2013,02,26, 09,15,00) DateTime
#datetimezone(2013,02,26, 09,15,00, 09,00) DateTimeZone
#duration(0,1,30,0) Duration
“hello” Text
#binary(“AQID”) Binary
{1, 2, 3} List
[ A = 1, B = 2 ] Record
#table({“X”,“Y”},{{0,1},{1,0}}) Table
(x) => x + 1 Function
type { number } type table [ A = any, B = text ] Type
24
Chapter 2 Data Types
Null
The value of null is used instead of missing values in Power Query and is
defined as null. Some mathematical operations can be done on the null
values. Assuming that the variable x is equal to null and y is a none null
value, various mathematical operations and their results are shown in
Table 2-3.
x>y null
x >= y null
x<y null
x <= y null
x=y null
x <> y null
x ?? y y
y ?? x y
Logical Values
The logical value type of data, with the symbol of , includes two states:
true or false. To compare the values in this group, true can be considered
equivalent to the number 1 and false can be considered equivalent to the
number 0.
Generally, logical values are obtained by comparing one value with
another (for example, 2>3) or the output of information functions such as
Number.IsOdd.
25
Chapter 2 Data Types
Numbers
The numbers type includes a large range of numerical values (integer and
decimal). In addition to numbers, there are two values named #infinity
(obtained from dividing a positive number by zero) and #-infinity
(obtained from dividing a negative number by zero) that represent positive
infinity (the largest possible number) and negative infinity (the smallest
possible number) and they are also included in this group of data.
The #nan value, equal to dividing zero by zero, is also included in
this group of data type. Generally, any mathematical and comparison
operations such as x > y, x >= y, x < y, x <= y, x = y, x <> y, x + y, x - y, x * y, x /
y, x ?? y, and +x can be performed on numbers x and y in Power Query.
Different number formats are provided in Table 2-4.
26
Chapter 2 Data Types
27
Chapter 2 Data Types
#datetimezone(
year, month, day,
hour, minute, second,
offset-hours, offset-minutes)
1≤ year ≤ 9999
1≤ month ≤ 12
1≤ day ≤ 31
0≤ hour ≤ 23
0≤ minute ≤ 59
0≤ second ≤ 59
-14≤ offset-hours ≤ 14
-59 ≤ offset-minutes ≤ 59
28
Chapter 2 Data Types
This format is used to display dates, and it can display any Date
date in the range of year 1900 to 9999.
29
Chapter 2 Data Types
Text
The text format, with the symbol of , includes a wide range of values
and must be enclosed in double quotes (“ ”) to define them, such as “ABC”
or even “2.” The text format is used in Power Query for text with a length of
up to 268 million characters. When a number is entered inside double
quotes, its format is converted to text and mathematical operations are no
longer performed on it. Among the types of operators, comparison
operators (<=>), the operator for checking null values, and & operator for
concatenating two texts can be used for text values.
Binary
In Power Query, a binary value, with the symbol of , is equal to a string
of bits contained in files with a special format. For example, to call
information from an Excel file as tables within Power Query, first the
information is called up as binary values and then it is converted to tables
using various functions.
List
Lists include a set of values (with different data types) in a specific order.
For simplicity, a list can be considered as a column in a table without
a title. In Power Query, to define a list, simply place the desired values
inside { } and separate them from each other using commas. For example,
different examples of lists are given here:
={1,2,3,4}
={1,2,3,{4,5}}
={1,“a”,2,“B”,{“C”,“D”}}
={[a=1,b=2],2,{1},{1,2},[c=2],[d={1,2,3,}]}
30
Chapter 2 Data Types
Record
Another type of data in Power Query is the record. For simplicity, records
can be considered as a single row of a table, where each value has a header
(or title) and there is just a value for each title. To define a record, it is
sufficient to enter the field title and its value inside [ ], separating different
fields with a comma, such as [a=1,b=2]
In the previous example, the record has two fields with the names a
and b, and their values are 1 and 2, respectively. There are no limitations in
the type of values in a record, and they can include various types such as
lists, records, and tables. Here are different examples of records:
[a=1,v={1,2,3,4}]
[a=[b=1,c=2],d=[e={1,2,3,4},f=null]]
[a=“xyz”,b={1,2,3}]
Table
A table is a data format in which values are organized in rows with specific
column titles. The data in a table can have various formats, and there are
different functions in Power Query to manage tables. Tables can be created
directly in Power Query or called from other sources.
Function
Another type of data is a function, which will be explained in detail in
future chapters.
31
Chapter 2 Data Types
Type Conversion
There are several functions in Power Query for converting different data
formats into another, but before getting familiar with them, it should be noted
that not every data format can be converted to another format. For example,
you cannot convert the text format “Hello” to a number with any function.
The possibility of converting between different data formats in Power
Query is shown in Figure 2-1.
32
Chapter 2 Data Types
33
Chapter 2 Data Types
34
Chapter 2 Data Types
35
Chapter 2 Data Types
Conclusion
In this chapter, you explored different data types in Power Query and their
features. You learned about the possibility of changing data types and the
functions for data types conversions. You also learned about errors that
might happen from by using the wrong data types.
Now that you have mastered data types, you are ready to learn about
functions for manipulating values with different data types. In the next
chapter, the functions for working on values with the number type are
presented.
36
CHAPTER 3
Number Functions
This chapter delves into the realm of numeric data manipulation and
analysis within Power Query. These functions allow you to perform various
mathematical calculations and transformations on numeric data. These
functions are essential for manipulating numerical values within your
data, performing calculations, and generating insights. Number functions
can be applied to individual numeric values and columns of numeric data,
or they can be used in combination with other functions to create complex
calculations. The functions in this chapter are categorized into six groups,
which will be discussed in the rest of this chapter.
Constant Values
In Power Query, there are several functions that generate constant values
and they are mentioned in Table 3-1. These functions do not need any
argument, and they are used with opening and closing parenthesis.
Information Functions
In Power Query, there are three functions to check if a value odd, even,
or NaN and they are listed in Table 3-2. All of these functions receive a
numerical input and based on its value, the result of the function will be
True or False.
38
Chapter 3 Number Functions
Number.IsEven(2) True
Number.IsEven(3) False
Number.IsNaN(2) False
Number.IsNaN(2/0) False
Number.IsNaN(0/0) True
Number.IsOdd(2) False
Number.IsOdd(3) True
Rounding Operation
For rounding numbers in Power Query, the functions listed in Table 3-4
can be used. All of these functions receive two arguments:
39
Chapter 3 Number Functions
40
Chapter 3 Number Functions
= Number.Round(4.5)
= Number.Round(3.5)
41
42
Chapter 3
Mathematical Operations
In performing mathematical operations on numbers in Power Query, there
are two groups of functions. Functions in the first group, listed in Table 3-7,
receive a numerical value and perform mathematical calculations on it
based on the function’s type.
The functions in the second group receive two inputs, a and b, and
result as in Table 3-8 based on the used function.
43
Chapter 3 Number Functions
44
Chapter 3 Number Functions
Random Generation
Number.Random and Number.RandomBetween are used in Power Query to
generate random numbers. Number.Random does not take any argument
and is defined as Number.Random(), always resulting in a random number
between 0 and 1. The function Number.RandomBetween takes two numbers
a and b and always outputs a decimal random number between a and b.
Trigonometry
Trigonometric functions in Power Query, like other software, include three
groups of basic, hyperbolic, and inverse trigonometric functions, listed in
Table 3-10.
Conclusion
This chapter introduced you to the functions that manipulate numbers
in Power Query. These functions are categorized into the following
six groups.
45
Chapter 3 Number Functions
46
CHAPTER 4
Text Functions
In the realm of data transformation and manipulation, text data stands
as a foundational building block. Whether dealing with customer names,
product descriptions, log entries, or any other textual content, the ability
to extract, cleanse, and transform text becomes paramount. This is where
the prowess of Power Query truly shines. In this chapter, you will delve
into the art of harnessing text functions within Power Query, unveiling a
toolkit that empowers data professionals to conquer the complexities of
unstructured and semi-structured text data.
Your journey through this chapter will commence with the
fundamentals such as understanding the nuances of text encoding,
manipulation, and the role of Power Query’s text functions in these
processes. You’ll explore how to efficiently extract specific elements from
strings, like isolating URLs from paragraphs or capturing dates from
lengthy narratives.
As you traverse deeper, you’ll uncover the potential of text functions
to cleanse and standardize data. From removing extraneous spaces and
punctuation to converting case formats, these operations are the bedrock
of consistent and reliable text analysis.
These functions are categorized into the following five groups:
• Text corrections
• Text conversion
Text.At
Text.At is used to extract a character (only one character) within a text
based on its position, as follows:
Text.At(
text as nullable text,
index as number
) as nullable text
This function receives the following two arguments, and its result is
always a character within a reference text:
48
Chapter 4 Text Functions
= Text.At("XNW-11-1",0) “X”
= Text.At("XNW-11-1",1) “N”
= Text.At("XNW-11-1",3) “-”
Text.Start
Text.Start is used to extract a specific number of characters from the
beginning of a text, as follows:
Text.Start(
text as nullable text,
count as number
) as nullable text
Entering the number 2 in the second input result in the first two
characters of the reference text. To further examine this function, multiple
examples are provided in Table 4-2.
49
Chapter 4 Text Functions
=Text.Start("Xn1281",2) “Xn”
=Text.Start("Xn1281",3) “Xn1”
=Text.Start("Xn1281",1) “X”
=Text.Start(121,2) Error
The result of the last formula is Error, because the first input is entered
as a number but a text value should be entered. So, in this case before
using Text.Start, you need to convert the first input to text by another
function like Text.From. Therefore, the corrected version of the last
formula is as follows:
=Text.Start(Text.From(121),2)
Text.End
The definition of this function is like Text.Start, but this function is used to
extract a specific number of characters from the end of the reference text.
Text.End(
text as nullable text,
count as number
) as nullable text
50
Chapter 4 Text Functions
=Text.End("Xn1281",2) “81”
=Text.End("Xn1281",3) “281”
=Text.End("Xn1281",1) “1”
The output of this function is always a text and is not summable unless
you convert the result to a number before using it in the formula.
Text.Range
Another function for extracting characters in a text is Text.Range. It is used
for extracting the middle characters of a text as follows:
Text.Range(
text as nullable text,
offset as number,
optional count as nullable number
) as nullable text
In this function, entering the value “Xn192” in the first input, the
number 2 in the second input, and the number 1 in the third input means
51
Chapter 4 Text Functions
=Text.Range("Xn1281",2,1) “1”
=Text.Range("Xn1281",0,3) “Xn1”
=Text.Range("Xn1281",1,2) “n1”
=Text.Range("Xn1281",3,12) Error
Text.AfterDelimiter
Text.AfterDelimiter is used to extract a part of a text after a specific
delimiter, as follows:
Text.AfterDelimiter(
text as nullable text,
52
Chapter 4 Text Functions
delimiter as text,
optional index as any
) as any
The output of this function is always a part of the reference text that
comes after the delimiter. To clarify this function, various examples are
provided in Table 4-5.
= Text.AfterDelimiter("XNW-11-181-M","-") “11-181-M”
= Text.AfterDelimiter("XNW-11-181-M","-",0) “11-181-M”
= Text.AfterDelimiter("XNW-11-181-M","-",1) “181-M”
= Text.AfterDelimiter("XNW-11-181-M","-",2) “M”
= Text.AfterDelimiter("FMn12N16","N") “16”
= Text.AfterDelimiter("FMn12N16","n") “12N16”
= Text.AfterDelimiter("XNW-11-181-M","u",2)
53
Chapter 4 Text Functions
For the last example in Table 4-5, the result will be empty if the
reference text is not included the delimiter.
In the third input, beside entering a number to determine the desired
repetition of the delimiter in the reference text, the searching direction for the
delimiter in the reference text can be specified by entering RelativePosition.
FromStart or RelativePosition.FromEnd beside the number in a list.
For example, the following formula results in the characters after the
first “-” and is equal to “11-181-M”:
= Text.AfterDelimiter(
"XNW-11-181-M",
"-",
0)
In another example, the result of the following formula is the same, but
it is emphasized that it is searching for the first “-” from the beginning of
the text (left to right):
= Text.AfterDelimiter(
"XNW-11-181-M",
"-",
{0,RelativePosition.FromStart})
But by using the following formula, the result is different. In this case,
you are looking for the text after the first dash, but it is emphasized to
search for the first “-” from the end of the text (right to left). So, the result of
the function is equal to “M”:
= Text.AfterDelimiter(
"XNW-11-181-M",
"-",
{0,RelativePosition.FromEnd})
54
Chapter 4 Text Functions
Text.BeforeDelimiter
Text.BeforeDelimiter is defined like Text.AfterDelimiter and is used
to extract the characters before the delimiter in the reference text. This
function is defined in Power Query as follows:
Text.BeforeDelimiter(
text as nullable text,
delimiter as text,
optional index as any
) as any
55
Chapter 4 Text Functions
= Text.BeforeDelimiter("XNW-11-181-M","-",0)
The result of the following formula is the same as the above formula,
except that this formula displays the text before the first “-” by searching
from the beginning of the reference text (left side):
= Text.BeforeDelimiter(
"XNW-11-181-M",
"-",
{0,RelativePosition.FromStart})
The next formula extracts the text before the first dash by searching
from end of the reference text (close to the end of the text). Therefore, the
result of the function is “XNW-11-181”.
56
Chapter 4 Text Functions
= Text.BeforeDelimiter(
"XNW-11-181-M",
"-",
{0,RelativePosition.FromEnd})
Text.BetweenDelimiters
Text.BetweenDelimiters is used to separate a part of text between two
delimiters, such as between “-” and “/” as follows:
Text.BetweenDelimiters(
text as nullable text,
startDelimiter as text,
endDelimiter as text,
optional startIndex as any,
optional endIndex as any
) as any
57
Chapter 4 Text Functions
= Text.BetweenDelimiters(
"XNW-11-181/MN-12/2-",
"-",
"/")
To start a separation from the second repetition of “-”, you just need to
enter the number 1 in the fourth input of this function as follows, so the
result of the formula is “181”:
= Text.BetweenDelimiters(
"XNW-11-181/MN-12/2-",
"-",
"/",
1)
= Text.BetweenDelimiters(
"XNW-11-181/MN-12/2-",
"-",
58
Chapter 4 Text Functions
"/",
1,
1)
Text.Select
To extract special characters from a reference text, Text.Select can be
used as follows:
Text.Select(
text as nullable text,
selectChars as any
) as nullable text
So, by the following formula, all the repetition of characters “2”, “X”, and
“-” should be extracted from the reference text entered in the first input,
and the result is equal to “22-X-2”:
= Text.Select("2023-YXL-12",{"2","X","-"})
59
Chapter 4 Text Functions
= Text.Select("2023-YXL-12",{"A".."Z"})
Text.Split
Text.Split is used to split a reference text into several texts based on a
delimiter. For example, “X-12-M81” can be divided into {“X”, “12”, “M81”}
by this function (in this case, the delimiter is “-”):
Text.Split(
text as text,
separator as text
) as list
This function takes the following two arguments and results in a list of
separated texts:
= Text.Split("XNW-11-181-M","-") {“XNW”,“11”,“181”,“M”}
= Text.Split("XNW-11-181-M","1") }“XNW-”,“”,“-”,“8”,“-M”}
= Text.Split("XNW-11-181-M","n") }“XNW-11-181-M”}
= Text.Split("XNW-11-181-M","N") }“X”,“W-11-181-M”}
60
Chapter 4 Text Functions
= Text.SplitAny("XNW-11-181-M","W-")
Text.Remove
Specific characters can be removed from a reference text by using Text.
Remove as follows.
Text.Remove(
text as nullable text,
removeChars as any
) as nullable text
61
Chapter 4 Text Functions
= Text.Remove("XNW-11-181/MN","1") “XNW--8/MN”
= Text.Remove("XNW-11-181/MN","-") “XNW11181/MN”
= Text.Remove("XNW-11-181/MN","/") “XNW-11-181MN”
= Text.Remove("XNW-11-181/MN",{"1","-","/"}) “XNW8MN”
= Text.Remove("XNW-11-181/MN",{"A".."Z"}) “-11-181/”
= Text.Remove("XNW-11-181/MN",{"0".."9"})
By using the following formula, all capital letters in the text can be
removed. In this case, the result of the formula is equal to “ -11-181/ ”.
= Text.Remove("XNW-11-181/MN",{"A".."Z"})
= Text.Remove("XNW-11-181/MN",{"a".."z","A".."Z"})
Text.RemoveRange
The Text.RemoveRange function is used to remove a sequence of
characters in the reference text based on their position (for example,
removing the seventh to tenth characters within a specific text). This
function is defined in Power Query as follows:
62
Chapter 4 Text Functions
Text.RemoveRange(
text as nullable text,
offset as number,
optional count as nullable number
) as nullable text
= Text.RemoveRange("2023-YXL-12",0,5) “YXL-12”
= Text.RemoveRange("2023-YXL-12",4,5) “202312”
= Text.RemoveRange("2023-YXL-12",8,3) “YXL-12”
= Text.RemoveRange("2023-YXL-12",8,5) Error
Text.PadEnd
By using Text.PadEnd, the length of a text (number of characters) can
be increased to the desired number and new spaces can be filled with a
specified character at the end of the text.
63
Chapter 4 Text Functions
Text.PadEnd(
text as nullable text,
count as number,
optional character as nullable text
) as nullable text
= Text.PadEnd("AW",5,"X")
= Text.PadEnd("AW",5,"X") “AWXXX”
= Text.PadEnd("AW",5) “AW ”
= Text.PadEnd("AW",1,"X") “AW”
64
Chapter 4 Text Functions
Text.PadStart
The Text.PadStart function is like the Text.PadEnd function. The
difference is that in this function, the desired character will be placed
at the beginning of the reference text. In other words, by using Text.
PadStart, the length of a text can be increased by repeating new
character(s) at the beginning of it. This function is defined as follows:
Text.PadStart(
text as nullable text,
count as number,
optional character as nullable text
) as nullable text
= Text.PadStart("AW",5,"X") “XXXAW”
= Text.PadStart("AW",5) “ AW”
= Text.PadStart("AW",1,"X") “AW”
65
Chapter 4 Text Functions
Text.Replace
By using this function, a substring in a text can be replaced with new
characters as follows:
Text.Replace(
text as nullable text,
old as text,
new as text
) as nullable text
= Text.Replace("XNW-11-181/MN","-","/") “XNW/11/181/MN”
= Text.Replace("XNW-11-181/MN","/","-") “XNW-11-181-MN”
= Text.Replace("XNW-11-181/MN","XNW","U") “U-11-181/MN”
66
Chapter 4 Text Functions
Text.ReplaceRange
Text.ReplaceRange is used to replace several characters with new
characters based on their position in the reference text. This function is
defined in the following way in Power Query:
Text.ReplaceRange(
text as nullable text,
offset as number,
count as number,
newText as text
) as nullable text
.
= Text.RemoveRange("2023-YXL-12",5,3,"A")
67
Chapter 4 Text Functions
Text.Insert
To add new characters to the reference text, Text.Insert is used as
follows:
Text.Insert(
text as nullable text,
offset as number,
newText as text
) as nullable text
= Text.Insert("2023-YXL-12",0,"AB-") “AB-2023-YXL-12”
= Text.Insert("2023-YXL-12",5,"AB-") “2023-AB-YXL-12”
= Text.Insert("2023-YXL-12",9,"AB-") “2023-YXL-AB-12”
68
Chapter 4 Text Functions
Text.Repeat
The Text.Repeat function is used to repeat a text as desired numbers, as
follows:
Text.Repeat(
text as nullable text,
count as number
) as nullable text
= Text.Repeat("X",3) “XXX”
= Text.Repeat("X-",3) “X-X-X-”
= Text.Repeat("X-Y",3) “X-YX-YX-Y”
Text.Combine
By using Text.Combine, two or more texts can be combined and result
in a text.
Text.Combine(
texts as list,
optional separator as nullable text
) as text
69
Chapter 4 Text Functions
= Text.Combine({"A","B","C"}) “ABC”
= Text.Combine({"A","B","C"},"-") “A-B-C”
= Text.Combine({"A","B","C"}," ") “A B C”
Text Correction
There are various functions in Power Query to modify texts. These
functions mostly receive a text as input and, after removing extra spaces,
special characters, and capitalizing or lowercasing letters in the reference
text, display the corrected text as output. Table 4-16 lists these functions
along with their application.
70
Chapter 4 Text Functions
Text.Lower* Using this function, all English letters in the inputed text will be
converted to lowercase.
Text.Upper* The output of this function will be the same as the input
text, except that all letters in the output will be displayed in
uppercase.
Text.Proper* The output of this function will be the same as the input text,
except that in the output text, the first letter of all words will be
displayed in uppercase, and the other letters will be displayed in
lowercase.
Text.Reverse Using this function, the sequence of characters in the reference
text will be reversed.
Text.Trim* Using this function, all extra spaces in a text (spaces at the
beginning, ending, or spaces next to each other) can be
removed.
Text.TrimEnd* Using this function, extra spaces at the end of the text can be
removed.
Text.TrimStart* Using this function, extra spaces at the beginning of the text can
be removed.
Text.Clean Using this function, special characters such as #(if) within the
text can be removed, in addition to extra spaces.
Text.Length This function is used to determine the number of characters in
the reference text.
71
Chapter 4 Text Functions
Text.StartsWith
To check the initial characters of a text, Text.StartsWith can be used as
follows:
Text.StartsWith(
text as nullable text,
substring as text,
optional comparer as nullable function
) as nullable logical
72
Chapter 4 Text Functions
Using this function, you can check whether a text starts with certain
characters or not. This function takes three arguments as follows, and its
output is always True or False.
= Text.StartsWith("XN18-M13","X") True
= Text.StartsWith("XN18-M13","XN1") True
= Text.StartsWith("XN18-M13","xn") False
= Text.StartsWith("XN18-M13","xn", Comparer. True
OrdinalIgnoreCase)
73
Chapter 4 Text Functions
Text.EndsWith
To examine the ending characters of a text, Text.EndsWith can be used as
follows:
Text.EndsWith(
text as nullable text,
substring as text,
optional comparer as nullable function
) as nullable logical
By using this function, you can check whether a text ends with certain
characters or not. This function receives the following three inputs, and its
output is always equal to True or False.
74
Chapter 4 Text Functions
= Text.EndsWith("XN18-M13","M13") True
= Text.EndsWith("XN18-M13","13") True
= Text.EndsWith("XN18-M13","m13") False
= Text.EndsWith("XN18-M13","m13", Comparer. True
OrdinalIgnoreCase)
Text.Contains
To check the existence of substring in a reference text, Text.Contains can
be used as follows:
Text.Contains(
text as nullable text,
substring as text,
optional comparer as nullable function
) as nullable logical
75
Chapter 4 Text Functions
= Text.Contains("XN18-M13","M") True
= Text.Contains("XN18-M13","8-M") True
= Text.Contains("XN18-M13","m") False
= Text.Contains("XN18-M13","M", True
Comparer.OrdinalIgnoreCase)
Text.PositionOf
To obtain the position of a substring in the reference text, Text.
PositionOf can be used as follows:
Text.PositionOf(
text as text,
substring as text,
optional occurrence as nullable number,
optional comparer as nullable function
) as any
76
Chapter 4 Text Functions
77
Chapter 4 Text Functions
= Text.PositionOf("XNW-11-181-M","-") 3
= Text.PositionOf("XNW-11-181-M","-",1) 10
= Text.PositionOf("XNW-11-181-M","-", 10
Occurrence.Last)
= Text.PositionOf("XNW-11-181-M","-",2) {3,6,10}
= Text.PositionOf("XNW-11-181-M","R") -1
Text.PositionOfAny
Text.PositionOfAny is like Text.PositionOf. The difference is that using
Text.PositionOfAny means it’s possible to simultaneously search for
multiple strings within the reference text, as follows:
Text.PositionOfAny(
text as text,
characters as list,
optional occurrence as nullable number
) as any
78
Chapter 4 Text Functions
= Text.PositionOfAny("XNW-11-181-M",{"-"}) 3
= Text.PositionOfAny("XNW-11-181-M",{"1"}) 4
= Text.PositionOfAny("XNW-11-181-M",{"-","1"}) 3
= Text.PositionOfAny("XNW-11-181-M",{"-","1"},1) 10
= Text.PositionOfAny("XNW-11-181-M",{"-","1"},2) {3,4,5,6,7,9,10}
Text Conversion
In Power Query, the format of values is important, and each function
works on a specific range of formats. For example, in the first input of
Text.Start, a text must be entered, and entering a number will result
in an error. So, to separate the first four characters (year) in the number
20231123, firstly it should be converted to text and then you can perform
the desired operation on it.
There are different functions in Power Query for values format
conversion, which are explained in Table 4-23.
79
Chapter 4 Text Functions
Character.FromNumber(65) “A”
Character.ToNumber("A") 65
Text.From(83) “83”
Text.ToList("Hello") {“H”,“e”,“L”,“L”,“O”}
Value.FromText("83") 83
80
Chapter 4 Text Functions
Summary
In this chapter, you embarked on a journey through the world of text
functions within Power Query, unveiling a toolkit that empowers data
professionals to conquer the complexities of unstructured and semi-
structured text data. Textual information, from customer feedback to
product descriptions, is integral to our digital lives, and mastering its
manipulation is essential for data transformation.
You began by understanding the fundamentals of text encoding,
manipulation, and the role of Power Query’s text functions in these
processes. You explored the art of efficiently extracting specific elements
from strings, a skill that allows you to isolate URLs from paragraphs or
capture dates from lengthy narratives.
Moving forward, you delved into techniques for cleansing and
standardizing data, including removing extraneous spaces, punctuation,
and converting case formats. These operations form the foundation of
consistent and reliable text analysis.
To complete the Power Query functions, the functions related to
manipulating the dates and times are presented in the next chapter. These
functions are used to extract the required information from the values
in the format of time and date. So you will learn how to add or subtract
specific values from dates or how to compare the different date values.
81
CHAPTER 5
DateTime.LocalNow() as datetime
Date Information
To examine if a date is in a specific date period (such as last week or next
month) or not, several functions can be used. The following is a list of the
functions you can use to examine the availability of a date in the next,
previous, or current time period (just one time period):
84
Chapter 5 Date and Time Functions
Date.IsInPreviousNDays Date.IsInNextNDays
Date.IsInPreviousNWeeks Date.IsInNextNWeeks
Date.IsInPreviousNMonths Date.IsInNextNMonths
Date.IsInPreviousNQuarters Date.IsInNextNQuarters
Date.IsInPreviousNYears Date.IsInNextNYears
85
86
Chapter 5
CurrentMonth(x) F F F T F F F F F F F F F F F F F F
NextMonth(x) F F F F F F F F F F F F F F F F F F
Date and Time Functions
PreviousMonth(x) T F F F F F F F F F F F F F F F F F
NextYear(x) F F F F F F F F F F F T F F F F F F
CurrentYear(x) T F T T T T T T T F F F F F F F T F
NextNQuarters(x,5) F F T F T F F F T F F T F F F F T F
PreviousQuarter(x) T F F F F T F T F F F F F F F F F F
NextNMonths(x,7) F F T F T F T F T F F F F F F F T F
CurrentWeek(x) F F F F F F F F F F F F F F F F F F
Chapter 5 Date and Time Functions
DateTime Information
Like the functions in the previous section, this group of information
functions also includes two groups. The first group of functions, including
the following functions, receives one input and is used to check the
existence of an entered time in the input during a time (hour, minute, or
second) before or after the current time. The following cases are included:
DateTime.IsInCurrentHour
DateTime.IsInCurrentMinute
DateTime.IsInCurrentSecond
DateTime.IsInNextHour
DateTime.IsInNextMinute
DateTime.IsInNextSecond
DateTime.IsInNextNHours
DateTime.IsInNextNMinutes
DateTime.IsInNextNSeconds
The second group of functions receives two inputs (date and time to be
checked and N as the number of time periods) and are used to check the
existence of a time in N time periods (hour, minute, or second) before or
after the current time. The functions in this group are as follows:
DateTime.IsInPreviousHour
DateTime.IsInPreviousMinute
DateTime.IsInPreviousNHours
DateTime.IsInPreviousNMinutes
Assuming that the current time is 4:30 on April 14, 2023, the result of
various formulas is shown in Table 5-2. (To shorten the table, the phrase
DateTime.IsIn has been removed from the beginning of all functions, and
the term X is used instead of the date and time value.)
87
Chapter 5 Date and Time Functions
88
Chapter 5 Date and Time Functions
89
Chapter 5 Date and Time Functions
S
hifting Dates
To determine the start or end date of the week, month, season, or year of a
specific date, the following functions can be used:
Date.EndOfDay Date.StartOfDay
Date.EndOfWeek Date.StartOfWeek
Date.EndOfMonth Date.StartOfMonth
Date.EndOfQuarter Date.StartOfQuarter
Date.EndOfYear Date.StartOfYear
90
Chapter 5 Date and Time Functions
91
Chapter 5 Date and Time Functions
92
Chapter 5 Date and Time Functions
Conversion to Record
Several functions can be used to convert date values to the record. The
Time.ToRecord, as the initial function, is used to convert a time value as
follows to a record:
As you can see, this function receives an input and displays its values
in a record in the output. Therefore, the result of the formula DateTime.ToR
ecord(#datetime(2022,5,3,2,10,30)) is equal to the following value:
93
Chapter 5 Date and Time Functions
Date Specifications
There are different functions, described below, in Power Query to extract
the information (number of day, month, or year) from date values.
Date.Day
This function is defined as follows in Power Query and takes an input of
type date, and in the output, and the day value of the reference date is
shown in the result.
In other words, this function displays the day part of the entered date
in the output. Therefore, the result of the following formula will be equal to
the number 3:
= Date.Day(#date(2022,5,3))
Date.DayOfWeek
Using this function, the day number (in a week) of a date can be
determined. This function is defined as follows in Power Query and
receives two inputs. The first input is of type date, and in the second input,
entering values such as Day.Sunday or Day.Monday, you specify which day
of the week is the first day of each week.
Date.DayOfWeek(
dateTime as any,
optional firstDayOfWeek as nullable number
) as nullable number
94
Chapter 5 Date and Time Functions
This function always returns a number and represents the day of the
week. The result of the following formula is equal to 1:
= Date.DayOfWeek(#date(2022,5,3))
Date.DayOfWeekName
This function is like the Date.DayOfWeek function, but instead of the day
number in the week, the day name will be displayed in the output by this
function. This function is defined in Power Query as follows:
Date.DayOfWeekName(
date as any,
optional culture as nullable text
)
The first input of this function represents the reference date and in the
second input, which is optional, the logic of day naming by entering an
expression such as "en-US" can be determined. The result of the following
formula is equal to Tuesday:
= Date.MonthName(#date(2022,5,3))
Date.WeekOfMonth
To determine the week number in a month, the following function can
be used:
Date.WeekOfMonth(
dateTime as any,
optional firstDayOfWeek as nullable number
) as nullable number
95
Chapter 5 Date and Time Functions
Based on the function syntax, this function receives two inputs. The
first input is of type date and in the second input, entering values such as
Day.Sunday or Day.Monday, you specify which day of the week is the first
day of the week. According to the function explanations, the result of the
following formula is equal to 19, indicating that this date belongs to the
second week of the year:
= Date.WeekOfYear(#date(2022,5,3))
Date.WeekOfYear
To determine the week number (from the beginning of the year) of a date,
this function can be used as follows:
Date.WeekOfYear(
dateTime as any,
optional firstDayOfWeek as nullable number
) as nullable number
Based on the function syntax, it takes two inputs. The first input is of
type date and in the second input, entering values such as Day.Sunday or
Day.Monday, you specify which day of the week is the first day of the week.
According to the function explanations, the result of the following formula
is equal to 19, indicating that this date belongs to the 19th week of the year:
= Date.WeekOfYear(#date(2022,5,3))
Date.Month
This function is defined in Power Query as follows and receives one input
of type date and specifies the month number of entered date:
96
Chapter 5 Date and Time Functions
In other words, this function displays the month section of the entered
date in the output. Therefore, the result of the following formula is equal to 5:
= Date.Month(#date(2022,5,3))
Date.MonthName
This function is like the Date.Month function, but instead of the month
number, the month name will be displayed in the output. This function is
defined in Power Query as follows:
Date.MonthName(
date as any,
optional culture as nullable text
) as nullable text
The first input of this function represents the reference date and in
the second input, which is optional, you can define the logic of the month
naming by entering an expression such as "en-US". The result of the
following formula is equal to May:
= Date.MonthName(#date(2022,5,3))
Date.DayOfYear
This function is defined in Power Query as follows and receives one input
of type date and specifies in the output the day number of the entered date
since the beginning of the year:
Therefore, the result of the following formula is 123, indicating that this
date belongs to the 123rd day of the year:
= Date.DayOfYear(#date(2022,5,3))
97
Chapter 5 Date and Time Functions
Date.Year
This function is defined in Power Query as follows. It receives one input of
type date and the year value of the entered date will determine in the output.
In other words, this function displays the year section of the entered a date
in the output. Therefore, the result of the following formula is equal to 2022:
= Date.Year(#date(2022,5,3))
Summary
In this chapter, you embarked on a journey to master the intricacies of the
date and time functions within Power Query. In the realm of data analysis,
time is an essential dimension that requires precision and finesse to
manipulate effectively.
Your journey began with a deep dive into foundational concepts, such
as the nuances of date encoding, time zones, and arithmetic. Armed with
this knowledge, you explored the power of date and time functions in
transforming raw timestamps into intelligible and meaningful information.
As you ventured further, you discovered how these functions facilitate
the extraction of day-of-week patterns, calculations of durations between
significant events, and standardization of temporal data formats. This laid
the groundwork for effective temporal analysis.
In the next chapter, you’ll explore a wide range of functions to
manipulate list values. Based on these functions, you can easily generate
different lists, filter and sort the values in a list, extract a specific value from
a list, or combine lists.
98
CHAPTER 6
List Functions
In the vast landscape of data manipulation, individual data points often
weave together to create a larger narrative. This is where the power of
lists comes into play. From collections of sales transactions to arrays
of survey responses, lists capture the essence of data relationships
and dependencies. Welcome to a chapter dedicated to exploring the
realm of list functions within Power Query. They are your gateway to
unraveling intricate data structures and unleashing the true potential of
interconnected information.
Lists are the threads that weave the fabric of data stories. Imagine
dissecting an order into its constituent products, tracking a project’s multi-
step workflow, or comprehending the dynamics of customer preferences.
These scenarios demand the mastery of list functions, the tools that
empower data professionals to manipulate, dissect, and reassemble data
in ways that were once intricate challenges.
Your journey through this chapter begins with a foundational
understanding of lists as versatile data structures. You’ll explore their
unique properties, from their ordered nature to the ability to hold
heterogeneous data types. With this knowledge in hand, you’ll plunge
into the world of list creation, from the manual assembly of elements to
dynamic generation through calculated expressions.
But lists don’t merely exist in isolation; they interact, transform, and
adapt. You’ll explore the arsenal of list functions that Power Query offers
to reshape data collections to your exact specifications. Whether it’s
={1,2,3,4,5}
={"a","b","c"}
100
Chapter 6 List Functions
={1..100}
This method is not only for numbers. Characters between c and n can
easily defined as follows and as shown in Figure 6-4:
= {"c".."n"}
101
Chapter 6 List Functions
= {3..9} ={3,4,5,6,7,8,9}
= {"w","z"} = {“w”,“z”}
= {"w".."z"} ={“w”,“x”,“y”,“z”}
= {"W".."Z"} ={“W”,“X”,“Y”,“Z”}
= {"#".."&"} = {“#”,“^”,“%”,“&”}
102
Chapter 6 List Functions
List.Numbers
List.Numbers is used as follows to generate a list consisting series of
numbers with specific intervals:
List.Numbers(
start as number,
count as number,
optional increment as nullable number
) as list
103
Chapter 6 List Functions
= List.Numbers(3,4) {3,4,5,6}
= List.Numbers(3,4,-1) {3,2,1,0}
= List.Numbers(3,4,6) {3,9,15,21}
List.Generate
List.Generate is used as follows to create a list (including numbers, texts,
records, or tables):
List.Generate(
initial as function,
condition as function,
next as function,
optional selector as nullable function
) as list
List.Generate takes four arguments. All are functions, and the last
argument is optional.
104
Chapter 6 List Functions
Let’s start with a simple example that represents a list of even numbers
less than 10. This function can be defined as follows:
In the first input, the first value is 0. Based on the second input, all
values in the list should be lower than 10 (generating values in the list will
continue until reaching a value greater than or equal to 10). In the third
input, in each iteration, the new value of the list is generated based on the
previous value added by 2. This input is a function that receives x as input
and results in x+2 as output, in which => is the function sign.
Based on the above example, the desired list starts with the number 0,
and in each step, 2 is added to the previous number, and this process
continues until you reach the number greater than or equal to 10.
Therefore, the result of this formula is {0,2,4,6,8}. See Figure 6-5.
105
Chapter 6 List Functions
The inputs of this function are themselves type functions, and their
explanation is shown in Table 6-4.
106
Chapter 6 List Functions
107
Chapter 6 List Functions
List.Times
List.Times is used as follows to create a list consisting of values in the
format of time:
List.Times(
start as time,
count as number,
108
Chapter 6 List Functions
step as duration
) as list
List.Times(#time(1,10,0),4,#duration(0,1,10,0)).
List.Dates
This function is used as follows to create a list including values in the
format of date:
List.Dates(
start as date,
count as number,
step as duration
) as list
109
Chapter 6 List Functions
List.DateTimes
Like the List.Times and List.Date functions, which are used to create a
list of values in the format of time or date, the List.DateTimes function is
also used to create a list, but the output list contains both date and time
values at the same time. This function can be defined as follows:
List.DateTimes(
start as datetime,
count as number,
step as duration
) as list
110
Chapter 6 List Functions
List.Random
List.Random is used as follows to create a list including random numbers
between 0 and 1:
List.Random(
count as number,
optional seed as nullable number
) as list
111
Chapter 6 List Functions
List.Reverse
List.Reverse is very simple in Power Query and can be used to reverse
the order of values in a list. This function takes an argument in the format
of list, and displays the reversed list in the output as follows:
To clarify the use of this function, some examples of this function are
provided in Table 6-8.
= List.Reverse({1..4}) {4,3,2,1}
= List.Reverse({"a",1,"b",10}) {10,“b”,1,“a”}
= List.Reverse({3,8,1}) {1,8,3}
112
Chapter 6 List Functions
List.Sort
By using List.Sort, values within a list can be sorted in ascending and
descending, alphabetically, numerically, or any other logic. This function
is defined as follows:
List.Sort(
list as list,
optional comparisonCriteria as any
) as list
=List.Sort({1,5,-2,-3}) {-3,-2,1,5}
= List.Sort({1,5,2,3},Order.Descending) {5,3,2,1}
= List.Sort({"a","x","b","c"}) {“a”,“b”,“c”,“x”}
= List.Sort({"a","x","b","c"},Order. {“x”,“c”,“b”,“a”}
Descending)
113
Chapter 6 List Functions
In this formula, you sort the values in the list by considering their
absolute values instead of their original values. Therefore, the result of the
above formula is equal to {1, -2, -3, 5}.
The following formulas can also used to sort the list without
considering the value’s signs from larger to smaller:
The output of the above formula is equal to {4, 2, 3, 1, 5}, in which all
even values come first and are followed by odd values. To sort odd and
even values in their groups from smallest to the largest, the following
formula can be used:
114
Chapter 6 List Functions
The result of the above formula is equal to {“XYA”, “ACC”, “WND”, “AXY”}.
List.First(
list as list,
optional defaultValue as any
) as any
List.Last(
list as list,
optional defaultValue as any
) as any
115
Chapter 6 List Functions
= List.First({1..10})
= List.First({ },-1)
List.FirstN)
list as list,
countOrCondition as any
) as any
List.LastN(
list as list,
optional countOrCondition as any
) as any
These functions take two inputs, where the first input is in a list format
and the second input is in a numerical format.
116
Chapter 6 List Functions
= List.FirstN({1..10},3) {1,2,3}
= List.FirstN({1..10},5) {1,2,3,4,5}
= List.FirstN({1..10},each _<5) {1,2,3,4}
= List.FirstN({5, 4,1, 2, 6, 1}, each _>3) {5.4}
= List.LastN({1..10},3) {8,9,10}
= List.LastN({1..10},5) {6,7,8,9,10}
= List.LastN({1..10},each _<5) {1,2,3,4}
= List.LastN({5, 4,1, 2, 6, 1}, each _<3) {1}
117
Chapter 6 List Functions
but the output only includes the number 1. The reason is that the values at
the end of the list are displayed in the output if the condition is met, and
as soon as a value that does not meet the condition is encountered, the
search operation stops, and the next values are not examined. Therefore, in
this example, since the number 6 (the second to last data) is greater than 3,
the data before the number 6 are not examined.
List.Range
This function is used to extract values from different parts of a list and is
defined as follows:
List.Range(
list as list,
offset as number,
optional count as nullable number
) as list
118
Chapter 6 List Functions
= List.Range({1..10},5,3) {6,7,8}
= List.Range({1..10},1,3) {2,3,4}
= List.Range({1..10},3,1) {4}
List.Max(
list as list,
optional default as any,
optional comparisonCriteria as any,
optional includeNulls as nullable logical
) as any
List.Min(
list as list,
optional default as any,
optional comparisonCriteria as any,
optional includeNulls as nullable logical
) as any
These functions take the following four inputs, where only the first
input is mandatory and the others are optional:
119
Chapter 6 List Functions
=List.Max({1..10}) 10
= List.Max({},-1) -1
= List.Max({"a".."n"},-1) “n”
= List.Max({-20..10},null,each Number.Abs(_)) -20
List.MinN(
list as list,
countOrCondition as any,
optional comparisonCriteria as any,
optional includeNulls as nullable logical
) as list
120
Chapter 6 List Functions
List.MaxN(
list as list,
countOrCondition as any,
optional comparisonCriteria as any,
optional includeNulls as nullable logical
) as list
According to the above syntax, these functions take these four inputs:
= List.MaxN({-20..10},3) {10,9,8}
= List.MaxN({"a",1,-1,"N"},3) {“a”,“N”,1}
121
Chapter 6 List Functions
List.Select
List.Select is one of the most important and powerful functions in Power
Query for extracting the values from a list based on the custom condition
by the following syntax:
List.Select(
list as list,
selection as function
) as list
This function takes two inputs, and its output is always a list.
122
Chapter 6 List Functions
List.FindText
To extract text values from a list that contains one specific character or text,
List.FindText with the following syntax can be used:
List.FindText(
list as list,
text as text
) as list
123
Chapter 6 List Functions
=List.FindText({"a".."z"},"m") {“m”}
= List.FindText({"a".."z"},"M") {}
= List.FindText({"abc","bcf","deg","z"},"b") {“Abc”,“bcf”}
List.RemoveNull
This function is used to remove null values from a list by the
following syntax:
124
Chapter 6 List Functions
This function takes only one input in the form of list, and after
removing null values in that list, the result will display in the output.
According to the above explanation, the result of the following formula
is equal to {1,2,“”,3,“null”}:
= List.RemoveNulls({1,null,2,null,"",3,"null"})
As you can see, the output of this formula also includes the value “null”
because the word null is entered in double quotes and considered as a text
value and this function does not consider this value as null.
List.Distinct
List.Distinct is used to remove duplicate values in a list and always
results in a list with unique values.
List.Distinct(
list as list,
optional equationCriteria as any
) as list
Based on the above explanation, the result of this function for different
examples is shown in Table 6-16.
125
Chapter 6 List Functions
= List.Distinct({1,3,2,1,2,5,1}) {1,3,2,5}
= List.Distinct({1,1,1,1,1,1,1}) {1}
= List.Distinct({"a","A","b","a"}) {“a”,“A”,“b”}
= List.Distinct({"a","A","b","a"},Comparer. {“a”,“b”}
OrdinalIgnoreCase)
= List.Distinct({1..10},each Number.IsOdd(_)) {1,2}
List.RemoveFirstN(
list as list,
optional countOrCondition as any
) as list
List.RemoveLastN(
list as list,
optional countOrCondition as any
) as list
126
Chapter 6 List Functions
List.RemoveRange
List.RemoveRange is used to remove values in a list by the
following syntax:
List.RemoveRange(
list as list,
index as number,
optional count as nullable number
) as list
= List.RemoveRange({1..10},3,5) {1,2,3,9}
= List.RemoveRange({1..10},4,4) {1,2,3,4,9,10}
127
Chapter 6 List Functions
List.RemoveItems
Using List.RemoveItems with the following syntax, specific values can be
removed from a list:
List.RemoveItems(
list1 as list,
list2 as list
) as list
This function takes the following two inputs in the format of a list:
=List.RemoveItems({1..10},{3..8})
List.RemoveMatchingItems(
list1 as list,
list2 as list,
optional equationCriteria as any
) as list
128
Chapter 6 List Functions
List.Skip
List.Skip is like List.RemoveFirstN and is used to remove a number of
values or values with specific conditions from the beginning of a list, as
follows:
List.Skip(
list as list,
optional countOrCondition as any
) as list
=List.Skip({1..7},4)
= List.Skip({1..7},each _<5)
List.Alternate
List.Alternate with the following syntax is used to remove and retain a series
of values in a list with a specific pattern (e.g., removing every other values):
List.Alternate(
list as list,
count as number,
129
Chapter 6 List Functions
= List.Alternate({1..10}, 1)
= List.Alternate({1..10}, 1,1)
130
Chapter 6 List Functions
= List.Alternate({1..10}, 1,2)
= List.Alternate({1..10}, 2,1)
The following formula is like the above formula, with the difference
that the pattern is executed after the second value in the list. {1,2,5,8}
= List.Alternate({1..10}, 2,1,2)
List Merging
There are multiple functions in Power Query for merging two or more lists
together and they are explained in this section. In addition to the existing
functions, lists can be merged easily by using the & operator. So {1,2} &
{1,4,3} is {1,2,1,4,3}. However, to examine more advanced list merging
scenarios, the following functions should be used.
List.Combine
This function is used to merge two or more lists together as follows:
This function takes one input, in the format of list, so all the lists (that
you want to combine) must be placed inside a reference list as the input
of this function. Then, in the output of this function, all the lists provided
inside that reference list are merged and displayed in a new list.
131
Chapter 6 List Functions
= List.Combine({{1,2},{1,4,3}})
= List.Combine({ {1,2},{1,3},{1,3}})
List.InsertRange
To add new values (a list of values) to a list in a specific position, List.
InsertRange can be used as follows:
List.InsertRange(
list as list,
index as number,
values as list
) as list
132
Chapter 6 List Functions
List.Zip
To combine the corresponding values of several lists, List.Zip can be
used as follows:
This function receives an input in the form of a list. The entered list
includes several lists and their corresponding values come to gather in
separate list in the output.
For example, entering {{1..3},{4..5}} in the first input of this function
results in {{1,4},{2,5},{3,null}}.
In a similar case, the result of the formula = List.Zip({{1..4},
{3..5},{2..7}}) is equal to the following list:
={1,3,2},{2,4,3},{3,5,4},{4,null,5},{null,null,6},
{null,null,7}}
List.Union
List.Union with the following syntax is used to combine two or more
lists, like the List.Combine function. The difference is that this function
neglects duplicate values (values repeated in all lists).
133
Chapter 6 List Functions
List.Union(
lists as list,
optional equationCriteria as any
) as list
= List.Union({{1,2},{1,4,3}}) {1,2,4,3}
= List.Union({{1,2,1},{1,4,3}}) {1,2,1,4,3}
= List.Union({{1,2},{1,3},{1,3}}) {1,2,3}
= List.Union({{1,2,3,2},{1,3,2,4}}) {1,2,3,2,4}
= List.Union({{1,2},{1,3},{1,4,1}}) {1,2,1,3,4}
List.Intersect
List.Intersect with the following syntax is used to extract common
values in two or more lists:
List.Intersect(
lists as list,
optional equationCriteria as any
) as list
134
Chapter 6 List Functions
The output includes values that have been repeated in all the input
lists. This function takes two arguments.
= List.Intersect({{1..4},{2..5}}) {2,3,4}
= List.Intersect({{1..4},{2..5},{3..6}}) {3,4}
= List.Intersect({{1,2},{1,3},{1,4,1}}) {1}
List.Difference
List.Difference is used to calculate the difference between two lists and
always results in a list of values that exists in the first list but not in the
second list.
List.Difference(
list1 as list,
list2 as list,
optional equationCriteria as any
) as list
135
Chapter 6 List Functions
Based on this explanation, the result of this function for different input
cases is as shown in Table 6-21.
= List.Difference({1..10},{3..12}) {1,2}
= List.Difference({3..12},{1..10}) {11,12}
= List.Difference({3..12},{1..30}) {}
=List.Count({1..5})
There are other functions that are divided into the following groups.
Functions in the first group are listed in Table 6-22 and all receive a list
as input.
136
Chapter 6 List Functions
List.Median(
list as list,
optional comparisonCriteria as any
) as any
137
Chapter 6 List Functions
List.Mode(
list as list,
optional equationCriteria as any
) as any
List.Percentile(
list as list,
percentiles as any,
optional options as nullable record
) as any
This function receives three inputs in this order: the reference list, the
desired percentile (which is entered as a decimal number such as 0.25),
and precision.
Based on the above explanation, Table 6-24 provides multiple
examples of the results of this function.
138
Chapter 6 List Functions
List.Count({1..10,4}) 10
List.NonNullCount({1..10,4}) 10
List.StandardDeviation({1..10,4}) 2.788867
List.Sum({1..10,4}) 50
List.Product({1..10,4}) 1612800
List.Average({1..10,4}) 5
List.Mode({1..10,4}) 4
List.Median.({1..10,4}) 4.5
List.Percentile({1..10,4},0.25) 3.25
={1,2,3}={1,3,2}
In checking the equality of two lists, the result will be True only if the
values in the both list are same (in terms of value and format) and also in the
same order. Since in the above formula, the numbers 3 and 2 in the second
list have been swapped compared to the first list, the result of the above
formula will be False, but the result of the following formula will be True:
={1,2,3}={1,2,3}
139
Chapter 6 List Functions
In the following formulas, checking the existence of a value within the list
under consideration is discussed.
List.IsEmpty
To check a list is empty or includes at least a value, List.IsEmpty can be
used as below:
=List.Empthy({ })
List.IsDistinct
To check for duplicate values within a list, the List.IsDistinct function can
be used as follows:
List.IsDistinct(
list as list,
optional equationCriteria as any
) as logical
= List.IsDistinct({1..10})
= List.IsDistinct({1..10,1})
List.Contains
Using List.Contains, the existence of a specific value (just one value) in a
reference list can be checked. This function is defined as follows:
List.Contains(
list as list,
value as any,
optional equationCriteria as any
) as logical
141
Chapter 6 List Functions
The output of this function is True if the reference list contains the
value registered in the second input and False in other cases. Therefore,
the result of the following formula is True:
= List.Contains({1..10},3)
= List.Contains({1..10},11)
List.ContainsAll
To check the existence of two or more values within a list, List.
ContainsAll can be used as follows:
List.ContainsAll(
list as list,
values as list,
optional equationCriteria as any
) as logical
The result of this function is True if the reference list contains all
the values in the second input; in other cases, the result of this function
is False.
Some simple examples of this function are provided in Table 6-25.
142
Chapter 6 List Functions
= List.ContainsAll({1..10},{1,3,7}) True
= List.ContainsAll({1..10},{1,3,11}) False
List.ContainsAny
List.ContainsAny is like List.ContainsAll, with the difference that the
result of List.ContainsAny is true if the reference list contains at least one
value in the second input.
The List.ContainsAny is defined as follows:
List.ContainsAny(
list as list,
values as list,
optional equationCriteria as any
) as logical
143
Chapter 6 List Functions
= List.ContainsAny({1..10},{1,3,7}) True
= List.ContainsAny({1..10},{1,3,11}) True
= List.ContainsAny({1..10},{12,13,11}) False
List.AllTrue
This function receives a list as input, and if all the values in the reference
list are equal to True, the result of this function is True; otherwise, the
result of this function is False. It is defined as follows:
It should be noted that the list entered in this function must contain
True or False values, and if it includes a number or text, the result of this
function is an error.
List.AnyTrue
This function is defined as follows:
This function receives a list as input, and if at least one of the values in
that list is equal to True, the result of this function is True; otherwise, the
result of this function is False.
Positions in a List
The functions related to extracting the position of values in a list are
explained in this section.
144
Chapter 6 List Functions
List.Positions
The List.Positions function is defined very simply in Power Query as
follows:
This function takes a list as input and its output is equal to a list
explaining the position of values. The output of this function is only
dependent on the number of values in the reference list and is not
dependent on their value or type. In other words, the result of this function
is always equal to {0,1} for lists with two values and {0,1,2} for lists with
three values.
Therefore, the result of the following formula is equal to {0,1,2}:
=List.Positions({1,10,-1})
List.PositionOf
To extract the position of a value, List.PositionOf can be used as follows:
List.PositionOf(
list as list,
value as any,
optional occurrence as nullable number,
optional equationCriteria as any
) as any
145
Chapter 6 List Functions
= List.PositionOf({1..10},5) 4
= List.PositionOf({4,5,3,5,2},5) 1
= List.PositionOf({4,5,3,5,2},5,Occurrence.First ) 1
= List.PositionOf({4,5,3,5,2},5,Occurrence.Last ) 3
= List.PositionOf({4,5,3,5,2},5,Occurrence.All ) {1,3}
= List.PositionOf({"a","b","A","A"},"A") 2
146
Chapter 6 List Functions
List.PositionOfAny
List.PositionOfAny is like List.Position, but with the difference that in
this function, it is possible to search for two or more values simultaneously
within a reference list and extract their positions. This function is defined
as follows:
List.PositionOfAny(
list as list,
values as list,
optional occurrence as nullable number,
optional equationCriteria as any
) as any
147
Chapter 6 List Functions
= List.PositionOfAny({1..10},5) 4
= List.PositionOfAny({4,5,3,5,2},5) 1
= List.PositionOfAny({4,5,3,5,2},{5,3},Occurrence. 1
First )
= List.PositionOfAny({4,5,3,5,2},{3},Occurrence.Last ) 3
= List.PositionOfAny({4,5,3,5,2},{5,3},Occurrence.All ) {1,2,3}
= List.PositionOfAny({"a","b","A","A"},{“a","A"}) 0
List.ReplaceValue
Using List.ReplaceValue means a value in a list can be replaced with a
new value as follows:
List.ReplaceValue(
list as list,
oldValue as any,
newValue as any,
replacer as function
) as list
148
Chapter 6 List Functions
= List.ReplaceValue({1..4},4,3,Replacer. {1,2,3,3}
ReplaceValue )
= List.ReplaceValue({1..4},4,3,Replacer. {1,2,3,4}
ReplaceText )
= List.ReplaceValue({"a","b","B"},"b","B", {“a”,“B”,“B”}
Replacer.ReplaceText )
List.ReplaceMatchingItems
List.ReplaceMatchingItems is like List.ReplaceItems, but List.
ReplaceItems can only replace one value of the list with a new value,
149
Chapter 6 List Functions
List.ReplaceMatchingItems(
list as list,
replacements as list,
optional equationCriteria as any
) as list
= List.ReplaceMatchingItems({1..5},{{1,5}}) {5,2,3,4,5}
= List.ReplaceMatchingItems({1..5},{{1,5},{2,6}}) {5,6,3,4,5}
150
Chapter 6 List Functions
List.ReplaceRange
To replace a part of a list based on its position (for example, from the 5th to
the 12th values) by new values, List.ReplaceRange can be used as follows:
List.ReplaceRange(
list as list,
index as number,
count as number,
replaceWith as list
) as list
= List.ReplaceRange({1..10},3,6,{5}) {1,2,3,5,10}
= List.ReplaceRange({1..10},0,6,{5}) {5,7,8,9,10}
= List.ReplaceRange({1..10},1,8,{5,6}) {1,5,6,10}
151
Chapter 6 List Functions
List.Transform
List.Transform can be used to change the values in a list by specific logic
or operation. For example, with this function, all values can be added
to the number 1, or multiplied by 2, English letters can be converted to
uppercase or lowercase, or extra spaces can be removed. This function is
defined as follows:
List.Transform(
list as list,
transform as function
) as list
There are different ways to define the second input, but in the simplest
ones, you can enter the word each at the beginning of this input. Then the
“_” determines the values in the list, so each _+2 can be used to add 2 in
all the values in the list or each Number.Abs(_) can be used to remove the
values sign.
In another way, instead of mentioning each and then using “_” for
values in the list, a specific variable like x can be used in the parenthesis
and then the operation is determined after =>. So (x) => x+1 can be used
in the second input to add 1 to all values in the list.
With the above explanation, various examples of this function are
provided in Table 6-32.
152
Chapter 6 List Functions
= List.Transform({1..5},each _+ 2) {3,4,5,6,7}
= List.Transform({1..5},each Number.Factorial(_)) {1,2,6,24,120}
= List.Transform({1..5},each Number. {3,7,20,55,148}
Round(Number.Exp(_)))
= List.Transform({"abc","DEF","mNg"}, {“Abc”,“Def”,“Mng”}
each Text.Proper(_))
List.TransformMany
List.TransformMany can be used to change or transform values in a list by
specific logic as follows:
List.TransformMany(
list as list,
collectionTransform as function,
resultTransform as function
) as list
153
Chapter 6 List Functions
In the following formula, the reference list (x) is {1,2,3}, and based
on the function defined in the second input, the second list (y) is {2,4,6}.
Considering these two lists, the result of the formula is {“1-2”, “2-4”, “3-6”}.
= List.TransformMany(
{1,2,3},
(x) => {2*x},
(x,y) => (Text.From(x) & "-" & Text.From(y))
)
Other Functions
In addition to the above-mentioned functions, there are other functions in
Power Query for modifying a list.
List.Single
This function receives an input in the form of a list. If the list has only one
value, it displays that value as output. Otherwise, the result of this function
is an error. This function is defined as follows:
List.SingleOrDefault
This function is like the List.Single function, but in this function, it is
possible to specify a value to be displayed in the output instead of an error.
This function is defined in Power Query as follows:
154
Chapter 6 List Functions
List.SingleOrDefault(
list as list,
optional default as any
) as any
List.Repeat
Using List.Repeat, it is possible to repeat the values of a list in specific
numbers as follows:
List.Repeat(
list as list,
count as number
) as list
155
Chapter 6 List Functions
=List.Repeat({1,2},2) {1,2,1,2}
= List.Repeat({1..3},2) {1,2,3,1,2,3}
= List.Repeat({1,2},3) {1,2,1,2,1,2}
List.Split
By using this function with the following syntax, a large list can be
converted into several smaller lists:
List.Split(
list as list,
pageSize as number
) as list
156
Chapter 6 List Functions
= List.Split({1..5},2) {{1,2},{3,4},{5}}
= List.Split({1..5},3) {{1,2,3},{4,5}}
= List.Split({1..5},4) {{1,2,3,4},{5}}
Summary
In this chapter, you embarked on an exploration of the versatile and
dynamic world of list functions within Power Query. Lists, as collections
of data points, play a pivotal role in understanding data relationships and
dependencies, and mastering their manipulation opens doors to intricate
data transformations.
Your journey commenced by understanding the fundamental nature
of lists, their ordered structure, and their ability to hold heterogeneous
data types. You learned how to manually create lists and generate them
dynamically through calculated expressions, establishing a foundation for
advanced list manipulation.
As you delved deeper, you discovered the power of list functions
that Power Query offers. These functions enable you to reshape data
collections, from filtering and sorting elements to merging lists to create
cohesive data structures. This versatility allows you to address complex
data manipulation challenges with precision.
Furthermore, you explored the realm of nested lists, where lists within
lists create hierarchies that mirror real-world relationships. This skill
is invaluable when dealing with multidimensional data structures and
navigating their complexities.
At the heart of your journey is the concept of aggregation and
summarization. You uncovered the potential of list functions to distill
157
Chapter 6 List Functions
158
CHAPTER 7
Records
In the symphony of data manipulation, individual pieces of information
often harmonize to create a more comprehensive melody. Enter records,
the versatile and dynamic data structures that allow us to encapsulate and
comprehend the interplay between various attributes of an entity. This
chapter delves into the world of record functions within Power Query,
unraveling the art of transforming raw data into meaningful insights by
harnessing the power of structured relationships.
Your journey through this chapter commences with a foundational
exploration of records: their anatomy, their attributes, and their capacity to
hold a heterogeneous blend of data types. Armed with this understanding,
you'll dive into the realm of record creation, discovering how to craft these
dynamic containers both manually and through calculated expressions.
Defining a Record
Records are a set of values, each with a specific title, and only one value
can be entered for each title. For simplicity, a record can be considered
equivalent to a row in a table. To define a record, it needs to first mention
the title (like headers in tables) and then show an equal sign and its value
between [ ] as follows and as in Figure 7-1:
=[A=123,B=81,C=“XYZ”]
= [A=5,B=A+3,C=A+B]
= [A=5,B={1..3},C=[A=1,B=3],D={2}]
160
Chapter 7 Records
= [A=5,B=3]&[A=2,C=4]
161
Chapter 7 Records
Record Properties
To extract the number of fields plus their names and values in a record, the
below function can be used.
Record.FieldCount
To count the number of fields in a record, Record.FieldCount is used as
follows:
The result of this function is always a number that displays the number
of fields in the input record. Therefore, the result of the following formula
is equal to 1:
= Record.FieldCount([A=5])
= Record.FieldCount([A=5,b=2])
= Record.FieldCount([A=5,B={1..3},C=[A=1,B=3],D={2}])
162
Chapter 7 Records
Record.FieldNames
Record.FieldNames is used to extract field titles in records as follows:
This function receives a record as input and returns the names of its
fields as a list in the output. Therefore, the result of the following formula is
{“A”, “B”}:
= Record.FieldNames([A=5,B=2])
= Record.FieldNames([Name="Omid",Family="Motamedi"])
Record.FieldValues
Record.FieldValues is like Record.FieldNames, but it results in the values
entered in the reference record instead of their title. It is defined as follows:
This function receives a record as input, and its result is equal to a list
of values. Therefore, the result of the following formula is equal to {5,2}:
= Record.FieldValues([A=5,B=2])
= Record.FieldValues([Name="Omid",Family="Motamedi"])
163
Chapter 7 Records
Record.ToList
Record.ToList is used to convert a record to a list as follows. This function
receives an input in the type of record and displays the values of its fields
as a list in the output.
= Record.ToList([A=5,B=2,C=10]) {5,2,10}
= Record.ToList([A=5,B={1..3},C=10]) {5,{1,2,3},10}
Record.Field
Record.Field is used to extract the value of a field in a record as follows:
Record.Field(
record as record,
field as text
) as any
= Record.Field([A=5,B=2],"A")
164
Chapter 7 Records
= Record.Field([Name="Omid",Family="Motamedi"],"Name")
But since the defined record in the following formula does not include
field C, the result of this formula is an error, as shown in Figure 7-5:
= Record.Field([A=5,B=2],"C")
Record.FieldOrDefault
This function is defined in Power Query as follows:
Record.FieldOrDefault(
record as nullable record,
field as text,
optional defaultValue as any
= Record.FieldOrDefault([A=5,B=2],"C")
165
Chapter 7 Records
= Record.FieldOrDefault([A=5,B=2],"C",-1)
Record.HasFields
To check if a record contains specific fields, Record.HasFields can be used
as follows:
Record.HasFields(
record as record,
fields as any
) as logical
This function receives two below inputs, and its output is always True
if the reference record contains all the defined fields in the second input or
False if the defined record does not contain at least one of the field names
entered in the second input.
= Record.HasFields([A=5,B=2,C={1..4}],"C") True
= Record.HasFields([A=5,B=2,C={1..4}],"D") False
= Record.HasFields([A=5,B=2,C={1..4}],{"A","C"}) True
= Record.HasFields([A=5,B=2,C={1..4}],{"A","D"}) False
166
Chapter 7 Records
Record Correction
In this section, the functions for modifying a record will be presented.
Record.AddField
To add a field to a reference record, Record.AddField with the following
syntax can be used:
Record.AddField(
record as record,
fieldName as text,
value as any,
optional delayed as nullable logical
) as record
= Record.AddField([A=5,B=2],"C",10)
In this case, the result is a record containing all fields (A, B, and C) as
[A=5,B=2,C=10].
167
Chapter 7 Records
Record.Combine
To add more than one field to a record at the same time or to combine
several records together, Record.Combine can be used as follows:
= Record.Combine({[A=5,B=2],[C=10]})
= Record.Combine({[A=5,B=2],[C=10,D=
2*C,E=1]})
Record.SelectFields
Record.SelectFields is used to extract a subset of a reference record as
follows:
Record.SelectFields(
record as record,
fields as any,
optional missingField as nullable number
) as record
168
Chapter 7 Records
Record.SelectFields([A=5,B=2,C=10,D=12],{"A","C"})
Record.SelectFields([A=5,B=2,C=10,D=12],{"A","C","N"})
169
Chapter 7 Records
However, entering the number 1 in the third input, the result of the
following will be as shown in Figure 7-7:
Record.SelectFields([A=5,B=2,C=10,D=12],{"A","C","N"},1)
And entering the number 2 in the third input, the result of the
following will be as shown in Figure 7-8:
Record.SelectFields([A=5,B=2,C=10,D=12],{"A","C","N"},2)
Record.RemoveFields
To remove one or more fields from a record, Record.RemoveFields is used
as follows:
Record.RemoveFields(
record as record,
fields as any,
optional missingField as nullable number
) as record
170
Chapter 7 Records
= Record.RemoveFields([A=5,B=2,C=10,D=2*C,E=1],"A")
= Record.RemoveFields([A=5,B=2,C=10,D=2*C,E=1],
{"A","B","C","X"})
= Record.RemoveFields([A=5,B=2,C=10,D=2*C,E=1],
{"A","B","C","X"},1)
171
Chapter 7 Records
Record.RenameFields
Record.RenameFields can be used to change the field names in a record as
follows:
Record.RenameFields(
record as record,
renames as list,
optional missingField as nullable number
) as record
172
Chapter 7 Records
= Record.RenameFields([A=5,B=2,C=10],{"A","X"}) [X=5,B=2,C=10]
= Record.RenameFields([A=5,B=2,C=10],{{"A","X"}, [X=5,W=2,C=10]
{"B","W"}})
= Record.RenameFields([A=5,B=2,C=10],{"U","X"}) Error
= Record.RenameFields([A=5,B=2,C=10],{"U","X"},1) [A=5,B=2,C=10]
Record.ReorderFields
To change the order of fields in a list, Record.ReorderFields can be used
as follows:
Record.ReorderFields(
record as record,
fieldOrder as list,
optional missingField as nullable number
) as record
173
Chapter 7 Records
= R ecord.ReorderFields([A=5,B=2,C=10],{"C","B", [C=10,B=2,A=5]
"A"})
= R ecord.ReorderFields([A=5,B=2,C=10],{"X","B", Error
"A"})
= R ecord.ReorderFields([A=5,B=2,C=10],{"X","B", [B=2,C=10,A=5]
"A"},1)
Record.TransformFields
With Record.TransformFields, the format of values in a record can be
changed as follows:
Record.TransformFields(
record as record,
transformOperations as list,
optional missingField as nullable number
) as record
174
Chapter 7 Records
Record Conversion
Here are several functions in Power Query for converting a record to a list
or table.
Record.FromList
As the name of this function suggests, it is used to convert a list to a record
as follows:
Record.FromList(
list as list,
fields as any
) as record
175
Chapter 7 Records
This function takes two inputs as follows, and its output is always equal
to a record.
= Record.FromList({1,2,3},{"A","B","C"}) [A=1,B=2,C=3]
= Record.FromList({1,"XWZ",3},{"A","B","C"}) [A=1,B="XWZ",C=3]
= Record.FromList({1,2,3},type [A=1,B=2,C=3]
[A=number,B=number,C=number])
Record.FromTable
Using Record.FromTable, tables with specific conditions can be converted
to records as follows:
As you can see, this function receives an input in the type of table and
converts it to the record. To use this function, the input table must include
two columns. The first column contains values in the format of text. Then,
in the output record, the values of the first column of the reference table
are considered as the field names, and the values of the second column in
the table are considered as the values of the output.
176
Chapter 7 Records
Record.ToTable
By using Record.ToTable, a record can be transformed into a table as
follows:
This function receives an input of the record type and transforms the
same record into a two-column table according to the following format.
The first column of the table includes the names of record fields, and the
second column includes the values of the fields. See Figure 7-9.
Summary
This chapter was a comprehensive exploration of record functions
within Power Query, offering a deep dive into the art of transforming
raw data into meaningful insights by harnessing the power of structured
relationships.
The journey began with a foundational understanding of records as
dynamic data structures. You learned about their ability to encapsulate
various attributes of an entity, akin to creating multidimensional canvases
for data representation. Through this, you gained insight into the manual
creation of records and generating them through calculated expressions.
The true essence of record functions emerged as you unraveled their
capabilities in data transformation and enrichment. You explored a diverse
177
Chapter 7 Records
178
CHAPTER 8
Tables
In the sprawling landscape of data exploration and analysis, tables stand as
the cornerstone of structure and coherence. They organize chaos, provide
context, and facilitate insights by presenting data in a logical and digestible
format. Welcome to a chapter that delves into the intricate world of table
functions within Power Query, guiding you through the art of transforming
raw information into organized, actionable knowledge.
Tables are the canvases upon which data stories unfold. Imagine
customer orders neatly categorized, project tasks meticulously tracked, or
survey responses collated for analysis. In a world of disparate data points,
tables are the glue that holds information together, facilitating seamless
exploration and understanding.
Your journey through this chapter begins with a fundamental
understanding of tables: their anatomy, their columns, and their rows of
data. You’ll explore how Power Query views tables as dynamic structures
capable of housing diverse data types and relationships. Armed with this
knowledge, you’ll embark on a journey to create tables manually and
through calculated expressions.
However, tables are not static entities; they are pathways to
transformation and enlightenment. Discover the myriad table functions
that Power Query provides, enabling you to filter, sort, reshape, and
aggregate data with precision. From column removal to row filtering, these
functions empower you to craft tables that align with your analytical goals.
C
reating a Table
There are various methods in Power Query to create or call tables from
different data sources. This section covers creating a table from a record,
list, rows, or columns using the following functions.
T able.FromColumns
As the name of this function suggests, Table.FromColumns is used to create
a table from a set of columns, as follows:
Table.FromColumns(
lists as list,
optional columns as any
) as table
180
Chapter 8 Tables
= Table.FromColumns(
{
{"Omid","Ali"},
{"Motamedi","Amjad"},
{31,35}
},
{"Name","Family","Age"})
Omid Motamedi 31
Ali Amjad 35
= Table.FromColumns(
{
{"Omid"},
{null,"Amjad"},
{"",35}
},
{"Name","Family","Age"})
181
Chapter 8 Tables
Omid null
null Amjad 35
= Table.FromColumns({{"Omid","Ali"},{"Motamedi","Amjad"},
{31,35}})
Omid Motamedi 31
Ali Amjad 35
T able.FromRows
The Table.FromRows function is like the Table.FromColumns function.
The difference is that in the first input of this function, instead of defining
the columns’ values of the table, the values corresponding to the rows are
defined as follows:
Table.FromRows(
rows as list,
optional columns as any
) as table
182
Chapter 8 Tables
= Table.FromRows(
{
{"Omid","Motamedi",31},
{"Ali","Amjad",35}
},
{"Name","Family","age"}
)
Omid Motamedi 31
Ali Amjad 35
183
Chapter 8 Tables
And if the second input is not entered, the result will be as shown in
Table 8-5.
= Table.FromRows(
{
{"Omid","Motamedi",31},
{"Ali","Amjad",35}
})
Omid Motamedi 31
Ali Amjad 35
In a similar situation, if some values are not available, you can replace
them with the null expression or leave them empty, as shown here and in
Table 8-6:
= Table.FromRows({{"Omid","",""},{null,"Amjad",35}},{"Name",
"Family","age"})
Omid
null Amjad 35
184
Chapter 8 Tables
T able.FromRecords
Table.FromRecords is used to create a table from a set of records. In this
function, each record presents a row of the table and the records must be
entered into a list as the first input of this function. The fields of the records
can be the same or different, but in the second input of this function, you
specify which fields should be displayed in the output. In the third input of
this function, you specify how to calculate the values of a field if no value is
entered. This function is defined as follows:
Table.FromRecords(
records as list,
optional columns as any,
optional missingField as nullable number
) as table
185
Chapter 8 Tables
= Table.FromRecords(
{[Name="Omid",Family="Motamedi",Age=31],
[Name="Ali",Family="Amjad",Age=35]}
)
Omid Motamedi 31
Ali Amjad 35
And by specifying some column names like Name and Age in the
second input, the result will change, as shown in Table 8-8.
= Table.FromRecords(
{[Name="Omid",Family="Motamedi",Age=31],
[Name="Ali",Family="Amjad",Age=35]},
{"Name","Age"})
Omid 31
Ali 35
186
Chapter 8 Tables
If the second record does not include the Name field, the result of the
following formula will look like Table 8-9:
= Table.FromRecords(
{[Name="Omid",Family="Motamedi",Age=31],
[Family="Amjad",Age=35]},
{"Name","Age"})
Omid 31
Error Error
In this case, by entering the number 2 in the third input of the function,
the result will be as shown in Table 8-10.
Omid 31
null 35
T able.FromValue
Using Table.FromValue, values with different formats can be converted to
a table as follows:
Table.FromValue(
value as any,
optional options as nullable record
) as table
187
Chapter 8 Tables
As you can see, this function receives the following two inputs:
A
2
3
a 1
b 2
c 3
188
Chapter 8 Tables
= Table.FromValue(2)
T able Conversion
Using Power Query functions, a table can be converted into a list, record,
columns, or rows. In this section, these kinds of functions are examined.
For simplicity, Table 8-14 is considered as TableA and is used in the several
example.
Omid Motamedi 31
Ali Amjad 35
189
Chapter 8 Tables
T able.ToRecords
Using Table.ToRecords with the following syntax, a table can be converted
to a set of records:
This function takes an input of type table, and its output is a list where
each of its values represents a record that corresponds to a row of the
original table values.
With these explanations, the result of the formula Table.
ToRecords(TableB) is equal to the following list:
{[Name="Omid",Family="Motamedi",Age=31],
[Name="Ali",Family="Amjad",Age=35]}
{[Product="W11",Price=100,Material="Metal"],
[Product="S06",Price=75,Material="Wood"],
[Product="R08",Price=125,Material="Glass"]}
T able.ToColumns
The Table.ToColumns function with the following syntax can be used to
convert the table’s column values into lists:
190
Chapter 8 Tables
{{ "Omid","Ali"},
{ "Motamedi","Amjad"},
{31, 35}}
T able.ToRows
Using Table.ToRows, a table can be converted to a set of rows where each
row is displayed within a list. In other words, this function receives an
input in the format of a table and its output is equal to a list where each of
its values represents a list that is equivalent to a row of the original table.
The syntax of this function is as follows:
{{ "Omid","Motamedi", 31},
{ "Ali","Amjad", 35}}
191
Chapter 8 Tables
T able.ToList
Table.ToList is used to convert a table to a list as follows:
Table.ToList(
table as table,
optional combiner as nullable function
) as list
Combiner.CombineTextByDelimiter
Combiner.CombineTextByEachDelimiter
Combiner.CombineTextByLengths
Combiner.CombineTextByPositions
Combiner.CombineTextByRanges
An important point about this function is that before using it, the
format of all columns of the reference table must be changed to text.
By considering TableA as shown in Table 8-13, and assuming that
the column format in the Price column is defined as text, the result of
formula = Table.ToList(TableA) is equal to the list {"W11,100,Metal",
"S06,75,Wood", "R08,125,Glass"}.
In addition, the following formula states that the first, second, and
third columns of TableA should be combined together by taking 1, 2, and
2 first characters of each column, respectively. Therefore, the result is
{"W10Me", "S75Wo", "R12Gl"}:
= Table.ToList(TableA, Combiner.CombineTextByLengths({1,2,2}))
192
Chapter 8 Tables
= Table.ToList(TableA, Combiner.CombineTextByDelimiter("-"))
= Table.ToList(TableA, Combiner.
CombineTextByEachDelimiter({".","_"}))
So the result is
T able.Column
Using Table.Column with the following syntax, the values of a column can
be extracted as a list:
Table.Column(
table as table,
column as text
) as list
193
Chapter 8 Tables
The result of this function is a list that includes the values of the
specified column in the second input. Therefore, the result of Table.
Column(TableC, "Quantity") is equal to this list:
{17,18,10,1,6,9,6,6,10,3,18}
T able.TransformRows
With the function Table.TransformRows, you can transform values in the
rows of a table into a list as follows:
Table.TransformRows(
table as table,
transform as function
) as list
By this function, the values of the table are converted into a list based
on the transformation function defined in the second input. For example,
the result of the following formula is equal to the list {100, 75, 125}:
194
Chapter 8 Tables
In the following example, the first character of the product name and
the last character of the product material should be displayed next to each
other in the output list. The result is {"Wl", "Sd", "Rs"}:
T able.Split
Table.Split is used to split a table into multiple tables. For example, with
this function a table with 100 rows can be split into 50 tables with 2 rows.
Table.Split(
table as tle,
pageSize as number
) as list
195
Chapter 8 Tables
T able.SplitAt
Table.SplitAt is used to divide a table into two sub-tables based on a
criteria.
Table.SplitAt(
table as table,
count as number
) as list
196
Chapter 8 Tables
Information
There are several functions in Power Query that provide information such
as the number of columns and rows in a table. These functions receive an
input in the form of a table and provide information about that table in the
output. Table 8-16 lists of these functions along with their applications.
197
Chapter 8 Tables
Considering Table 8-14 named TableA and Table 8-15 named TableB,
Table 8-17 shows the results of using various formulas.
=Table.ColumnCount(TableB) 3
=Table.IsEmpty (TableB) False
=Table.RowCount(TableB) 2
=Table.ColumnCount(TableA) 3
=Table.IsEmpty (TableA) False
=Table.RowCount(TableA) 3
198
Table 8-18. Result of Table.Profile
Column Min Max Average StandardDeviation Count NullCount DistinctCount
Age 31 35 33 2.828427125 2 0 2
Family Amjad Motamedi null null 2 0 2
Name Ali Omid null null 2 0 2
Chapter 8
199
Tables
Chapter 8 Tables
= Table.FromColumns(
{{1,2,3,4,5,6,7,8,9,10,11},
{#date(2023,1,30),#date(2023,1,31),#date(2023,2,1),
#date(2023,2,1),#date(2023,2,1),#date(2023,2,2),#date
(2023,2,2),#date(2023,2,3),#date(2023,2,4),#date
(2023,2,4),#date(2023,2,6)},
{"A","A","C","A","B","B","A","C","B","A","C"},
{17,18,10,1,6,9,6,6,10,3,18}},
{"Row","Date","Product Name","Quantity"})
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
200
Chapter 8 Tables
T able.FirstValue
The result of Table.FirstValue is always equal to the value entered in
the first row and first column of a table. Table.FirstValue is defined as
follows in Power Query:
Table.FirstValue(
table as table,
optional default as any
) as any
Table.First(
table as table,
optional default as any
) as any
Table.Last(
table as table,
optional default as any
) as any
201
Chapter 8 Tables
The result of these functions is always a record that displays the values
of the first or last row of the reference table. Therefore, the result of the
formula Table.First(TableC) is as follows:
[Row=1,Date="1/30/2023",Product Name=A,Quantity=17]
[Row=11,Date="2/6/2023",Product Name=C,Quantity=18]
Table.FirstN(
table as table,
countOrCondition as any
) as table
Table.LastN(
table as table,
countOrCondition as any
) as table
202
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
203
Chapter 8 Tables
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
11 2/6/2023 C 18
Table.Max(
table as table,
comparisonCriteria as any,
optional default as any
) as any
204
Chapter 8 Tables
Table.Min(
table as table,
comparisonCriteria as any,
optional default as any
) as any
205
Chapter 8 Tables
= Table.Max(TableC, "Quantity")
The result is shown in Figure 8-2. (Although there is a row with the sale
value of 18, the result of this function is only one of these rows.)
Table.MaxN(
table as table,
comparisonCriteria as any,
countOrCondition as any
) as table
Table.MinN(
table as table,
comparisonCriteria as any,
countOrCondition as any
) as table
206
Chapter 8 Tables
11 2/6/2023 C 18
2 1/31/2023 A 18
1 1/30/2023 A 17
4 2/1/2023 A 1
10 2/4/2023 A 3
8 2/3/2023 C 6
207
Chapter 8 Tables
T able.Range
To extract n rows from the middle rows of a table, Table.Range can be used
as follows:
Table.Range(
table as table,
offset as number,
optional count as nullable number
) as table
208
Chapter 8 Tables
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
T able.SelectRows
Table.SelectRows with the following syntax allows you to filter some rows
of a table based on the condition:
Table.SelectRows(
table as table,
condition as function
) as table
Table.SelectRows takes two inputs, and its result is always a table with
filtered rows from the reference table.
1. In the first input of this function, the reference table
is entered.
209
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
4 2/1/2023 A 1
7 2/2/2023 A 6
10 2/4/2023 A 3
= Table.SelectRows(TableC,each [Quantity]>10)
1 1/30/2023 A 17
2 1/31/2023 A 18
11 2/6/2023 C 18
210
Chapter 8 Tables
To execute more than one condition at the same time, the conditions
are connected by using “and” or “or” between them, as follows:
This means that only the rows related to the sales of product A with
more than 10 items on the quantity column will be displayed in the output.
(Both conditions must be met simultaneously.) See Table 8-29.
1 1/30/2023 A 17
2 1/31/2023 A 18
In another example, replacing “and” by “or” means that all rows where
product A has been sold should be displayed alongside all rows where
more than 10 units of the product have been sold (at least one of the
conditions must be met). See Table 8-30.
1 1/30/2023 A 17
2 1/31/2023 A 18
4 2/1/2023 A 1
7 2/2/2023 A 6
10 2/4/2023 A 3
11 2/6/2023 C 18
211
Chapter 8 Tables
T able.SelectRowsWithErrors
Table.SelectRowsWithErrors is used to find rows in a table with error
values in at least one column. This function is defined in Power Query as
follows:
Table.SelectRows(
table as table,
condition as function
) as table
212
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C Error
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C Error
9 2/4/2023 Error 10
10 2/4/2023 A 3
11 2/6/2023 C Error
3 2/1/2023 C Error
8 2/3/2023 C Error
9 2/4/2023 Error 10
11 2/6/2023 C Error
213
Chapter 8 Tables
9 2/4/2023 Error 10
Table.RemoveFirstN(
table as table,
optional countOrCondition as any
) as table
Table.RemoveLastN(
table as table,
optional countOrCondition as any
) as table
214
Chapter 8 Tables
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
215
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
216
Chapter 8 Tables
T able.Distinct
To remove duplicate rows in a table, Table.Distinct can be used as
follows:
Table.Distinct(
table as table,
optional equationCriteria as any
) as table
This function receives two inputs, and if there are duplicate rows based
on the conditions defined in the second input, all duplicate rows except
the first one are removed from the reference table.
1. The first input is the reference table.
= Table.Distinct(TableC,"Product Name")
Therefore, regardless of the values entered in other columns, only the
first row for each product will be displayed in the output, and other rows
will be removed. The result of the above formula is shown in Table 8-38.
217
Chapter 8 Tables
1 1/30/2023 A 17
3 2/1/2023 C 10
5 2/1/2023 B 6
= Table.Distinct(TableC,"Date")
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
6 2/2/2023 B 9
8 2/3/2023 C 6
9 2/4/2023 B 10
11 2/6/2023 C 18
= Table.Distinct(TableC,"Date")
218
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
T able.Skip
Table.Skip is very like Table.RemoveFirstN and is used to remove a
number of rows from the beginning of a table as follows:
Table.Skip(
table as table,
optional countOrCondition as any
) as table
219
Chapter 8 Tables
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
220
Chapter 8 Tables
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
T able.RemoveRows
To delete n rows from the middle of a table, Table.RemoveRows can be used
as follows:
Table.RemoveRows(
table as table,
offset as number,
optional count as nullable number
) as table
221
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
T able.AlternateRows
Using Table.AlternateRows, a certain number of rows in a table can be
removed based on a specific pattern, as follows:
Table.AlternateRows(
table as table,
offset as number,
skip as number,
take as number
) as table
The result of this function is always a table that includes several rows
from the reference table. This function receives four inputs.
222
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
6 2/2/2023 B 9
9 2/4/2023 B 10
223
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
6 2/2/2023 B 9
7 2/2/2023 A 6
10 2/4/2023 A 3
11 2/6/2023 C 18
T able.RemoveRowsWithErrors
Table.RemoveRowsWithErrors is used to remove rows that have an error
value in one of their columns, as follows:
Table.RemoveRowsWithErrors(
table as table,
optional columns as nullable list
) as table
224
Chapter 8 Tables
T able.RemoveMatchingRows
To remove rows with specific conditions from a table, Table.
RemoveMatchingRows can be used as follows:
Table.RemoveMatchingRows(
table as table,
rows as list,
optional equationCriteria as any
) as table
225
Chapter 8 Tables
= Table.RemoveMatchingRows(TableC,{[Product
Name="A"]},"Product Name")
3 2/1/2023 C 10
5 2/1/2023 B 6
6 2/2/2023 B 9
8 2/3/2023 C 6
9 2/4/2023 B 10
11 2/6/2023 C 18
= Table.RemoveMatchingRows(
TableC,
{[Product Name="A"],[Product Name="B"]},
"Product Name")
3 2/1/2023 C 10
8 2/3/2023 C 6
11 2/6/2023 C 18
226
Chapter 8 Tables
= Table.RemoveMatchingRows(
TableC,
{[Product Name="A",Quantity=3]},
{"Product Name","Quantity"})
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
11 2/6/2023 C 18
227
Chapter 8 Tables
T able.InsertRows
Table.InsertRows is used to insert new rows between rows of a table:
Table.InsertRows(
table as table,
offset as number,
rows as list
) as table
= Table.InsertRows(TableC,3,
{[Row=12,Date="",Product Name="M",Quantity=7],
[Row=13,Date="",Product Name="M",Quantity=12]}
)
228
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
12 M 7
13 M 12
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
T able.ReplaceRows
Table.ReplaceRows is used to replace some rows of a table with new rows
as follows:
Table.ReplaceRows(
table as table,
offset as number,
count as number,
rows as list
) as table
229
Chapter 8 Tables
= Table.ReplaceRows(TableC,3,5,
{
[Row=12,Date="",Product Name="M",Quantity=7],
[Row=13,Date="",Product Name="M",Quantity=12]
})
230
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
12 M 7
13 M 12
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
T able.ReplaceMatchingRows
Using Table.ReplaceMatchingRows, values entered in specific rows can
be replaced with new values. This function is defined in Power Query as
follows:
Table.ReplaceMatchingRows(
table as table,
replacements as list,
optional equationCriteria as any
) as table
231
Chapter 8 Tables
{{[a=1,b=2],[a=-1,b=-2]},
{ [a=-1,b=2],[a=1,b=2]}}
T able.Repeat
Using this function in Power Query, the rows of a table can be repeated
as many times as determined in the second input of the function. Table.
Repeat is defined in Power Query as follows:
Table.Repeat(
table as table,
count as number
) as table
232
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
233
Chapter 8 Tables
O
rder of rows
Functions can be used to change the order of the rows in a table.
T able.ReverseRows
Using Table.ReverseRows, the rows of a table can be reversed. This
function is defined in Power Query as follows:
11 2/6/2023 C 18
10 2/4/2023 A 3
9 2/4/2023 B 10
8 2/3/2023 C 6
7 2/2/2023 A 6
6 2/2/2023 B 9
5 2/1/2023 B 6
4 2/1/2023 A 1
3 2/1/2023 C 10
2 1/31/2023 A 18
1 1/30/2023 A 17
234
Chapter 8 Tables
T able.Sort
To sort the rows of a table by specific logic such as ascending and
descending, use Table.Sort as follows:
Table.Sort(
table as table,
comparisonCriteria as any
) as table
{{"Quantity", Order.Descending},{"Product
Name", Order.Descending}}
= Table.Sort(TableC,"Quantity")
235
Chapter 8 Tables
4 2/1/2023 A 1
10 2/4/2023 A 3
7 2/2/2023 A 6
5 2/1/2023 B 6
8 2/3/2023 C 6
6 2/2/2023 B 9
3 2/1/2023 C 10
9 2/4/2023 B 10
1 1/30/2023 A 17
11 2/6/2023 C 18
2 1/31/2023 A 18
= Table.Sort(TableC,{"Quantity","Product Name"})
236
Chapter 8 Tables
4 2/1/2023 A 1
10 2/4/2023 A 3
7 2/2/2023 A 6
5 2/1/2023 B 6
8 2/3/2023 C 6
6 2/2/2023 B 9
9 2/4/2023 B 10
3 2/1/2023 C 10
1 1/30/2023 A 17
2 1/31/2023 A 18
11 2/6/2023 C 18
In the following formula, the basis for sorting the Quantity column
is from largest to smallest, and in case of equal Quantity, the basis is the
Product Name. See Table 8-55.
= Table.Sort(TableC,{{"Quantity",Order.
Descending},"Product Name"})
237
Chapter 8 Tables
2 1/31/2023 A 18
11 2/6/2023 C 18
1 1/30/2023 A 17
9 2/4/2023 B 10
3 2/1/2023 C 10
6 2/2/2023 B 9
7 2/2/2023 A 6
5 2/1/2023 B 6
8 2/3/2023 C 6
10 2/4/2023 A 3
4 2/1/2023 A 1
T able.IsDistinct
To check the existence of duplicate rows in a table, Table.IsDistinct can
be used as follows:
Table.IsDistinct(
table as table,
optional comparisonCriteria as any
) as logical
238
Chapter 8 Tables
This function takes two inputs. If there are duplicate rows in the
reference table based on determined columns in the second input, the
result is false; in other cases, the result is true.
1. The reference table is entered in the first input.
= Table.IsDistinct(TableC)
= Table.IsDistinct(TableC,"Product Name")
But if you also consider the date in comparison with the following
formula, the result of the formula is True:
= Table.IsDistinct(TableC,{"Date","Product Name"})
T able.MatchesAllRows
Using Table.MatchesAllRows, a condition can be checked for all rows of a
table. The result of the function is true if the defined condition is true for
all rows, and false if the defined condition is not true for at least one row.
This function is defined in Power Query as follows:
239
Chapter 8 Tables
Table.MatchesAllRows(
table as table,
condition as function
) as logical
So, the result of the following formula is true because all data in the
Quantity column is positive:
= Table.MatchesAllRows(TableC,each [Quantity]>0)
= Table.MatchesAllRows(
TableC,
each [Product Name]="A" or [Product Name]="B" or [Product
Name]="C")
And since none of the product names are equal to X, the result of the
following formula is also true:
240
Chapter 8 Tables
T able.MatchesAnyRows
Table.MatchesAnyRows is like the Table.MatchesAllRows function, with
the difference that the result of this function is true only if at least the
defined conditions is true in one row of the table. The syntax of Table.
MatchesAnyRows is as follows:
Table.MatchesAnyRows(
table as table,
condition as function
) as logical
= Table.MatchesAnyRows(
TableC,
= Table.MatchesAnyRows(
TableC,
each [Quantity]=10)
241
Chapter 8 Tables
The result of the following formula is True because in one row of the
table, product A has been sold in the amount of 6:
= Table.MatchesAnyRows(
TableC,
each [Product Name]="A" and [Quantity]=6)
But the result of the following formula is false because in none of the
rows of the table has product A been sold in the amount of 10 units:
= Table.MatchesAnyRows(
TableC,
each [Product Name]="A" and [Quantity]=10)
And, since the column of Product Name does not include the value of
x, the result of the following formula is false:
= Table.MatchesAnyRows(
TableC,
each [Product Name]="X")
T able.Contains
To check the availability of a value in a table, Table.Contains can be used
as follows:
Table.Contains(
table as table,
row as record,
optional equationCriteria as any
) as logical
242
Chapter 8 Tables
= Table.Contains(
TableC,
[Quantity=18])
And the result of the following formula is False because there is no row
in the table in which the product name is registered as B in the Product
Name column with a Quantity value of 18:
= Table.Contains(
TableC,
[Product Name="B", Quantity=18])
= Table.Contains(
TableC,
[Product Name="B",Quantity=18],
"Quantity")
243
Chapter 8 Tables
Table.ContainsAll(
table as table,
rows as list,
optional equationCriteria as any
) as logical
Table.ContainsAny(
table as table,
rows as list,
optional equationCriteria as any
) as logical
244
Chapter 8 Tables
= Table.ContainsAll(
TableC,
{[Quantity=18],[Quantity=10]})
But since the Quantity column in the reference table does not include
the 12, the result of the following formula is False:
= Table.ContainsAll(
TableC,
{[Quantity=18],[Quantity=10],[Quantity=12]})
= Table.ContainsAny(
TableC,
{[Quantity=18],[Quantity=10],[Quantity=12]})
T able.SingleRow
Table.SingleRow receives an input in the format of the table, and if that
table has only one row, this function displays the result of that row as a
record in the output; otherwise, the result of this function is an error. This
function is defined in Power Query as follows:
245
Chapter 8 Tables
C
olumn Names
To extract the list of column names and change them, there are different
functions that are discussed in this section. To simplify the example,
TableC is used in different formulas.
T able.ColumnNames
By using this function with the following syntax, the column’s names of a
table can be extracted:
This function takes a table as input and displays its column titles
as a list in the output. Therefore, the result of the formula Table.
ColumnNames(TableC) is equal to this list:
{"Row","Date","Product Name","Quantity"}
T able.DemoteHeaders
If the first row of a table has been mistakenly identified as column headers,
Table.DemoteHeaders can be used to return the column headers to
the first row of the table. This function takes a table as input, then adds
the column headers of the table as the first row, and names the table’s
columns as Column1, Column2, and so on.
246
Chapter 8 Tables
T able.PromoteHeaders
Using Table.PromoteHeaders, the values entered in the first row of the
table are promoted to the header. This function is defined in Power Query
as follows:
Table.PromoteHeaders(
table as table,
optional options as nullable record
) as table
247
Chapter 8 Tables
248
Chapter 8 Tables
= Table.PromoteHeaders(TableC, [PromoteAllScalars = true,
Culture = "en-US"])
T able.HasColumns
To evaluate the existence of one or more columns (based on the column
titles) within a table, Table.HasColumns can be used as follows:
Table.HasColumns(
table as table,
columns as any
) as logical
249
Chapter 8 Tables
T able.ColumnsOfType
To extract column name with specific format of values, Table.
ColumnsOfType can be used as follows:
250
Chapter 8 Tables
The output of this function is a list that includes the titles of the table
columns with the searching format. Therefore, according to the above
explanation, the result of the following formula is equal to all the column
names with the value format of any, so it is equal to {"Row", "Date",
"Product Name", "Quantity"}:
= Table.ColumnsOfType(TableC,{type any})
T able.PrefixColumns
This function is used in Power Query to add a prefix before the name of all
columns as follows:
Table.PrefixColumns(
table as table,
prefix as text
) as table
The output of this function is a table like the reference table, except
that a prefix has been added before the title of all columns.
For example, the result of the formula = Table.
PrefixColumns(TableC,"New") is shown in Table 8-60.
251
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
T able.RenameColumns
As the name of this function suggests, Table.RenameColumns is used to
rename column titles in a table as follows:
Table.RenameColumns(
table as table,
renames as list,
optional missingField as nullable number
) as table
252
Chapter 8 Tables
Consider the following formulas and the results (Tables 8-61 and 8-62:
= Table.RenameColumns(TableC,{"Product Name","P_Name"})
253
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
= Table.RenameColumns(
TableC,
{{"Product Name","P_Name"},{"Quantity","Amount"}})
254
Chapter 8 Tables
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
Modifing columns
There are several functions in the Power Query for modifying columns
by adding, removing, or reordering columns and they are described in
this section. To simplify the examples, TableC is used in the examples of
this part.
255
Chapter 8 Tables
T able.RemoveColumns
Using Table.RemoveColumns, extra columns in a table can be removed as
follows:
Table.RemoveColumns(
table as table,
columns as any,
optional missingField as nullable number
) as table
This function receives three inputs.
256
Chapter 8 Tables
1/30/2023 A 17
1/31/2023 A 18
2/1/2023 C 10
2/1/2023 A 1
2/1/2023 B 6
2/2/2023 B 9
2/2/2023 A 6
2/3/2023 C 6
2/4/2023 B 10
2/4/2023 A 3
2/6/2023 C 18
= Table.RemoveColumns(
TableC,
{"Row","Quantity"})
257
Chapter 8 Tables
1/30/2023 A
1/31/2023 A
2/1/2023 C
2/1/2023 A
2/1/2023 B
2/2/2023 B
2/2/2023 A
2/3/2023 C
2/4/2023 B
2/4/2023 A
2/6/2023 C
=Table.RemoveColumns(TableC,{"Row","Quantity2"})
In this case, the third input of this function can be used as follows:
=Table.RemoveColumns(
TableC,
{"Row","Quantity2"},
MissingField.UseNull )
258
Chapter 8 Tables
1/30/2023 A 17
1/31/2023 A 18
2/1/2023 C 10
2/1/2023 A 1
2/1/2023 B 6
2/2/2023 B 9
2/2/2023 A 6
2/3/2023 C 6
2/4/2023 B 10
2/4/2023 A 3
2/6/2023 C 18
T able.SelectColumns
In order to remove most of columns in a table, instead of using Table.
RemoveColumn and mentioning the name of columns that you want to
remove, Table.SelectColumns with the following syntax can be used.
In this case, the name of columns that you want to present in the output
should be entered in the second inpuy.
Table.SelectColumns(
table as table,
columns as any,
optional missingField as nullable number
) as table
259
Chapter 8 Tables
= Table.SelectColumns(TableC,"Date")
260
Chapter 8 Tables
1/30/2023
1/31/2023
2/1/2023
2/1/2023
2/1/2023
2/2/2023
2/2/2023
2/3/2023
2/4/2023
2/4/2023
2/6/2023
= Table.SelectColumns(
TableC,
{"Product Name","Row","Quantity"})
261
Chapter 8 Tables
A 1 17
A 2 18
C 3 10
A 4 1
B 5 6
B 6 9
A 7 6
C 8 6
B 9 10
A 10 3
C 11 18
= Table.SelectColumns(
TableC,
{"Date","Quantity2"})
In this case, using the third input of this function as follows, the result
of the function is as shown in Table 8-68 instead of an error:
= Table.SelectColumns(
TableC,
{"Date","Quantity2"},
MissingField.Ignore)
262
Chapter 8 Tables
1/30/2023
1/31/2023
2/1/2023
2/1/2023
2/1/2023
2/2/2023
2/2/2023
2/3/2023
2/4/2023
2/4/2023
2/6/2023
Table.DuplicateColumn
By this function, a copy of the columns in the table can be created. The
syntax of this function in Power Query is as follows:
Table.DuplicateColumn(
table as table,
columnName as text,
newColumnName as text,
optional columnType as nullable type
) as table
263
Chapter 8 Tables
= Table.DuplicateColumn(
TableC,
"Product Name",
"Product Name 2")
264
Chapter 8 Tables
1 1/30/2023 A 17 A
2 1/31/2023 A 18 A
3 2/1/2023 C 10 C
4 2/1/2023 A 1 A
5 2/1/2023 B 6 B
6 2/2/2023 B 9 B
7 2/2/2023 A 6 A
8 2/3/2023 C 6 C
9 2/4/2023 B 10 B
10 2/4/2023 A 3 A
11 2/6/2023 C 18 C
Table.TransformColumnTypes
To change the format of values in the columns of a table, this function is
used as follows:
Table.TransformColumnTypes(
table as table,
typeTransformations as list,
optional culture as nullable text
) as table
265
Chapter 8 Tables
= Table.TransformColumnTypes(
TableC,
{{"Row", Percentage.Type},
{"Date", type datetime}})
The result of this function is the same as TableC in which the format of
values in the Row column has been changed to percentage and the Date
column has been changed to date and time. See Table 8-70.
266
Chapter 8 Tables
Table.TransformColumns
To apply modifications (such as multiplying, adding a specific number to
the column values, cleaning text values, or removing extra spaces) to the
columns of a table, this function can be used as follows:
Table.TransformColumns(
table as table,
transformOperations as list,
optional defaultTransformation as nullable function,
optional missingField as nullable number
) as table
267
Chapter 8 Tables
Based on the function explanations and using TableC, the result of this
function in different scenarios is explained.
In the following formula, the value of 2 should be added to the values
in the Quantity column in TableC:
= Table.TransformColumns(
TableC,
{"Quantity",each _+2})
268
Chapter 8 Tables
1 1/30/2023 A 19
2 1/31/2023 A 20
3 2/1/2023 C 12
4 2/1/2023 A 3
5 2/1/2023 B 8
6 2/2/2023 B 11
7 2/2/2023 A 8
8 2/3/2023 C 8
9 2/4/2023 B 12
10 2/4/2023 A 5
11 2/6/2023 C 20
= Table.TransformColumns(
TableC,
{{"Date",each Date.Day(_)},
{"Product Name",each "Product: " & _},
{"Quantity",each _+2}})
269
Chapter 8 Tables
1 30 Product: A 19
2 31 Product: A 20
3 1 Product: C 12
4 1 Product: A 3
5 1 Product: B 8
6 2 Product: B 11
7 2 Product: A 8
8 3 Product: C 8
9 4 Product: B 12
10 4 Product: A 5
11 6 Product: C 20
= Table.TransformColumns(
TableC,
{"Quantity",each _/List.Sum(TableC[Quantity]) }
)
If this formula is used, the result is obtained in the form of Table 8-73.
270
Chapter 8 Tables
1 1/30/2023 A 0.163461538
2 1/31/2023 A 0.173076923
3 2/1/2023 C 0.096153846
4 2/1/2023 A 0.009615385
5 2/1/2023 B 0.057692308
6 2/2/2023 B 0.086538462
7 2/2/2023 A 0.057692308
8 2/3/2023 C 0.057692308
9 2/4/2023 B 0.096153846
10 2/4/2023 A 0.028846154
11 2/6/2023 C 0.173076923
Table.AddIndexColumn
Table.AddIndexColumn can be used as follows to add a new index column
to the table:
Table.AddIndexColumn(
table as table,
newColumnName as text,
optional initialValue as nullable number,
optional increment as nullable number,
optional columnType as nullable type
) as table
271
Chapter 8 Tables
= Table.AddIndexColumn(TableC,"Index Column")
272
Chapter 8 Tables
1 1/30/2023 A 17 0
2 1/31/2023 A 18 1
3 2/1/2023 C 10 2
4 2/1/2023 A 1 3
5 2/1/2023 B 6 4
6 2/2/2023 B 9 5
7 2/2/2023 A 6 6
8 2/3/2023 C 6 7
9 2/4/2023 B 10 8
10 2/4/2023 A 3 9
11 2/6/2023 C 18 10
= Table.AddIndexColumn(
TableC,
"Index Column",
4,
-2)
273
Chapter 8 Tables
1 1/30/2023 A 17 4
2 1/31/2023 A 18 2
3 2/1/2023 C 10 0
4 2/1/2023 A 1 -2
5 2/1/2023 B 6 -4
6 2/2/2023 B 9 -6
7 2/2/2023 A 6 -8
8 2/3/2023 C 6 -10
9 2/4/2023 B 10 -12
10 2/4/2023 A 3 -14
11 2/6/2023 C 18 -16
Table.AddColumn
The Table.AddColumn function is one of the most important functions in
Power Query and is used to add a new column to a table as follows:
Table.AddColumn(
table as table,
newColumnName as text,
columnGenerator as function,
optional columnType as nullable type
) as table
274
Chapter 8 Tables
For example, in this formula, a new column is added to the TableC that
specifies the year for each row. See Table 8-76.
= Table.AddColumn(TableC,"Year",each Date.Year([Date]))
1 1/30/2023 A 17 2023
2 1/31/2023 A 18 2023
3 2/1/2023 C 10 2023
4 2/1/2023 A 1 2023
5 2/1/2023 B 6 2023
6 2/2/2023 B 9 2023
7 2/2/2023 A 6 2023
8 2/3/2023 C 6 2023
9 2/4/2023 B 10 2023
10 2/4/2023 A 3 2023
11 2/6/2023 C 18 2023
275
Chapter 8 Tables
In the following example, the month name for each row is added to the
table. See Table 8-77.
= Table.AddColumn(
TableC,
"Month",
each Date.MonthName([Date]))
1 1/30/2023 A 17 January
2 1/31/2023 A 18 January
3 2/1/2023 C 10 February
4 2/1/2023 A 1 February
5 2/1/2023 B 6 February
6 2/2/2023 B 9 February
7 2/2/2023 A 6 February
8 2/3/2023 C 6 February
9 2/4/2023 B 10 February
10 2/4/2023 A 3 February
11 2/6/2023 C 18 February
276
Chapter 8 Tables
1 1/30/2023 A 17 TRUE
2 1/31/2023 A 18 TRUE
3 2/1/2023 C 10 FALSE
4 2/1/2023 A 1 FALSE
5 2/1/2023 B 6 FALSE
6 2/2/2023 B 9 FALSE
7 2/2/2023 A 6 FALSE
8 2/3/2023 C 6 FALSE
9 2/4/2023 B 10 FALSE
10 2/4/2023 A 3 FALSE
11 2/6/2023 C 18 TRUE
Table.AddRankColumn
Using Table.AddRankColumn, the rows of a table can be ranked based on
their values as follows:
Table.AddRankColumn(
table as table,
newColumnName as text,
comparisonCriteria as any,
optional options as nullable record
) as table
277
Chapter 8 Tables
278
Chapter 8 Tables
= Table.AddRankColumn(
TableC,
"rank",
"Quantity")
4 2/1/2023 A 1 1
10 2/4/2023 A 3 2
5 2/1/2023 B 6 3
7 2/2/2023 A 6 3
8 2/3/2023 C 6 3
6 2/2/2023 B 9 6
3 2/1/2023 C 10 7
9 2/4/2023 B 10 7
1 1/30/2023 A 17 9
2 1/31/2023 A 18 10
11 2/6/2023 C 18 10
= Table.AddRankColumn(
TableC,
"rank"
,{"Quantity",Order.Descending})
279
Chapter 8 Tables
2 1/31/2023 A 18 1
11 2/6/2023 C 18 1
1 1/30/2023 A 17 3
3 2/1/2023 C 10 4
9 2/4/2023 B 10 4
6 2/2/2023 B 9 6
5 2/1/2023 B 6 7
7 2/2/2023 A 6 7
8 2/3/2023 C 6 7
10 2/4/2023 A 3 10
4 2/1/2023 A 1 11
In another situation, if you want to change the logic of ranking, you can
use the following formula:
= Table.AddRankColumn(
TableC,
"rank",
{"Quantity",Order.Descending},
[RankKind =RankKind.Dense])
280
Chapter 8 Tables
2 1/31/2023 A 18 1
11 2/6/2023 C 18 1
1 1/30/2023 A 17 2
3 2/1/2023 C 10 3
9 2/4/2023 B 10 3
6 2/2/2023 B 9 4
5 2/1/2023 B 6 5
7 2/2/2023 A 6 5
8 2/3/2023 C 6 5
10 2/4/2023 A 3 6
4 2/1/2023 A 1 7
Table.SplitColumn
Table.SplitColumn can be used to divide the values in a column of a
table into multiple columns based on a specific criterion. This function is
defined in Power Query as follows:
Table.SplitColumn(
table as table,
sourceColumn as text,
splitter as function,
optional columnNamesOrNumber as any,
optional default as any,
optional extraColumns as any
) as table
281
Chapter 8 Tables
Splitter.SplitByNothing
Splitter.SplitTextByCharacterTransition
Splitter.SplitTextByAnyDelimiter
Splitter.SplitTextByDelimiter
Splitter.SplitTextByEachDelimiter
Splitter.SplitTextByLengths
Splitter.SplitTextByPositions
Splitter.SplitTextByRanges
Splitter.SplitTextByRepeatedLengths
Splitter.SplitTextByWhitespace
282
Chapter 8 Tables
= Table.SplitColumn(
TableC,
"Date",
Splitter.SplitTextByDelimiter("/"),
1 1 30 2023 A 17
2 1 31 2023 A 18
3 2 1 2023 C 10
4 2 1 2023 A 1
5 2 1 2023 B 6
6 2 2 2023 B 9
7 2 2 2023 A 6
8 2 3 2023 C 6
9 2 4 2023 B 10
10 2 4 2023 A 3
11 2 6 2023 C 18
283
Chapter 8 Tables
= Table.SplitColumn(
TableC,
"Date",
Splitter.SplitTextByDelimiter("/"),
{"Day", "Month"})
1 1 30 A 17
2 1 31 A 18
3 2 1 C 10
4 2 1 A 1
5 2 1 B 6
6 2 2 B 9
7 2 2 A 6
8 2 3 C 6
9 2 4 B 10
10 2 4 A 3
11 2 6 C 18
284
Chapter 8 Tables
= Table.SplitColumn(
TableC,
"Date",
Splitter.SplitTextByRepeatedLengths(3),
{"Date.1", "Date.2", "Date.3"})
Table.CombineColumns
Using Table.CombineColumns, the values of two or more columns can be
merged into a new column. This function is defined in Power Query as
follows:
285
Chapter 8 Tables
Table.CombineColumns(
table as table,
sourceColumns as list,
combiner as function,
column as text
) as table
Combiner.CombineTextByDelimiter
Combiner.CombineTextByEachDelimiter
Combiner.CombineTextByLengths
Combiner.CombineTextByPositions
Combiner.CombineTextByRanges
4. In the last input of this function, the name of the
new column is entered as a text value.
Sara Sani 18
Omid Motamedi 32
Mohamad Abed 26
286
Chapter 8 Tables
= Table.CombineColumns(
TableG,
{"Name", "Family"},
Combiner.CombineTextByDelimiter(","),
"New Column")
is equal to Table 8-86, in which the name and family are placed next to
each other and separated by a comma.
Sara, Sani 18
Omid, Motamedi 32
Mohamad, Abed 26
If the following formula is used, the first three characters of the name
and the first two characters of the family will be placed next to each other:
= Table.CombineColumns(
TableG,
{"Name", "Family"},
Combiner.CombineTextByLengths({3,2}),
"New Column")
287
Chapter 8 Tables
SarSa 18
OmiMo 32
MohAb 26
Table.CombineColumnsToRecord
Table.CombineColumnsToRecord is used to combine values from two or
more columns into a record and display them in one column in Power
Query as follows:
Table.CombineColumnsToRecord(
table as table,
newColumnName as text,
sourceColumns as list,
optional options as nullable record
) as table
288
Chapter 8 Tables
Sara, Sani 18
Omid, Motamedi 32
Mohamad, Abed 26
The result of
= Table.CombineColumnsToRecord(
TableK,
"New Column",
{"Name", "Family"})
[Record] 18
[Record] 32
[Record] 26
In this output, each of the values in the New Column is a record that
includes the name and surname.
289
Chapter 8 Tables
Table.ReorderColumns
To change the order of columns in a table, Table.ReorderColumns can be
used as follows:
Table.ReorderColumns(
table as table,
columnOrder as list,
optional missingField as nullable number
) as table
The output of this function is the same as the input table, except
that the order of columns in the output table has changed. This function
receives three inputs:
1. The first input of this function specifies the
reference table.
= Table.ReorderColumns(TableC,{"Product Name","Row","Quantity
","Date"})
290
Chapter 8 Tables
A 1 17 1/30/2023
A 2 18 1/31/2023
C 3 10 2/1/2023
A 4 1 2/1/2023
B 5 6 2/1/2023
B 6 9 2/2/2023
A 7 6 2/2/2023
C 8 6 2/3/2023
B 9 10 2/4/2023
A 10 3 2/4/2023
C 11 18 2/6/2023
Table.Pivot
By using the Table.Pivot, values entered in a column of a table can be
displayed as separate columns in a wider table. This function is defined in
Power Query as follows:
Table.Pivot(
table as table,
pivotValues as list,
attributeColumn as text,
valueColumn as text,
optional aggregationFunction as nullable function
) as table
291
Chapter 8 Tables
292
Chapter 8 Tables
= List.Distinct(TableE[Product Name])
293
Chapter 8 Tables
The output of this function is equal to the list of all product names, so
by the following formula, if new products are added to TableE, they are
automatically considered:
= Table.Pivot(
TableE,
List.Distinct(TableE[Product Name]),
"Product Name",
"Quantity",
List.Sum)
Table.Unpivot
This function with the following syntax is used to unpivot columns, which
means transforming some column headers into a single column and their
values into another column in front of them (display the values entered in
2 or more columns under each other in two columns):
Table.Unpivot(
table as table,
pivotColumns as list,
attributeColumn as text,
valueColumn as text
) as table
294
Chapter 8 Tables
= Table.Unpivot(
TableF,
{"A","B","C"},
"Product Name",
"Quantity")
295
Chapter 8 Tables
1/30/2023 A 17
1/31/2023 A 18
2/1/2023 A 1
2/1/2023 C 10
2/1/2023 B 6
2/2/2023 A 6
2/2/2023 B 9
2/3/2023 C 6
2/4/2023 A 3
2/4/2023 B 10
2/6/2023 C 18
= Table.UnpivotOtherColumns(
TableF,
{"Date"},
"Attribute",
"Value")
296
Chapter 8 Tables
Table.Group
Table.Group is one of the most important functions in Power Query and it
can be used to summarize values in a table. For example, the annual sales
per product can be easily extracted from a large database of historical sales
by this function. The Table.Group function in Power Query is defined as
follows:
Table.Group(
table as table,
key as any,
aggregatedColumns as list,
optional groupKind as nullable number,
optional comparer as nullable function
) as table
297
Chapter 8 Tables
= Table.Group(
TableC,
"Product Name",
{"Total",each List.Sum([Quantity])})
A 45
C 34
B 25
In the above formula, since no value is entered for the fourth input
of this function, the grouping type is considered as GroupKind.Global.
However, if you enter the grouping type as GroupKind.Local in the fourth
input of this function, grouping occurs based on the neighbors’ product
names. The result is shown in Table 8-96.
298
Chapter 8 Tables
= Table.Group(
TableC,
"Product Name",
{"Total",each List.Sum([Quantity])},
GroupKind.Local)
A 35
C 10
A 1
B 15
A 6
C 6
B 10
A 3
C 18
= Table.Group(
TableC,
"Product Name",
{
{"Total",each List.Sum([Quantity])},
{"AVG", each List.Average([Quantity])}
})
299
Chapter 8 Tables
A 45 9
C 34 11.33333333
B 25 8.333333333
In this function, to bring the values related to each product in the form
of a table next to that product, this function can be used as follows:
=Table.Group(
TableC,
{"Product Name"},
{"Count", each _})
A [Table]
C [Table]
B [Table]
300
Chapter 8 Tables
For each product, all rows related to its sales are listed. For example,
the table related to product C is visible. See Figure 8-3.
Table.Transpose
This function is used to transpose (swap rows and columns of a table) a
table as follows:
Table.Transpose(
table as table,
optional columns as any
) as table
301
Chapter 8 Tables
= Table.Transpose(TableS,{"Name","Family","Age"})
302
Chapter 8 Tables
Combining Tables
There are two methods for combining tables. In the first method, which is
known as appending, the values of two tables are placed under each other.
For example, table one is the sales values for 2019 and table two is the
sales values for 2020. By appending these two tables, a new table is created
including the sales values in both the years 2019 and 2020.
However, sometimes it is necessary to bring the values from a column
of Table A and display it in a new column in Table B. For example, if the
values of product prices are in Table A and Table B include the historical
sales volume, to calculate the total sales in dollar, it is necessary to call the
price from Table A and display it in Table B. This method of combining
tables, which ends with adding new columns to the reference table, is
called merging.
In this section, the functions related to both methods of combining
tables are discussed.
Table.Combine
Table.Combine is used to append two or more tables in Power Query as
follows:
Table.Combine(
tables as list,
optional columns as any
) as table
303
Chapter 8 Tables
For this example, consider TableH, TableI, and TableJ (shown in Tables
8-102, 8-103, and 8-104, respectively).
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
304
Chapter 8 Tables
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
305
Chapter 8 Tables
1/30/2023 17
1/31/2023 18
2/1/2023 10
2/1/2023 1
2/1/2023 6
2/2/2023 9
2/2/2023 6
2/3/2023 6
2/4/2023 10
2/4/2023 3
2/6/2023 18
306
Chapter 8 Tables
1 1/30/2023 A 17 null
2 1/31/2023 A 18 null
3 2/1/2023 C 10 null
4 2/1/2023 A 1 null
5 2/1/2023 B 6 null
6 2/2/2023 B 9 null
7 2/2/2023 A 6 null
8 2/3/2023 C 6 null
9 2/4/2023 Null 10 B
10 2/4/2023 Null 3 A
11 2/6/2023 Null 18 C
307
Chapter 8 Tables
Table.Join
To merge tables (add columns of one table to another table as new
columns), Table.Join can be used. To match the rows of the tables, both
tables should include a common column (such as product ID or customer
ID) with the same format and common values. This function can be used
as follows:
Table.Join(
table1 as table,
key1 as any,
table2 as table,
key2 as any,
optional joinKind as nullable number,
optional joinAlgorithm as nullable number,
optional keyEqualityComparers as nullable list
) as table
308
Chapter 8 Tables
JoinKind.Inner
JoinKind.LeftOuter
JoinKind.RightOuter
JoinKind.FullOuter
JoinKind.LeftAnti
JoinKind.RightAnti
JoinAlgorithm.Dynamic 0
JoinAlgorithm.PairwiseHash 1
JoinAlgorithm.SortMerge 2
JoinAlgorithm.LeftHash 3
JoinAlgorithm.RightHash 4
JoinAlgorithm.LeftIndex 5
JoinAlgorithm.RightIndex 6
309
Chapter 8 Tables
Comparer.Equals
Comparer.FromCulture
Comparer.Ordinal
Comparer.OrdinalIgnoreCase
3/21/2021 A 5
3/21/2021 B 4
3/23/2021 A 3
3/23/2021 C 5
3/24/2021 D 1
A 100
B 150
E 200
F 100
310
Chapter 8 Tables
If products A and B are both present on the two tables, and product
A has been repeated twice in the first table, the result of the following
formula is shown in Table 8-111:
= Table.Join(TableP,"Product",TableQ,"Prod
uct Name")
3/21/2021 A 5 A 100
3/23/2021 A 3 A 100
3/21/2021 B 4 B 150
In the above formula, the type of merger is not specified, and in this
case, the default method of merging, which is known as inner merger, is
applied, which will lead to a table for the rows with the product name that
existed in both tables.
To explain inner merge, consider Figure 8-4. The left circle represents
the names of the products listed in the first table and the right circle
represents the names of the products listed in the second table. In this
example, two products, A and B, are listed in both tables (shown in the
common area of two circles).
311
Chapter 8 Tables
312
Chapter 8 Tables
JoinKind.LeftOuter
JoinKind.RightOuter
JoinKind.FullOuter
JoinKind.LeftAnti
JoinKind.RightAnti
313
Chapter 8 Tables
= Table.Join(TableP,
"Product",
TableQ,
"Product Name",
JoinKind.LeftOuter)
3/21/2021 A 5 A 100
3/23/2021 A 3 A 100
3/21/2021 B 4 B 150
3/23/2021 C 5 null null
3/24/2021 D 1 null null
= Table.Join(
TableP,
"Product",
TableQ,
"Product Name",
JoinKind.RightOuter)
314
Chapter 8 Tables
3/21/2021 A 5 A 100
3/23/2021 A 3 A 100
3/21/2021 B 4 B 150
Null null null E 200
Null null null F 100
= Table.Join(
TableP,
"Product",
TableQ,
"Product Name",
JoinKind.FullOuter)
3/21/2021 A 5 A 100
3/23/2021 A 3 A 100
3/21/2021 B 4 B 150
3/23/2021 C 5 null null
3/24/2021 D 1 null null
Null null null E 200
Null null null F 100
315
Chapter 8 Tables
= Table.Join(
TableP,
"Product",
TableQ,
"Product Name",
JoinKind.LeftAnti)
= Table.Join(
TableP,
"Product",
TableQ,
"Product Name",
JoinKind.RightAnti)
The main drawback of this function is that the column names in the
first and second tables must be completely unique and distinct. If there
is a column with the same name in both tables, this function will result in
an error.
316
Chapter 8 Tables
Table.NestedJoin
Table.NestedJoin is like the Table.Join function; the difference is that in
the output of this function, the related rows in the second table are added
to a column in the first table as values in the format of the table. Since the
name of the new column is specified by the user, if the tables have columns
with the same name, the output of this function is not an error.
This function is defined in the query as follows:
Table.NestedJoin(
table1 as table,
key1 as any,
table2 as any,
key2 as any,
newColumnName as text,
optional joinKind as nullable number,
optional keyEqualityComparers as nullable list
) as table
317
Chapter 8 Tables
JoinKind.Inner
JoinKind.LeftOuter
JoinKind.RightOuter
JoinKind.FullOuter
JoinKind.LeftAnti
JoinKind.RightAnti
7. The seventh input of this function is optional and
determines the comparison criterion entering one
of the following values:
Comparer.Equals
Comparer.FromCulture
Comparer.Ordinal
Comparer.OrdinalIgnoreCase
= Table.NestedJoin(
TableP,
"Product",
TableQ,
"Product Name",
"New Data")
318
Chapter 8 Tables
3/21/2021 A 5 [Table]
3/21/2021 B 4 [Table]
3/23/2021 A 3 [Table]
3/23/2021 C 5 [Table]
3/24/2021 D 1 [Table]
As seen, the New Data column only contains the table expression. By
clicking the first value of this column, the data summary will be displayed,
as shown in Figure 8-6.
Expanding Columns
In the case that values in a column are in the format of list, record, or table,
the functions of this section can be used to expand such values.
319
Chapter 8 Tables
Table.ExpandListColumn
If one of the columns in the table contains information of type list, this
function can be used as follows to expand the values as new rows:
Table.ExpandListColumn(
table as table,
column as text
) as table
1/30/2023 [List]
1/31/2023 [List]
2/1/2023 [List]
2/2/2023 [List]
2/3/2023 [List]
2/4/2023 [List]
2/6/2023 [List]
320
Chapter 8 Tables
In the Product Name column, the names of the products sold on that
date are listed. For example, in Figure 8-7, the list of products sold on the
date 2/1/2023 is displayed.
= Table.ExpandListColumn(TableM,"Product Name")
321
Chapter 8 Tables
1/30/2023 A
1/30/2023 B
1/31/2023 A
2/1/2023 C
2/1/2023 A
2/1/2023 B
2/2/2023 B
2/2/2023 A
2/3/2023 C
2/4/2023 B
2/4/2023 A
2/6/2023 C
Table.ExpandRecordColumn
To expand the values entered in the format of records in a table, Table.
ExpandRecordColumn can be used as follows:
Table.ExpandRecordColumn(
table as table,
column as text,
fieldNames as list,
optional newColumnNames as nullable list
) as table
322
Chapter 8 Tables
1/30/2023 [Record]
1/31/2023 [Record]
2/1/2023 [Record]
2/2/2023 [Record]
2/3/2023 [Record]
2/4/2023 [Record]
2/6/2023 [Record]
Here the values related to sales (including product name and quantity)
for each date are entered as records in the Info column. Figure 8-8
shows the details of the record entered in the Info column for the date of
2023/2/2.
323
Chapter 8 Tables
To extract the values of this column, the following formula can be used:
= Table.ExpandRecordColumn(TableN,"Info",{"Quantity"})
1/30/2023 10
1/31/2023 3
2/1/2023 5
2/2/2023 21
2/3/2023 11
2/4/2023 12
2/6/2023 8
324
Chapter 8 Tables
= Table.ExpandRecordColumn(TableN,
"Info",
{"Product Name","Quantity"})
1/30/2023 A 10
1/31/2023 B 3
2/1/2023 A 5
2/2/2023 C 21
2/3/2023 C 11
2/4/2023 A 12
2/6/2023 B 8
= Table.ExpandRecordColumn(
TableN,
"Info",
{"Product Name","Quantity"},
{"X1","X2"})
325
Chapter 8 Tables
1/30/2023 A 10
1/31/2023 B 3
2/1/2023 A 5
2/2/2023 C 21
2/3/2023 C 11
2/4/2023 A 12
2/6/2023 B 8
Table.ExpandTableColumn
To expand the values in the format of a table, entered in a column, this
function can be used as follows:
Table.ExpandTableColumn(
table as table,
column as text,
columnNames as list,
optional newColumnNames as nullable list
) as table
326
Chapter 8 Tables
1/30/2023 [Table]
1/31/2023 [Table]
2/1/2023 [Table]
The product names and sales quantity for each date are presented in
the format of the table in front of each table. Figure 8-9 shows the entered
values for the last date.
327
Chapter 8 Tables
To extract the values from the Info column, this function can be used
as follows:
= Table.ExpandTableColumn(
TableO,
"Info",
{"Product Name","Quantity"})
1/30/2023 A 2
1/30/2023 B 12
1/30/2023 C 6
1/31/2023 B 8
1/31/2023 C 9
2/1/2023 A 3
2/1/2023 C 7
328
Chapter 8 Tables
Table.AggregateTableColumn
Instead of extracting values from the tables entered in the Info
column, the summary of values can be extracted by using Table.
AggregateTableColumn. This function can be used as follows:
Table.AggregateTableColumn(
table as table,
column as text,
aggregations as list
) as table
= Table.AggregateTableColumn(
TableO,
"Info",
{{"Quantity",List.Sum,"Sum of Quantity"}})
329
Chapter 8 Tables
1/30/2023 20
1/31/2023 17
2/1/2023 10
= Table.AggregateTableColumn(
TableO,
"Info",
{{"Quantity",List.Sum,"Sum of Quantity"},
{"Quantity",List.Average,"Average of
Quantity"},
{"Product",List.Count,"Count of Product"}})
1/30/2023 20 6.666666667 3
1/31/2023 17 8.5 2
2/1/2023 10 5 2
330
Chapter 8 Tables
Table.FillDown(
table as table,
columns as list
) as table
Table.FillUp(
table as table,
columns as list
) as table
331
Chapter 8 Tables
1 1/30/2023 A 17
2 null A 18
3 2/1/2023 C 10
4 null A 1
5 null B 6
6 2/2/2023 B 9
7 null A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 null A 3
11 2/6/2023 C 18
1 1/30/2023 A 17
2 1/30/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
(continued)
332
Chapter 8 Tables
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
= Table.FillUp(TableL,{"Date"})
1 1/30/2023 A 17
2 2/1/2023 A 18
3 2/1/2023 C 10
4 2/2/2023 A 1
5 2/2/2023 B 6
6 2/2/2023 B 9
7 2/3/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/6/2023 A 3
11 2/6/2023 C 18
333
Chapter 8 Tables
Table.ReplaceErrorValues
This function is used to replace error values in one or more columns of a
table with new values as follows:
Table.ReplaceErrorValues(
table as table,
errorReplacement as list
) as table
334
Chapter 8 Tables
2 1/31/2023 A 18 1
11 2/6/2023 Error 18 1
1 1/30/2023 A 17 2
3 2/1/2023 C 10 3
9 2/4/2023 B 10 3
6 2/2/2023 Error 9 4
5 2/1/2023 B Error 5
7 2/2/2023 A 6 5
8 2/3/2023 C 6 5
10 2/4/2023 A Error 6
4 2/1/2023 A 1 Error
So, the following formulas come with different results. See Tables 8-133
and 8-134.
Table.ReplaceErrorValues(
TableR,
{"Product Name","New Value1"})
335
Chapter 8 Tables
2 1/31/2023 A 18 1
11 2/6/2023 New Value1 18 1
1 1/30/2023 A 17 2
3 2/1/2023 C 10 3
9 2/4/2023 B 10 3
6 2/2/2023 New Value1 9 4
5 2/1/2023 B Error 5
7 2/2/2023 A 6 5
8 2/3/2023 C 6 5
10 2/4/2023 A Error 6
4 2/1/2023 A 1 Error
Table.ReplaceErrorValues(
TableR,
{
{"Product Name","New Value1"},
{"Quantity","New Value2"},
{"rank","New Value3"}
})
336
Chapter 8 Tables
2 1/31/2023 A 18 1
11 2/6/2023 New Value1 18 1
1 1/30/2023 A 17 2
3 2/1/2023 C 10 3
9 2/4/2023 B 10 3
6 2/2/2023 New Value1 9 4
5 2/1/2023 B New Value2 5
7 2/2/2023 A 6 5
8 2/3/2023 C 6 5
10 2/4/2023 A New Value2 6
4 2/1/2023 A 1 New Value3
Table.ReplaceValue
To replace the values recorded in the columns of a table with new values,
the Table.ReplaceValue can be used as follows.
Table.ReplaceValue(
table as table,
oldValue as any,
newValue as any,
replacer as function,
columnsToSearch as list
) as table
337
Chapter 8 Tables
= Table.ReplaceValue(
TableC,
"A",
"Mobile",
Replacer.ReplaceText,
{"Product Name"})
338
Chapter 8 Tables
1 1/30/2023 Mobile 17
2 1/31/2023 Mobile 18
3 2/1/2023 C 10
4 2/1/2023 Mobile 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 Mobile 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 Mobile 3
11 2/6/2023 C 18
= Table.ReplaceValue(
TableC,
1,
100,
Replacer.ReplaceValue,
{"Row","Quantity"})
339
Chapter 8 Tables
100 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 100
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
Table.FindText
This function is used to extract rows from a table that contain a specific
text. This function is defined in Power Query as follows:
Table.FindText(
table as table,
text as text
) as table
340
Chapter 8 Tables
The output of the function is the rows of the reference table that
include the searching text. Therefore, the result of the formula = Table.
FindText(TableC, "A") is equal to Table 8-137.
1 1/30/2023 A 17
2 1/31/2023 A 18
4 2/1/2023 A 1
7 2/2/2023 A 6
10 2/4/2023 A 3
Table.PositionOf
Table.PositionOf is used to extract the number of rows including specific
values, as follows:
Table.PositionOf(
table as table,
row as record,
optional occurrence as any,
optional equationCriteria as any
) as any
341
Chapter 8 Tables
342
Chapter 8 Tables
A 1
B 3
A 2
B 3
A 2
C 1
A 2
C 3
B 2
= Table.PositionOf(TableT,[Product Name="C",Quantity=1])
= Table.PositionOf(
TableT,
[Product Name="A",Quantity=2])
343
Chapter 8 Tables
If you want to display all occurrences in the output, use the following
formula:
= Table.PositionOf(
TableT,
[Product Name="A",Quantity=2]
,Occurrence.All)
Table.PositionOfAny
Table.PositionOfAny is like the Table.PositionOfAny function, with the
difference that in the second input of this function, several conditions can
be defined like
Table.PositionOfAny(
table as table,
rows as list,
optional occurrence as nullable number,
optional equationCriteria as any
) as any
= Table.PositionOfAny(
TableT,
{
344
Chapter 8 Tables
[Product Name="A",Quantity=2],
[Product Name="C",Quantity=1]
},
Occurrence.All)
Summary
This chapter was a journey through the intricate landscape of table
functions within Power Query, providing a comprehensive understanding
of how tables serve as the foundation for structured data analysis and
manipulation.
The chapter commenced with a fundamental exploration of tables
as dynamic data containers. You grasped the essence of columns and
rows, understanding how Power Query views tables as versatile structures
capable of holding diverse data types and relationships. Through this, you
learned to manually create tables and generate them through calculated
expressions.
As you delved deeper, you uncovered the transformative power of
table functions. These functions empower you to filter, sort, reshape, and
aggregate data within tables with precision. The art of crafting tables that
align with your analytical goals became second nature, enhancing your
data manipulation abilities.
The chapter led you into the realm of table relationships, where data
connections unveiled themselves. You explored techniques for joining,
merging, and relating tables, piecing together data from diverse sources to
weave coherent narratives. This skillset allows you to unearth insights that
span multiple datasets.
Furthermore, you delved into the world of data enrichment and
summarization. The potential of these functions came to light as you
discovered how to distill complex datasets into concise, insightful
summaries. This skill empowers you to identify trends, patterns, and
anomalies hidden within the data.
345
Chapter 8 Tables
346
CHAPTER 9
Folder.Contents
Using Folder.Contents, values stored in files with various formats (such as
.xlsx, .accdb, .txt, and CSV) in a folder can be called as follows:
Folder.Contents(
path as text,
optional options as nullable record
) as table
348
Chapter 9 Extracting from Data Sources
= Folder.Contents("C:\Users\omid_\OneDrive\
Desktop\Data")
Csv.Document
By using Folder.Contents to load values from text or CSV files, saved in a
specific path, the information will be shown in the format of binary in the
column with the name “content”. To extract such values, CSV.Document can
be used as follows:
Csv.Document(
source as any,
optional columns as any,
optional delimiter as any,
optional extraValues as nullable number,
optional encoding as nullable number
) as table
349
Chapter 9 Extracting from Data Sources
each CSV.Document([Content]))
350
Chapter 9 Extracting from Data Sources
As you can see, the word Table is shown in the new column in all rows.
By selecting the value of this column in front of the CSV or text files (rows
1, 2, 4, and 5), the values inside that file will be displayed correctly, as
shown in Figure 9-4.
351
Chapter 9 Extracting from Data Sources
But, by selecting the value of this column in the row where the data
format is XLSX (Excel file), the result will be disordered and displayed as
shown in Figure 9-5.
352
Chapter 9 Extracting from Data Sources
The same result can be shown by selecting the Table in the rows for
PDF files. See Figure 9-6.
Excel.Workbook
Following the previous example, after calling the Excel files stored in a
specific path by Folder.Content as binary values, to extract the tables in
the excel files from binary values, Excel.Workbook can be used as follows:
Excel.Workbook(
workbook as binary,
optional useHeaders as any,
optional delayTypes as nullable logical
) as table
353
Chapter 9 Extracting from Data Sources
"Custom",
each Excel.Workbook([Content]))
354
Chapter 9 Extracting from Data Sources
As you can see, for binary files with the XLSX extension, the result of
this function is a new table containing values of that file, and for other file
formats, the result of this function is an error.
Pdf.Tables
In the previous example, by using Pdf.Tables as follows, instead of Excel.
Workbook, the values in the PDF file can be extracted:
Pdf.Tables(
pdf as binary,
optional options as nullable record
) as table
Based on the function syntax, this function receives these two inputs:
The result is shown in Figure 9-8, where for binary files with the .pdf
extension, the result of this function is equal to the new table containing
the values stored in the PDF file. For other file types, the result of this
function is an error.
355
Chapter 9 Extracting from Data Sources
Sql.Database
Sql.Database is defined in Power Query to call tables from a SQL database
as follows:
Sql.Database(
server as text,
database as text,
optional options as nullable record
) as table
356
Chapter 9 Extracting from Data Sources
Excel.CurrentWorkbook
Excel.CurrentWorkbook can be used in Excel for calling all tables in a
current workbook into Power Query, like so:
Excel.CurrentWorkbook() as table
Summary
In this chapter, you embarked on a comprehensive journey through the
realm of data extraction using Power Query’s data source functions. The
chapter highlighted the significance of accessing and unifying data from
diverse sources to fuel informed decision-making.
357
Chapter 9 Extracting from Data Sources
358
CHAPTER 10
Other Functions
While Power Query’s foundational functions empower you to navigate,
shape, and aggregate data, there are moments when your data
transformations demand an extra layer of sophistication. This is where the
advanced functions come into play. They offer the ability to make nuanced
decisions, handle exceptions gracefully, and craft custom expressions
tailored to your specific needs.
Your journey through this chapter begins with the exploration of
conditional logic, which is the art of asking questions of your data.
Functions like IF introduce decision-making abilities into your data
transformations. Whether it’s filtering data based on specific conditions
or dynamically altering calculations based on input, these functions
empower you to craft custom, context-aware data flows.
But data isn’t always pristine, and errors can creep in unexpectedly.
This is where functions like TRY, OTHERWISE, and ERROR come to the rescue.
You’ll delve into the world of error handling, learning how to gracefully
manage exceptions, replace missing values, and ensure that your data
transformations continue even in the face of unforeseen challenges.
As you venture deeper into this chapter, you’ll unveil the potential of
LET, a function that might seem modest but holds the key to streamlining
complex expressions. Learn how LET functions enhance readability and
maintainability by breaking down intricate transformations into more
digestible parts.
if
The if in Power Query is very simple and is usually used within the Table.
AddColumns function. This function is generally defined as follows:
For example,
360
Chapter 10 Other Functions
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
361
Chapter 10 Other Functions
= Table.AddColumn(
TableC,
"Sales",
each
(if [Product Name]="A" then 10 else if [Product
Name]="B" then 13 else 7)*[Quantity])
1 1/30/2023 A 17 170
2 1/31/2023 A 18 180
3 2/1/2023 C 10 70
4 2/1/2023 A 1 10
5 2/1/2023 B 6 78
6 2/2/2023 B 9 117
7 2/2/2023 A 6 60
8 2/3/2023 C 6 42
9 2/4/2023 B 10 130
10 2/4/2023 A 3 30
11 2/6/2023 C 18 126
Combining Conditions
To evaluate two or more conditions simultaneously, and and or can be
used. For example, to identify the rows where the quantity of product A is
greater than 10, two conditions can be defined as follows:
362
Chapter 10 Other Functions
When the word and is used between two conditions, the result will be
true for the rows where the value of “A” is entered in the column of product
name and the number registered in the quantity column is greater than 10.
Therefore, the result of
= Table.AddColumn(
TableC,
"A and Greater than 10",
each [Product Name]="A" and [Quantity]>10)
1 1/30/2023 A 17 TRUE
2 1/31/2023 A 18 TRUE
3 2/1/2023 C 10 FALSE
4 2/1/2023 A 1 FALSE
5 2/1/2023 B 6 FALSE
6 2/2/2023 B 9 FALSE
7 2/2/2023 A 6 FALSE
8 2/3/2023 C 6 FALSE
9 2/4/2023 B 10 FALSE
10 2/4/2023 A 3 FALSE
11 2/6/2023 C 18 FALSE
363
Chapter 10 Other Functions
value “A” is recorded in the product name column or the number entered
in the quantity column is greater than 10, the formula result will be true.
Therefore, the result of
= Table.AddColumn(
TableC,
"A or Greater than 10",
each [Product Name]="A" or [Quantity]>10)
1 1/30/2023 A 17 TRUE
2 1/31/2023 A 18 TRUE
3 2/1/2023 C 10 FALSE
4 2/1/2023 A 1 TRUE
5 2/1/2023 B 6 FALSE
6 2/2/2023 B 9 FALSE
7 2/2/2023 A 6 TRUE
8 2/3/2023 C 6 FALSE
9 2/4/2023 B 10 FALSE
10 2/4/2023 A 3 TRUE
11 2/6/2023 C 18 TRUE
364
Chapter 10 Other Functions
when using the not in the last example (shown below), wherever the
formula value in column A or Greater than 10 is equal to true, it is changed
to false, and wherever the function value is equal to false, it is changed to
true. See Table 10-5.
= Table.AddColumn(
TableC,
"A and Greater than 10",
each not ([Product Name]="A" or [Quantity]>10))
)
1 1/30/2023 A 17 FALSE
2 1/31/2023 A 18 FALSE
3 2/1/2023 C 10 TRUE
4 2/1/2023 A 1 FALSE
5 2/1/2023 B 6 TRUE
6 2/2/2023 B 9 TRUE
7 2/2/2023 A 6 FALSE
8 2/3/2023 C 6 TRUE
9 2/4/2023 B 10 TRUE
10 2/4/2023 A 3 FALSE
11 2/6/2023 C 18 FALSE
365
Chapter 10 Other Functions
=Number.From("2")
However, using Number.From for the text “A” as follows will result in
an error:
=Number.From("A")
In this case, the calculation stops due to the error, and the result shown
in Figure 10-1 is obtained.
=try Number.From("2")
Using this function in a case where the result of the function is an error
(as in the following example), the function result will contain a record that
specifies the cause of the error:
=try Number.From("A")
366
Chapter 10 Other Functions
For the above function, the result is equal to the following record:
[HasError=True, Error=
[Reason= "DataFormat.Error",
Message="We couldn't convert to Number.",
Detail="A",
Message.Format= "We couldn't convert to Number.",
Message.Parameters= null]]
As you can see, the first field of the output specifies that the formula
result is an error, and the second field (Error) provides additional
information about the reason for the error and the message displayed to
the user.
In a more complete form of the try function, it can be used with the
otherwise operator as follows:
367
Chapter 10 Other Functions
let
A=2,
B=3,
C=A*B
in
C
As you can see, the commands start with the let expression, followed
by the variable name and its value after the equal sign. The variable name
can include spaces (e.g., A A), which should be defined in double quotes
after the # sign, as in #"A A". Each command ends with a comma (except
the last one before the in expression) and is followed by the next variable
definition. In each command, the user can refer to the variables defined
in other commands (the previous or next one). This function ends with
the keyword in and then the name of variable you want to show as the
function output is mentioned.
The following is an example of the let-in function in Power Query:
let
Table = Table.FromColumns(
{{1,2,3,4,5,6,7,8,9,10,11},
368
Chapter 10 Other Functions
{44956,44957,44958,44958,44958,44959,44959,44960,
44961,44961,44963},
{"A","A","C","A","B","B","A","C","B","A","C"},
{17,18,10,1,6,9,6,6,10,3,18}},
{"Row" ,"Date","Product Name","Quantity"}),
Changed_Type_ColumnDate = Table.TransformColumnTypes
(Table,{{"Date", type date}}),
Removed_Column_Row = Table.RemoveColumns
(Changed_Type_ColumnDate,{"Row"}),
Added_Column_Sales = Table.AddColumn(Removed_Column_Row,
"Sale", each [Quantity]*10)
in
Added_Column_Sales
369
Chapter 10 Other Functions
1 44956 A 17
2 44957 A 18
3 44958 C 10
4 44958 A 1
5 44958 B 6
6 44959 B 9
7 44959 A 6
8 44960 C 6
9 44961 B 10
10 44961 A 3
11 44963 C 18
370
Chapter 10 Other Functions
1 1/30/2023 A 17
2 1/31/2023 A 18
3 2/1/2023 C 10
4 2/1/2023 A 1
5 2/1/2023 B 6
6 2/2/2023 B 9
7 2/2/2023 A 6
8 2/3/2023 C 6
9 2/4/2023 B 10
10 2/4/2023 A 3
11 2/6/2023 C 18
371
Chapter 10 Other Functions
1/30/2023 A 17
1/31/2023 A 18
2/1/2023 C 10
2/1/2023 A 1
2/1/2023 B 6
2/2/2023 B 9
2/2/2023 A 6
2/3/2023 C 6
2/4/2023 B 10
2/4/2023 A 3
2/6/2023 C 18
372
Chapter 10 Other Functions
1/30/2023 A 17 170
1/31/2023 A 18 180
2/1/2023 C 10 100
2/1/2023 A 1 10
2/1/2023 B 6 60
2/2/2023 B 9 90
2/2/2023 A 6 60
2/3/2023 C 6 60
2/4/2023 B 10 100
2/4/2023 A 3 30
2/6/2023 C 18 180
373
Chapter 10 Other Functions
374
Chapter 10 Other Functions
(X,Y)=> X*Y
(x)=>
let
a=x+1,
b=a*x,
c= if b > 10 then b*2 else b-2
in
c
375
Chapter 10 Other Functions
376
Chapter 10 Other Functions
377
Chapter 10 Other Functions
378
Chapter 10 Other Functions
a5 = a4 + a3
379
Chapter 10 Other Functions
a4 = a3 + a2
a3 = a2 + a1
380
Chapter 10 Other Functions
381
Index
A, B number format, 33, 34
record format, 35, 36
Binary values, 30, 350, 353
text, 79–81
text format, 34, 35
C Data sources
Columns names, 349 Csv.Document, 349–353
Table.ColumnNames, 246 Excel.CurrentWorkbook, 357
Table.ColumnsOfType, 250, 251 Excel.Workbook, 353–355
Table.DemoteHeaders, 246, 247 Folder.Contents, 348, 349
Table.HasColumns, 249–251 functions, 347
Table.PrefixColumns, 251, 252 Pdf.Tables, 355, 356
Table.PromoteHeaders, 247–249 Power Query, 347
Table.RenameColumns, Sql.Database, 356, 357
252, 253 Data types, 46, 99, 157, 159,
Combinations of 179, 345
conditions, 363–367 binary value, 30
Combining tables function, 31
Table.Combine, 303–308 initial characters, 23
Table.Join, 308–316 list, 30
Table.NestedJoin, 317–320 logical values, 25
Constant values, 37, 38 null value, 25
Csv.Document, 349–353 numbers, 26, 27
in Power Query, 24, 25
record, 31
D samples, 24
Data conversion table, 31
data formats, 32 text, 30
date and time format, 35 time and date, 27–30
384
INDEX
385
INDEX
386
INDEX
387
INDEX
388
INDEX
389
INDEX
390
INDEX
391
INDEX
392