Basics of M: Power Query Formula Language (2024)

M is the powerful language behind the scene of Power Query. Any transformation you apply will be written in M language. For many, M looks like a scary language. In this post I like to explain a bit of basics of M. Not mentioning any functions. Mainly I want to explain to you how the M language syntax is structured. Once you know the syntax, then everything becomes simple. M is a language that you can learn it’s syntax easily. As a Power Query developer; I highly recommend you to spend time on M, because there are MANY operations that you can with M, but you might not be able to do it simply with the graphical interface. If you would like to learn more about Power BI, read Power BI book from Rookie to Rock Star.

M is informal name of Power Query Formula Language. The formal name is so long that no one uses that, everyone call it M! M stands for Data Mashup, some say stands for Data Modeling. M is a functional language, and it is important to know functions of it. However, each language has a structure and syntax which is the beginner level of learning that language. In this post, I will explain the syntax of M. Before learning M, I would like you to read this sentence loud;

M is much more powerful than the graphical interface of Power Query

Yes, you read it correct! The graphical interface of Power Query is changing every month. Every month new functionality comes to this graphical interface. But the fact is all of these functionalities has been in the language for many years! if you knew the language, you could easily use them, instead of waiting for graphical interface option for it. There are heaps of examples for it. one very small example is here: you can extend your Grouping ability in Power Query with a very minor change in M script of it.

Syntax of this language is simple. it always has two blocks of programming: LET expression block, and IN expression block. Here is the most simple M syntax;

let x=1in x

let and in are reserved words. before going even one step further, the first and foremost thing you need to know;

M (Power Query Formula Language) is Case Sensitive. There is a difference between x and X.

what are these two programming blocks:

let: definition of all variables

in: output! Yes, in actually means out! just named as in. everything you put in this block will be the output of your query.

So basically, the query below means defining a variable named as x, assigning the value 1 to it, and showing it as the result set. so the query will return 1.

to run this example, you need to Open Power BI Desktop. Go to Get Data, start with New Blank Query.

then in View tab, select advanced Editor;

Make sure when you write the script that you put reserved words such as let and in all lowercase. also your variable name should be the same case in both let and in section.

As you can see, there is no need to define data types for variable. it will be automatically assigned when the first assignment occurs.

If you specify a text value, then variable would be a text data type automatically.

Lines of codes in M continues if you don’t put the end of the line character.

As you can see in above example, the line continues, and x will be equal to x=1+1 . If you want to put an end for a line use comma(,). example here:

every line needs a comma(,) to finish. except the last line before in.

Name of variables can be all one word, like Source. or it can has spaces in it. in case that you have some characters such as space, then you need to put the name inside double quote (“) and put a hashtag at the beginning of it(#). something similar to:

#"This is a variable name"

Variable name can contain special characters, here is an example:

Special characters

Variable names can have special character, as you can see below variable has all types of characters in it and still runs good.

Escape character

Double quote (“) is escape character. you can use it to define variables with names that has another double quote in it. here is an example:

first double quote (highlighted) above is necessary to be before the second double quote (which is part of the variable name).

Power Query is a step by step transformation. Every transformation usually happens in a step. While you are writing the code, you can also notice that in the right hand side, you will see every variable forms a step.

in screenshot above, you can see every variable is determined as a step. and if the variable has space in the name, it will show it with spaces in list of applied steps.

The last variable is always specified in the in section.

There are different ways of defining every literal in Power Query. For example, if you want to define a date variable, here is how to do it;

for defining all other types of literals, here is the reference table to use:

* for function and type; I’ll write another post later to explain how these types works.

M is a functional language, and for doing almost everything you need to call a function for it. functions can be easily called with name of the function and specifying parameters for it.

screenshot above uses Date.Year function which fetch year part of a date. Functions names starts always with capital letters: Date.Year()

like any programming language, you can put some comments in your code. it can be in two forms;

Single line commentary with double slash (//)

Multi line commentary between slash and starts (/* comments */)

Now that you know some basics, let’s look at an existing query in advanced editor mode and understand it.

in screenshot above, you can see all basics mentioned so far:

  1. let and in block
  2. variable names matching steps applied in the query
  3. some variable names with hashtag and double quote: #”var name”
  4. end of the line characters: comma
  5. calling many functions

There are still many parts of this query that you might not understand. specially when using functions. you need to learn what functions are doing in order to understand the code fully. I have written a blog post, that explains how to use #shared keyword to get documentation of all functions in Power Query.

In next posts, I’ll explain another levels of structures in M.

Basics of M: Power Query Formula Language (17)Basics of M: Power Query Formula Language (18)Basics of M: Power Query Formula Language (19)Basics of M: Power Query Formula Language (20)

Reza Rad

Trainer, Consultant, Mentor

Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.

Basics of M: Power Query Formula Language (2024)

FAQs

What is Power Query m formula language? ›

A core capability of Power Query is to filter and combine, that is, to mash-up data from one or more of a rich collection of supported data sources. Any such data mashup is expressed using the Power Query M formula language. The M language is a functional, case sensitive language similar to F#.

What coding language does Power Query use? ›

The M language is the data transformation language of Power Query. Anything that happens in the query is ultimately written in M. If you want to do advanced transformations using the Power Query engine, you can use the Advanced Editor to access the script of the query and modify it as you want.

How do you use M code in Power Query? ›

Create a query with the Power Query editor

To create an M query in the advanced editor, you follow this basic process: Create a series of query formula steps that start with the let statement. Each step is defined by a step variable name. An M variable can include spaces by using the # character as #"Step Name" .

What is the difference between DAX and M query? ›

DAX and Power Query (M) serve different roles in the data preparation and modeling process in Power BI. DAX is primarily used for data modeling and creating calculated columns and measures, whereas Power Query (M) is used for data transformation and loading data from external sources.

Should I learn M language? ›

M Query is a simple language with a simple syntax. We strongly advise you to spend time on M Query because there are so many operations that you can perform with M Query that you might not be able to do with the graphical interface and the dashboard. M is an abbreviation for Power Query Formula Language.

Is m code similar to SQL? ›

No, Power Query does not use SQL. Power Query uses its own language called M. However, it can connect to SQL databases and retrieve data using SQL queries. Additionally, Power Query has a feature called 'Query Folding' which transforms the steps you create into native SQL to optimize performance.

What is the M language based on? ›

It is a mashup language (hence the letter M) designed to create queries that mix together data. It is similar to the F Sharp programming language, and according to Microsoft it is a "mostly pure, higher-order, dynamically typed, partially lazy, functional language." The M language is case-sensitive.

Is Power Query outdated? ›

Power Query add-in deprecation

Early in the summer of 2019, we officially deprecated the Power Query add-in which is required for Excel 2010 and 2013 for Windows.

What is the difference between formula and Power Query? ›

Large Datasets: Power Query is well-suited for handling large datasets, as it performs transformations directly on the source data before loading it into Excel. This can help improve performance and reduce memory usage compared to Excel formulas.

How do I convert Excel to Power Query? ›

There are two ways to do this.
  1. Select Data > Get Data > From Other Sources > Blank Query.
  2. Select Data > Get Data > Launch Power Query Editor.

What are the two main blocks that make up an m code query? ›

M as a Query Language
  • 'let' The let block defines and calculates and compiles all variables.
  • 'in' The 'in' block is the output of all the calculations. Yes, it sounds strange that 'in' means output, but that is exactly what it is. ...
  • End of line. A comma ends a line of code in M.
Mar 22, 2020

What are functions in Power Query M language? ›

In the Power Query M formula language, a function is a mapping from a set of input values to a single output value. A function is written by first naming the function parameters, and then providing an expression to compute the result of the function. The body of the function follows the goes-to (=>) symbol.

What is the m programming language? ›

The M programming language is a structured, imperative programming language with powerful introspection and indirection facilities. Easy to learn and write, it enables quick developer productivity.

What is Power Query M function? ›

In the Power Query M formula language, a function is a mapping from a set of input values to a single output value. A function is written by first naming the function parameters, and then providing an expression to compute the result of the function.

What is the m language function to sort a column? ›

Table. Sort is a Power Query M function that sorts a table using a list of one or more column names and optional comparisonCriteria. The function returns a sorted table.

References

Top Articles
Iran’s hard-line parliament speaker emerges as the theocracy’s top figure in the presidential vote
Collapsing Buildings and Roadways
Fighter Torso Ornament Kit
Encore Atlanta Cheer Competition
AllHere, praised for creating LAUSD’s $6M AI chatbot, files for bankruptcy
Nehemiah 4:1–23
Voordelige mode in topkwaliteit shoppen
Unitedhealthcare Hwp
South Park Season 26 Kisscartoon
Mileage To Walmart
Us 25 Yard Sale Map
Ub Civil Engineering Flowsheet
Southland Goldendoodles
Crusader Kings 3 Workshop
Voyeuragency
Leeks — A Dirty Little Secret (Ingredient)
Jvid Rina Sauce
Nashville Predators Wiki
Who called you from +19192464227 (9192464227): 5 reviews
Saatva Memory Foam Hybrid mattress review 2024
3476405416
Craigslist Pet Phoenix
Indystar Obits
Lista trofeów | Jedi Upadły Zakon / Fallen Order - Star Wars Jedi Fallen Order - poradnik do gry | GRYOnline.pl
Iroquois Amphitheater Louisville Ky Seating Chart
Surplus property Definition: 397 Samples | Law Insider
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
The Eight of Cups Tarot Card Meaning - The Ultimate Guide
Phoenixdabarbie
Little Einsteins Transcript
Evil Dead Rise - Everything You Need To Know
Kristen Hanby Sister Name
Kaiserhrconnect
Calculator Souo
Att U Verse Outage Map
Whas Golf Card
Joplin Pets Craigslist
Ewwwww Gif
Craigslist Lakeside Az
The Syracuse Journal-Democrat from Syracuse, Nebraska
One Main Branch Locator
Join MileSplit to get access to the latest news, films, and events!
St Anthony Hospital Crown Point Visiting Hours
Exam With A Social Studies Section Crossword
Gamestop Store Manager Pay
Whitney Wisconsin 2022
Lesly Center Tiraj Rapid
Egg Inc Wiki
Mikayla Campinos Alive Or Dead
Understanding & Applying Carroll's Pyramid of Corporate Social Responsibility
Powah: Automating the Energizing Orb - EnigmaticaModpacks/Enigmatica6 GitHub Wiki
Cbs Scores Mlb
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 6144

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.