Reader small image

You're reading from  The Definitive Guide to Power Query (M)

Product typeBook
Published inMar 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781835089729
Edition1st Edition
Languages
Right arrow
Authors (3):
Gregory Deckler
Gregory Deckler
author image
Gregory Deckler

Greg Deckler is a 7-time Microsoft MVP for Data Platform and an active blogger and Power BI community member, having written over 6,000 solutions to community questions. Greg has authored many books on Power BI, including Learn Power BI 1st and 2nd Editions, DAX Cookbook, Power BI Cookbook 2nd Edition and Mastering Power BI 2nd Edition. Greg has also created several external tools for Power BI and regularly posts video content to his YouTube channels, Microsoft Hates Greg and DAX For Humans.
Read more about Gregory Deckler

Rick de Groot
Rick de Groot
author image
Rick de Groot

Rick de Groot was born in the Netherlands and has been working in BI for more than 14 years. He went freelance in 2016 and now works as an independent Power BI consultant. On his mission to make Power BI more accessible, he started two blogs: BI Gorilla and PowerQuery. how, and a YouTube channel sharing Power Query and Power BI content. His commitment to offering free content through multiple platforms has led him to earning the Microsoft Data Platform MVP award for two consecutive years.
Read more about Rick de Groot

Melissa de Korte
Melissa de Korte
author image
Melissa de Korte

Melissa de Korte's approach to facing challenges is fueled by relentless curiosity. She is a dedicated community member and content creator. Her portfolio includes blogs, tutorials, courses, and webinars, that make Power Query M more accessible and useful for all. Behind her professional persona lies a genuine dedication to empowering others through education and knowledge sharing, and a desire to encourage professionals to embrace the potential of Power Query, M.
Read more about Melissa de Korte

View More author details
Right arrow

Structured Values

In this chapter, we focus on an important concept in the Power Query M language: structured values. Whereas we introduced both values and data types in Chapters 4 and 5, structured values are more complex and require additional attention. Unlike simple or primitive values, they can include multiple other values within them. This complexity allows them to hold a range of primitive values, or even other structured values, opening up a wide range of possibilities for data manipulation and analysis.

The importance of structured values within the M language is such that we’re dedicating an entire chapter to them. The constructs you will learn not only contribute to more efficient and effective code, but also form the backbone of many operations within the M language.

This chapter covers the following topics:

  • Introducing structured values
  • Lists
  • Records
  • Tables

In each section, we will provide a clear explanation of the...

Introducing structured values

As we delve further into the M language, we arrive at one of the most important concepts: structured values. These values form the foundation of data shaping and manipulation in Power Query M. But what exactly do we mean when we say structured values? You can think of them as a container or package that can contain one or more values. These containers are organized in a way that allows us to perform transformations on them. As a developer, getting a thorough understanding of how to use these values allows you to perform challenging data transformations more easily.

Structured values allow us to deal with complexity by splitting data into manageable chunks. Each structured value – be it a list, record, table, or function – has its unique characteristics and applications, and understanding when to use each helps in writing effective code.

As we go through this chapter, you might find some concepts slightly more challenging than others...

Lists

Beginning our exploration of structured values, we first encounter lists. So, what is a list?

Introduction to lists

A list contains a sequence of comma-separated values of any type. That includes primitive values (like text, numbers, dates, or times) and structured values (lists, records, or tables).

So why should lists interest you to begin with? You will find that lists are used extensively throughout Power Query. For example, selecting a column returns its values in the form of a list. Similarly, when functions take multiple values as input (like List.Count) or return multiple values as output (like Table.ColumnNames), you will often find that these functions involve lists. Let’s have a look at how you can create a list.

Creating a list is a straightforward process. You can define a list value by enclosing values in curly brackets { }, formally known as list initialization. Each value within the list is separated by a comma.

For instance, you can...

Records

Next on our journey through structured values in Power Query’s M language, we encounter records.

Introduction to records

A record is a named list of values. While lists are a simple, ordered collection of values, records associate a field name or key to each value. This leads to a more complex, yet organized data structure.

You can think of a record as a single row in a table, where each field in the row has a unique column name and a value. Just like lists, records can contain any type of value, be it primitive (such as text, numbers, or dates), or structured (like lists, records, or tables).

You can create a record with the record initialization operator. This involves specifying pairs of keys and values within square brackets [], also known as record initialization. What’s important here is to remember that:

  • Every record is surrounded by square brackets.
  • A key (the field name) is followed by an equal sign (=). Records allow...

Tables

Now that we’ve taken a deep dive into lists and records, it’s time to extend our understanding to table values. As with lists and records, tables hold an important place in the M language due to their role in structuring and organizing data. So, what is a table?

Introduction to tables

At its most basic, a table is a structured value that arranges data in rows and columns. You could also say that a table is a list of records where each record represents a row of the table. Because of its two-dimensional form, it is easy to preview your data in a table and you will find that most queries that are loaded into Power BI or Excel have a table value as output. So how do you create a table?

Creating a table in Power Query’s M language is often done by calling an accessing data function as described in Chapter 3. For instance, when you import an Excel or CSV file, Power Query automatically creates a table for you.

However, to illustrate the characteristics...

Summary

In this chapter, we explored lists, records, and tables, showing how important they are when you’re using the M language.

We learned that structured values serve as containers holding one or more primitive or structured values. Learning about them is useful in many different areas. For instance, lists and records are often used to provide multiple items in function arguments and they help in simplifying your code by using them. Record structures are also great structures in which you can create variables.

We also investigated how to access items from the different structured values through selection and projection, enabling you to easily extract values. You will find that these skills help you understand code created by the user interface, but also help you create shorter code yourself.

We then looked at the creation of these values and how operators work on them. You also learned about complex data types, highlighting their importance and how to assign...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Definitive Guide to Power Query (M)
Published in: Mar 2024Publisher: PacktISBN-13: 9781835089729
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Gregory Deckler

Greg Deckler is a 7-time Microsoft MVP for Data Platform and an active blogger and Power BI community member, having written over 6,000 solutions to community questions. Greg has authored many books on Power BI, including Learn Power BI 1st and 2nd Editions, DAX Cookbook, Power BI Cookbook 2nd Edition and Mastering Power BI 2nd Edition. Greg has also created several external tools for Power BI and regularly posts video content to his YouTube channels, Microsoft Hates Greg and DAX For Humans.
Read more about Gregory Deckler

author image
Rick de Groot

Rick de Groot was born in the Netherlands and has been working in BI for more than 14 years. He went freelance in 2016 and now works as an independent Power BI consultant. On his mission to make Power BI more accessible, he started two blogs: BI Gorilla and PowerQuery. how, and a YouTube channel sharing Power Query and Power BI content. His commitment to offering free content through multiple platforms has led him to earning the Microsoft Data Platform MVP award for two consecutive years.
Read more about Rick de Groot

author image
Melissa de Korte

Melissa de Korte's approach to facing challenges is fueled by relentless curiosity. She is a dedicated community member and content creator. Her portfolio includes blogs, tutorials, courses, and webinars, that make Power Query M more accessible and useful for all. Behind her professional persona lies a genuine dedication to empowering others through education and knowledge sharing, and a desire to encourage professionals to embrace the potential of Power Query, M.
Read more about Melissa de Korte