Power Query
Introduction to M-Language
1
Introduction
What is Power Query?
An ETL (Extract, Transform, Load) tool integrated into Power BI and Excel.
2
Introduction
What is the M Language?
A functional programming language used in Power Query to transform
data.
Do I Have to learn M-Language?
3
The Role of M Language in Power Query
Microsoft has embedded a User Interface (UI) to allow you to perform
different kinds of transformations to your data
• In ease,
• With speed, and
• Without having to write any code.
This UI already uses M-Language in the background.
So, Why Learn M Language?
4
The Role of M Language in Power Query
• M-Language enables advanced data transformations that are not
available in the UI.
• Provides flexibility and control for complex data manipulation.
• It allows creating reusable logic and custom transformations.
• Optimizes performance for larger datasets.
5
The Role of M Language in Power Query
Example:
Power Query’s UI can remove duplicates based on a whole column or
specific columns.
But what if you want to remove duplicates based on a more complex
condition, such as keeping the row with the latest date for each unique
item?
Let’s try it.
6
The Role of M Language in Power Query
Product ID Date Sales
Example: Prod 101 1/1/2024 280
Prod 101 1/2/2024 208
You have a table of transactions, Prod 101 1/3/2024 345
and each transaction has a Prod 101
Prod 101
1/4/2024
1/5/2024
385
288
product ID and a transaction date. Prod 101 1/6/2024 492
Prod 101 1/7/2024 257
For each product, you want to Prod 101 1/8/2024 343
keep only the row with the most Prod 101
Prod 101
1/9/2024
1/10/2024
484
345
recent transaction date. Prod 102 1/1/2024 385
Prod 102 1/1/2024 425
Prod 102 1/1/2024 260
Prod 102 1/1/2024 221
Prod 102 1/1/2024 395
7
The Role of M Language in Power Query
8
The Role of M Language in Power Query
Explanation:
Sorted Table: Sorts the table by ProductID in ascending order and
Transaction Date in descending order (so the latest date appears first
for each product).
Remove Duplicates: Removes duplicates based on the ProductID
column, keeping only the first occurrence of each (which is now the
most recent due to the sorting).
9
M Language Fundamentals
Functional Language Characteristics:
•Functions are the primary building blocks.
•Each function takes inputs (arguments) and returns a value.
Case Sensitivity:
•M language is case-sensitive.
10
Syntax Overview
• “let...in” statements structure code in M.
• The “let” statement stores intermediate results.
• Each intermediate results has a name and must end with a comma “,”.
• Each step is applied to the previous step, so the order of steps is really
important.
• You can insert or remove steps in the middle of the applied steps, but
you have to be aware of the consequences.
• The “In” statement returns the end result of the code.
11
M-Language
Commonly Used Functions
12
Example
• A CSV file containing the sales
data for a manufacturer needs
to be analyzed.
• The data contains some
problems and needs to be
transformed in the power
query.
• i.e. numbers stored as text,
leading spaces, insufficient
categorization, etc.
13
Collect Data
14
Promoted Headers
15
Changed Data Type
16
Number.FromText
• Use Case:
Convert text to numbers (useful for cleaning up mixed data
types).
• Example:
Number.FromText([Sales])
• Explanation:
Converts the "Sales" column from text to numbers for
calculations.
17
Number.FromText
18
Number.Round
• Use Case:
Round numbers to a specified number of decimal places.
• Example:
Number.Round([Modified Sales], 2)
• Explanation:
Rounds the modified sales value to two decimal places.
19
Number.Round
20
if...then...else
• Use Case:
Apply conditional logic to assign values based on criteria.
• Example:
if [Rounded Sales] > 2500 then "High" else "Low“
• Explanation:
Adds a new column that labels sales greater than 2500 as
"High" and anything else as "Low."
21
if...then...else
22
Text.End
• Use Case:
Extract the first few characters of a text string.
• Example:
Text.End([Product-ID], 3)
• Explanation:
Extracts the last three characters of the "Product-ID" column.
23
Text.End
24
Text.Replace
• Use Case:
Replace a specific part of a text string.
• Example:
Text.Replace([Product], "Old", "New")
• Explanation:
Replaces the word "Old" with "New" in the "Product" column.
25
Text.Replace
26
Text.Trim
• Use Case:
Remove leading and trailing spaces from text.
• Example:
Text.Trim([Customer-Name])
• Explanation:
Cleans up the "Customer-Name" field by removing extra spaces.
27
Text.Trim
28
Text.Proper
• Use Case:
Capitalize the first letter of each word in a text string.
• Example:
Text.Proper([New Customer Name])
• Explanation:
Converts the "CustomerName" to proper case (e.g., "john
smith" becomes "John Smith").
29
Text.Proper
30
Text.Length
• Use Case:
Calculate the length of text in a column.
• Example:
Text.Length([CustomerName])
• Explanation:
Adds a new column with the number of characters in the
"CustomerName" field.
31
Text.Length
32
List.Sum
• Use Case:
Summing up a list of values (for example, from multiple columns).
• Example:
List.Sum({[Retail_discount], [WholeSales_discount], [Online_discount]})
• Explanation:
Adds up the values from the " Retail_discount," " WholeSale _discount," and
"Online _discount " columns.
33
List.Sum
34
Date.AddDays
• Use Case:
Add or subtract days from a date.
• Example:
Date.AddDays([Order Date], 7)
• Explanation:
Adds 7 days to the "Order Date" column to calculate delivery
dates.
35
Date.AddDays
36
Date.Year
• Use Case:
Extract the year from a date.
• Example:
Date.Year([Order Date])
• Explanation:
Extracts the year from the "Order Date" column to create a
"Year" column.
37
Date.Year
38
Any Questions ?
39
Thank You!
40