Power Query series (05) - M Language Introduction

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/stone0823/article/details/100176413

M or M language query language is called Power Query (referred to as PQ) hero behind the scenes. It is said that Power Query Editor visualization PQ 80% of the functionality can be achieved, so from the operational level, most people do not learn M language, but to learn and master the language is undoubtedly more powerful M at the time of data processing, but also, some of M the foundation also can deepen the understanding of the PQ procedures, and thus it is possible to do some of the steps to optimize and improve these applications. In general, M is a worth to learn and master the skills, Excel and Power BI can be used.

Create an empty query (blank query)

We are all connected in front of an external data source, and then double-click the query name Excel Sheet the right to enter the Power Query query editor, describes how today by creating an empty query into the Query Editor of method. There are two ways to create an empty query:

  • Method One: Switch to the [data] tab, then [Get Data] - [from other sources] - [blank] Query way, Excel creates a blank query, open and enter into Power Query query editor.
  • Method two: first get through the [data] - [start] Power Query query editor into Power Query query editor interface, and then create an empty query editor interface.

Senior Editor

M language code after entering the query editor, [Home] and [View two tabs, have a "senior editor" button, click to enter. For inquiries is empty, after entering the interface is like the following:


This article, we will pass this senior editor, to understand the basics of M language. We assume that you already have concepts from other programming languages, if you do not have experience using other programming languages, it is recommended reference books or other articles look at the concept of programming languages, such as variables, statements, programming language structure and so on.

let statement

Understand, the main role of let statement is in line with PQ editor, easy to see the formula and the results of the query in each step. For example, we write in the Advanced Editor to write a simple statement:


Click through to press the button, the editor displays the following:


The right step application , the editor to a, b, and result as a step, a step wherein a click of the middle column of the formula (Formula bar), respectively, showing a step of this formula.

let statement can be very complicated, but I understand this should not be much of a problem.

Identifier naming convention

Power Query M is a form of freedom, but case-sensitive language, so naming identifier should be case-sensitive, in addition, other restrictions is relatively small:

  • In non-digital, non-symbolic beginning (underscore (_) is excluded), letters or characters may be
  • The name can only be split with an underscore (_) and dot (.), The symbol appears if other identifiers before identifier plus #symbols, such as#year/month

type of data

M language understanding, should start from the data type. M data types can be divided into basic types and container types, basic types classified as follows:

Text (text)

Text M language to guide the double quotation marks, such as

"Hello World"

Text only because M is guided by double quotation marks, when contained in double quotes if Text is required before the quotes plus a double quotation marks, the same VBA. For example, in order to express: Tom said: "I love playing football very much", this sentence contains two double quotation marks, into the M language:

result = "Tom  said: ""I love playing football very much"""

At the end, because the Text itself needs to contain double quotation marks, so much behind the double quotation marks and two double quotation marks, it seems really confusing.

M languages ​​have three escape character (escape sequence)

-cr:	Carriage Return
-lf:	Line Feed
-tab:	Tab

If desired wrap in Text, tab key, etc., used #(escape_sequence)to express. such as:

"Hello#(tab)Alice"
"Hello#(cr)#(lf)World"

A wrap, even to use the two # sign, in order to simplify the continuous transfer of the following characters can be expressed this way:

// 与 "Hello#(cr)#(lf)World" 相同
“Hello#(cr, lf)World”

With the text between the &connection:

let
    count = 5,
    result = "You have " & Text.From(count) & "  times left"
 in 
    result

When performing the M language text connection, not implicit conversion (implicit conversion). Numbers can be Text.From()converted to text function.

Number

Digital literals can be decimal, hexadecimal may be. The following are some examples of numerical:

5 	// 整数
0
0.5  // 小数
1.5e+8  // 指数形式
1.6e-6
1.6E-6  // e 可以大写或者小写
.5  // 合法省略小数点
0xff  // 16 进制, 字母可以大写或者小写
0XFF

Special number

M language has the following special numbers:

#infinity  // produced by an expression like 1/0
#nan       // produced by an expression like 0/0
-#infinity // produced by an expression like -1/0

Date type more knowledge, let alone a bar.

Guess you like

Origin blog.csdn.net/stone0823/article/details/100176413