0% found this document useful (0 votes)
60 views1 page

Data Transformation With Power Query M in Power BI

This document discusses data transformation using Power Query M. Power Query M is the functional programming language used in Power Query for extract-transform-load operations. It allows importing and preparing data for use in Microsoft platforms like Power BI, Excel and Azure. The document defines Power Query and provides examples of functions for text, number, selection and filtering operations.

Uploaded by

namtunguyen6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views1 page

Data Transformation With Power Query M in Power BI

This document discusses data transformation using Power Query M. Power Query M is the functional programming language used in Power Query for extract-transform-load operations. It allows importing and preparing data for use in Microsoft platforms like Power BI, Excel and Azure. The document defines Power Query and provides examples of functions for text, number, selection and filtering operations.

Uploaded by

namtunguyen6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Text Operators Counting

Data Transformation with

"fish" & " & " & "chips" // Combine text with & // Access list elements with {}, zero-indexed
// Get the first few elements with FirstN()

Fruits{0} // 1st element; returns "apple"

List.FirstN(Fruits, 2) // Returns {"apple", null}

Power Query M in Power BI > Numbers // Accessing elements outside the range
throws an error

// Get the last few elements with LastN()

List.LastN(Fruits, 2) // Returns {null, "cherry"}

Fruits{3} // Throws an Expression.Error

Arithmetic // Get unique elements with Distinct()

// Append ? to return null if the index is List.Distinct(Menage) // Returns {1, -1, 0, 2,


Number.Power(3, 4) // Raise to the power with Number.Mod(22, 7) // Get the remainder after out of range
13, 80, 579}
Power()
division with Mod()
Fruits{3}? // Returns null
Learn Power BI at www.DataCamp.com Number.IntegerDivide(22, 7) // Integer divide Value.Equals(1.999999, 2, Precision.Double) //
a number with IntegerDivide() Check number close to equal with Equals()
Selection
Math functions
// Access list elements with {}, zero- // Get unique elements with Distinct()

>
indexed
List.Distinct(Menage) // Returns {1, -1, 0, 2, 13,
Definitions Number.Ln(10) // Calculate natural logarithm
with Ln()

Number.Abs(-3) // Calculate absolute values


with Abs()
Fruits{0} // 1st element; returns "apple"

80, 579}

Number.Exp(3) // Calculate exponential with Number.Sqrt(49) // Calculate the square root


Exp()
with Sqrt()
// Accessing elements outside the range // Get elements that match a criteria with Select()

Power Query is a tool for extract-transform-load (ETL). That is, it lets you import and prepare your data for use in Number.Round(12.3456, 2) // Round to n decimal Number.IsNaN(Number.NaN) // Returns true if throws an error
List.Select(Menage, each _ > 1) // Returns {2, 13,
Microsoft data platforms including Power BI, Excel, Azure Data Lake Storage and Dataverse.

places with Round() not a number Fruits{3} // Throws an Expression.Error

80, 579}

Power Query Editor is the graphical user interface to Power Query.

// Append ? to return null if the index is // Return true if all elements match a criteria with
out of range
MatchesAll()

Power Query M ("M" for short) is the functional programming language used in Power Query. 


> Text Values Fruits{3}? // Returns null

List.MatchesAll(Menage, each _ > 1) // Returns false

DAX is the other programming language available for Power BI. DAX is used for data analysis rather than ETL. Learn // Get the first few elements with FirstN()
// Return true if any elements match a criteria with
more about it in the DataCamp DAX cheat sheet. Creating text List.FirstN(Fruits, 2) // Returns {"apple", MatchesAny()

null}

List.MatchesAny(Menage, each _ > 1) // Returns true

// Text values are double-quoted, and can // Include control characters with #()

span multiple lines


"Split text with a tab character, #(tab), or start // Get the last few elements with LastN()
// Get value from list of length 1, or return
"M is a programming 
a new line with carriage-return line feed, List.LastN(Fruits, 2) // Returns {null, default, with SingleOrDefault()

language for ETL"

#(cr,lf)"

"cherry"} List.SingleOrDefault(Menage, -999) // Returns -999


// Embed quotes in strings by doubling them
// Embed # in strings with #(#)

"""M is magnificent"", mentioned Mike." "Hex codes for colors start with #(#)" Manipulation
An expression is a single formula that returns a value.

A query is a sequence of expressions used to define more complex data transformations. Queries are defined using let-
// Sort items in ascending order with Sort()
// Remove items by position with RemoveRange()

in code blocks. Creating text List.Sort(Menage) // Returns {-1, 0, 1, 1, 2, List.RemoveRange(Menage, 2, 3) // Returns {1,
13, 80, 579}

-1, 13, 80, 579}

// Text values are double-quoted, and can // Include control characters with #()

> Accessing M in Power BI span multiple lines

"M is a programming 

"Split text with a tab character, #(tab), or


start a new line with carriage-return line feed,
// Sort items in descending order

List.Sort(Menage, Order.Descending) // Returns


// Repeat elements with Repeat()

List.Repeat({"one", "two"}, 2) // Returns


language for ETL"

#(cr,lf)"

{579, 80, 13, 2, 1, 1, 0, -1}

{"one", "two", "one", "two"}

M code can be seen in Power Query Editor. In the ribbon, click on 'Transform data' to open the Power Query Editor.
// Embed quotes in strings by doubling them
// Embed # in strings with #(#)
// Reverse the order of items in a list with // Split list into lists of specified size with
"""M is magnificent"", mentioned Mike." "Hex codes for colors start with #(#)"
Reverse()
Split()

List.Reverse(Menage) // Returns {579, 80, 13, List.Split(Menage, 2) // Returns {{1, -1}, {0,
2, 1, 0, -1, 1}

1}, {2, 13}, {80, 579}}

Indexing
// Get non-null values with RemoveNulls()
// Flatten lists by removing 1 level of nesting
// Get the number of characters in text with Length()
// Get a substring with Middle()
List.RemoveNulls(Fruits) // Returns {"apple", with Combine()

Text.Length("How long will dinner be? About 25cm.") Text.Middle("Zip code: 10018", 10, 5) "cherry"} List.Combine({{"alpha"}, {"bravo", {"charlie",
"delta"}}}) // Returns {"alpha", "bravo",
{"charlie", "delta"}}
M code is shown in the Formula Bar. Splitting and combining text
Equality & membership
// Combine text, optionally separated with Combine()
// Split text on a delimiter with Split()

Text.Combine({"fish", "chips"}, " & ") Text.Split("fish & chips", " & ")
// Lists are equal if they contain the same elements in the same order

M code can also be seen in the Advanced Editor window. Click 'Advanced Editor' to open the Advanced Editor window.
{1, 2} = {1, 2} // true {1, 2} = {2, 1} // false {1, 2} = {1, 2, 3} // false
Mutating text
Calculations
// Convert text to upper case with Upper()
// Convert text to title case with Proper()

Text.Upper("iN cAsE oF eMeRgEnCy") // Returns Text.Proper("iN cAsE oF eMeRgEnCy") // Returns // Get the minimum value in a list with Min()
// Get the sum of values in a list with Sum()

"IN CASE OF EMERGENCY"

"In Case Of Emergency"

List.Min({0, 7, -3, 2, 1})

List.Sum({0, 7, -3, 2, 1})

// Convert text to LOWer case with Lower()


// Replace characters in text with Replace()

Text.Lower("iN cAsE oF eMeRgEnCy") // Returns Text.Replace("Have a nice trip", " n", "n ") // Get the minimum value in a list with Max()
// Get the product of values in a list with
"in case of emergency" // Returns "Have an ice trip" List.Max({0, 7, -3, 2, 1})

Product()

List.Product({0, 7, -3, 2, 1})

>
// Get quantile values from a list with
Creating values Type Conversion
Percentile()

List.Percentile(

// Get the mean of values in a list


List.Average({0, 7, -3, 2, 1})

with Average()

{0, 7, -3, 2, 1}, {0.25, 0.5, 0.75}, 

999 // Define a number

null // Define a null (missing value)

// Convert value to number with Number.From()


// Convert number to text with Number.ToText() [PercentileMode=PercentileMode.SqlDisc]
// Get the standard deviation of values in a list
Number.From(true) // Returns 1

— Formats: "D": integer digits, "E": ) with StandardDeviation()

true // Define a logical value

#date(2023, 12, 31) // Define a date with #date()

exponential, "F": fixed, G": general, "N": List.StandardDeviation({0, 7, -3, 2, 1})


// Dates and datetimes given as time in days number, "P": percent

"DataCamp" // Define a text value #datetime(2022, 9, 8, 15, 10, 0) // Define a datetime since 1899-12-30
Number.ToText(4500, "E") // Returns "4.5E3"

with #datetime()
Number.From(#datetime(1969, 7, 21, 2, 56, 0)) // Generation
Returns 25405.12

// Convert value to logical with


Logical.From()

// Convert text to number with Number.FromText()


Logical.From(2) // Generate random numbers between 0 and 1 // Generate a sequence of numbers with Numbers()

> Variables Number.FromText("4.5E3") // Returns 4500 with Random()

List.Random(3)

List.Numbers(1, 5, 2)

// Mimic a for loop with Generate()

Functions // Generate a sequence of dates with Dates()


List.Generate(

// Variables are assigned by writing a // By convention, variable names are


query with let-in
UpperCamelCase
List.Dates(#date(2023, 1, 1), 3, #duration(7, () => 2,

let
HeightM

0, 0, 0)) each _ < 20,

// Define a function with (args) => calculations

// Intermediate calculations
each Number.Power(_, 2))
let

TempF = 50
// Quote variable names and prefix with Hypotenuse = (x, y) => Number.Sqrt(Number.Power(x, 2) + Number.Power(y, 2))

TempC = 5 / 9 * (TempF - 32)


# for non-standard names

in

in
#"Height in Meters"
Hypotenuse
Set operations
// Resulting variable

// each is syntactic sugar for a function with 1 arg named _

TempC // Use it to iterate over lists and tables


// Get values in all inputs with Intersect()

each Number.Power(_, 2) // Same as (_) => Number.Power(_, 2) List.Intersect({{1, 3, 6, 10, 15}, {21, 15, 9, 3}, {0, 3, 6}}) // Returns {3}

> Operators // Get values in any inputs with Union()

Lists
List.Union({{1, 3, 6, 10, 15}, {21, 15, 9, 3}, {0, 6}}) Returns {0, 6,
>
3, // 1, 3, 9, 10, 15,
21}

Arithmetic operators
// Get value in one set but not the other with Difference()

102 + 37 // Add two numbers with +


4 * 6 // Multiply two numbers with *

Creation List.Difference({1, 3, 6, 10, 15}, {21, 15, 9, 3}) // Returns {1, 6, 10}
102 - 37 // Subtract a number with - 22 / 7 // Divide a number by another with /
// Define a list with {}
// Lists can be nested 

//You can include different data types including null


{"outer", {"inner"}} 

Numeric comparison operators {999, true, "DataCamp", null}

// Concatenate lists with &

3 = 3 // Test for equality with =


3 >= 3 // Test greater than or equal to with >=
// Define a sequence of numbers with m..n
{1, 4} & {4, 9} // Returns {1, 4, 4, 9}
<> 3 Test for inequality with <>
< 4 Test less than with <

Learn Power BI Online at


3 // 3 // {-1..3, 100} // Equivalent to {-1, 0, 1, 2, 3, 100}
3 > 1 // Test greater than with > 3 <= 4 // Test less than or equal to with <=

Example lists www.DataCamp.com


Logical Operators
let
let 

not (2 = 2) // Logical NOT with not


Fruits = {"apple", null, "cherry"}
Menage = {1, -1, 0, 1, 2, 13, 80, 579}

in 
in

(1 <> 1) and (1 < 1) // Logical AND with and

(1 >= 1) or (1 < 1) // Logical OR with or Fruits Menage

You might also like