Data Types
Kind Literal Comment
Empty value, void
1 * null = null // be careful!
Null null
Logical true / false
Number 0 1 -1 1.5 2.3e-5, 0xff Whole / decimal number, number in hex
#time( hour, minute, second )
#time(24,0,0) = #time(0,0,0)
If hour is 24, then minute and second must be 0
Time #time(9, 15, 0) 0 ≤ hour ≤ 24, 0 ≤ minute ≤ 59, 0 ≤ second ≤ 59
Date #date(2013, 2, 26) #date( year, month, day)
DateTime #datetime(2013, 2, 26, 9, 15, 0) #datetime( year, month, day, hour, minute, second )
#datetimezone( year, month, day, hour, minute,
second, offset-hours, offset-minutes )
0 ≤ year ≤ 9999, 0 ≤ month ≤ 12, 1 ≤ day ≤ 31
#datetimezone(2013, 2, 26, 9, 0 ≤ hour ≤ 23, 0 ≤ minute ≤ 59, 0 ≤ second ≤ 59
DateTimeZone 15, 0, 9, 0) -14 ≤ offset-hours + offset-minutes / 60 ≤ 14
Duration #duration( 0, 1, 30, 0) #duration( days, hours, minutes, seconds )
Just text in quotes
Special characters
=”#(cr,lf)” same as =”#(cr)#(lf)”,
string to check =”a#(cr,lf)b”
= “a#(tab)b” // a b
= “a” & “b””c” // ab”c
Text “hello”
Binary #binary(“AQID”) If you work with binary – you know
{ 1, 2, 3 }, { 1 .. 10 },
{“A”..”Z”, “a”..”z”}
List Comma separated values in curly brackets
Comma separated “Field Name = Value” in square
Record [ A=1, B=2 ] brackets
Table Simple way: result:
#table( { “X”, “Y” }, { { 1, 2 }, { 3,
4}})
Preferable: with specified
column types
#table( list of field names,
#table( type table
list of lists with values for rows of future table )
[Digit = number, Name = text], #table( { “Field1 Name”, “Field2 Name” },
{ {1,”one”}, { { “Field1 Value1”, “Field2 Value1” },
{2,”two”}, { “Field1 Value2”, “Field2 Value2” },
{3,”three”} } ) { “Field1 Value3”, “Field2 Value3” } } )
Empty table: #table( {“A”, “B”}, {} )
( arguments ) => some operations.
“nullable” is enough to make argument optional.
“optional” is enough to let null be passed to a
function.
(num as nullable number) =>
let
step1 = if num = null then 0 else
num,
(x) => x + 1 step2 = step1 * 2
in general:
in
(optional Num as nullable
number) => Num + 1 step2
Function
type{ number } // list
type table [A = any, B = text]
Type
Operator Result x=y Equal
x>y Greater than x<>y Not equal
x >= y Greater than or equal x or y Conditional logical OR
x<y Less than x and y Conditional logical AND
x <= y Less than or equal not x Logical NOT
PowerQuery code shortcuts
IF / THEN / ELSE
Result = if T>0 then A else B // low case if / then / else, M is case sensitive
TRY / CATCH – error handling
Result = try A/B otherwise 0 // low case “try [some action] otherwise [some action/object]”
Excel cell value (Named Range consisting of one cell)
Result = Excel.CurrentWorkbook(){[Name=”CELLNAME”]}[Content]{0}[Column1]
Rename Columns according to “Renaming Table”
Renamed_Columns = Table.RenameColumns(TARGET,
Table.ToColumns(Table.Transpose(RENAMING_TABLE)), MissingField.Ignore),
where RENAMING_TABLE looks like (headers do not matter)
Old Name New Name
A B
C D
Create a table from thin air
For example, when response is null but you want to keep structure of your PowerPivot table
= #table( {“A”, “B”}, {} ) – empty table, simple approach
Or with defined column types
= #table( type table [A = text, B = number], {} ) – empty table
= #table( type table [A = text, B = number], { {“one”, 1}, {“two”, 1} } )
ISNUMBER() analog
= Value.Is(Value.FromText( VALUE ), type number)
Or:
= “sample” is number // false, = 123 is number // true
ISTEXT() analog
= Value.Is(Value.FromText( VALUE ), type text)
Or:
= “sample” is text // true, = 123 is text // false
Expressions
“Hello World” // a text value
123 // a number
Recursion
1 + 2 // sum of two numbers [ Factorial = (n) =>
{1, 2, 3} // a list of three numbers if n <= 1 then
[ x = 1, y = 2 + 3 ] // a record containing two fields: x and y 1
(x, y) => x + y // a function that computes a sum else
if 2 > 1 then 2 else 1 // a conditional expression n * @Factorial(n – 1),
x = Factorial(5)
let x = 1 + 1 in x * 2 // a let expression
]
error “A” // error with message “A”
// @ is scoping operator
Operations with date and time in Power Query
Time
#time( hour, minute, second )
Operator Left Operand Right Operand Meaning
x+y time duration Date offset by duration
x+y duration time Date offset by duration
x–y time duration Date offset by negated duration
x–y time time Duration between dates
x&y date time Merged datetime
Date
#date( year, month, day)
Operator Left Operand Right Operand Meaning
x+y date duration Date offset by duration
x+y duration date Date offset by duration
x–y date duration Date offset by negated duration
x–y date date Duration between dates
x&y date time Merged datetime
DateTime
#datetime( year, month, day, hour, minute, second )
Operator Left Operand Right Operand Meaning
x+y datetime duration Datetime offset by duration
x+y duration datetime Datetime offset by duration
x–y datetime duration Datetime offset by negated duration
x–y datetime datetime Duration between datetimes
Duration
#duration( days, hours, minutes, seconds )
#duration(0, 0, 0, 5.5) // 5.5 seconds
#duration(0, 0, 0, -5.5) // -5.5 seconds
#duration(0, 0, 5, 30) // 5.5 minutes
#duration(0, 0, 5, -30) // 4.5 minutes
#duration(0, 24, 0, 0) // 1 day
#duration(1, 0, 0, 0) // 1 day
Operator Left Operand Right Operand Meaning
x+y datetime duration Datetime offset by duration
x+y duration datetime Datetime offset by duration
x+y duration duration Sum of durations
x–y datetime duration Datetime offset by negated duration
x–y datetime datetime Duration between datetimes
x–y duration duration Difference of durations
x*y duration number N times a duration
x*y number duration N times a duration
x/y duration number Fraction of a duration
Main source of info is M language specification: https://msdn.microsoft.com/en-
us/library/mt807488.aspx