Microsoft Excel Tutorials

Excel - M Language in Power Query

Excel - M Language in Power Query

M Language in Power Query 

Microsoft Excel's Power Query is a game-changer for data transformation and ETL (Extract, Transform, Load) processes. At the core of Power Query lies the M Language, a powerful, functional, and case-sensitive language designed specifically for data manipulation tasks. Understanding the M language can unlock a new level of data automation, customization, and transformation within Excel and Power BI.

This comprehensive guide covers the fundamentals of the M language in Power Query, its syntax, practical examples, advanced transformations, best practices, and optimization techniques. By the end of this tutorial, you’ll have the skills to leverage M Language effectively in Excel's Power Query Editor for data cleaning, reshaping, and analysis.

What is M Language in Power Query?

The M language, also known as the Power Query Formula Language, is used by Power Query to perform data extraction, transformation, and loading. It is functional in nature and designed to handle data from a variety of sources such as Excel files, databases, APIs, and web pages.

Unlike traditional Excel formulas, M is built for querying data and can manipulate entire tables, not just individual cells. M language is used behind the scenes whenever you apply steps in Power Query Editor, but you can also write and customize M scripts directly.

Why Learn M Language?

  • Customize transformations beyond GUI capabilities.
  • Create reusable and parameterized queries.
  • Efficiently clean and shape large datasets.
  • Integrate data from multiple and diverse sources.
  • Automate repetitive data processing tasks.

Basic M Language Syntax

Key Characteristics

  • Case-sensitive language.
  • Functions are defined using let ... in structure.
  • Data types are strictly enforced.

Basic Structure


let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FilteredRows = Table.SelectRows(Source, each [Sales] > 1000),
    SortedRows = Table.Sort(FilteredRows,{{"Date", Order.Ascending}})
in
    SortedRows

Explanation

  • let: Begins a sequence of steps.
  • Source: Loads data from an Excel Table named Table1.
  • FilteredRows: Filters rows where Sales > 1000.
  • SortedRows: Sorts the filtered data by Date ascending.
  • in: Returns the final result of SortedRows.

Common Data Types in M Language

  • Number: Represents numeric values.
  • Text: Represents text or string data.
  • Logical: Boolean values TRUE or FALSE.
  • List: Ordered sequences of items.
  • Record: A collection of fields like a dictionary.
  • Table: Structured data like Excel tables.
  • Function: Executable code logic that can be invoked with parameters.

Basic Examples of M Language

Example 1: Adding a Custom Column


let
    Source = Excel.CurrentWorkbook(){[Name="SalesData"]}[Content],
    AddColumn = Table.AddColumn(Source, "Bonus", each [Sales] * 0.10)
in
    AddColumn

Example 2: Removing Duplicates


let
    Source = Excel.CurrentWorkbook(){[Name="Employees"]}[Content],
    RemoveDuplicates = Table.Distinct(Source)
in
    RemoveDuplicates

Example 3: Changing Data Types


let
    Source = Excel.CurrentWorkbook(){[Name="SalesData"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source, {{"Date", type date}, {"Sales", type number}})
in
    ChangeType

Understanding Lists in M Language

Lists are single-column sequences of data in M Language.

Creating a List


{1, 2, 3, 4, 5}

Accessing Items in a List


myList{2}

Returns the third item in myList (since indexing starts at 0).

Working with Records

Records in M Language are similar to dictionaries in programming.

Example of Record


[
    Name = "Alice",
    Age = 30,
    Department = "Finance"
]

Accessing Record Fields


myRecord[Name]

Returns Alice.

Advanced M Language Functions

Table.SelectRows

Filters rows based on a condition.


Table.SelectRows(Source, each [Sales] > 1000)

Table.AddColumn

Adds a calculated column.


Table.AddColumn(Source, "Commission", each [Sales] * 0.05)

Table.Group

Groups data and performs aggregations.


Table.Group(Source, {"Department"}, {{"TotalSales", each List.Sum([Sales]), type number}})

Working with Parameters in M Language

Parameters in Power Query allow for dynamic queries.

Example of a Parameterized Query


let
    SalesThreshold = 500,
    Source = Excel.CurrentWorkbook(){[Name="SalesData"]}[Content],
    FilteredRows = Table.SelectRows(Source, each [Sales] > SalesThreshold)
in
    FilteredRows

Combining Multiple Queries

Using Table.Combine to merge tables:


let
    Table1 = Excel.CurrentWorkbook(){[Name="JanData"]}[Content],
    Table2 = Excel.CurrentWorkbook(){[Name="FebData"]}[Content],
    CombinedTables = Table.Combine({Table1, Table2})
in
    CombinedTables

Error Handling in M Language

To handle errors gracefully, you can use try ... otherwise:


let
    Source = try Excel.CurrentWorkbook(){[Name="MissingTable"]}[Content]
        otherwise Table.FromRows({}, {"Column1", "Column2"})
in
    Source

Debugging M Language Code

  • Step through the Applied Steps in Power Query Editor.
  • Use intermediate variables to check outputs at each step.
  • Leverage the Advanced Editor to trace and correct errors.

Best Practices for Writing M Language

  • Use meaningful variable names for each step.
  • Comment your code using // for clarity.
  • Avoid hardcoding; use parameters where possible.
  • Combine transformations efficiently to reduce processing time.
  • Test performance on large datasets before deployment.

Optimizing Power Query M Code

  • Filter data as early as possible.
  • Remove unnecessary columns to optimize memory.
  • Minimize the number of steps when possible.
  • Use native database queries when connecting to SQL sources.

Real-World Use Cases of M Language in Power Query

  • Data Cleaning: Removing duplicates, fixing data types, replacing nulls.
  • Data Transformation: Pivoting, unpivoting, and reshaping tables.
  • Data Merging: Joining tables from multiple sources.
  • ETL Automation: Creating parameterized data pipelines.
  • Data Aggregation: Grouping and summarizing sales data by regions.

The M Language in Power Query is an indispensable tool for anyone working with data in Excel or Power BI. Whether you are a data analyst, business intelligence professional, or Excel power user, mastering the M language can significantly enhance your data transformation capabilities. From basic filtering to complex data manipulation, M language empowers you to automate and optimize data processes efficiently within Excel’s ecosystem.

By combining M Language knowledge with Power Query's GUI and Excel formulas, you can create powerful, automated data models and analyses that drive better decision-making and insights.

Beginner 5 Hours
Excel - M Language in Power Query

M Language in Power Query 

Microsoft Excel's Power Query is a game-changer for data transformation and ETL (Extract, Transform, Load) processes. At the core of Power Query lies the M Language, a powerful, functional, and case-sensitive language designed specifically for data manipulation tasks. Understanding the M language can unlock a new level of data automation, customization, and transformation within Excel and Power BI.

This comprehensive guide covers the fundamentals of the M language in Power Query, its syntax, practical examples, advanced transformations, best practices, and optimization techniques. By the end of this tutorial, you’ll have the skills to leverage M Language effectively in Excel's Power Query Editor for data cleaning, reshaping, and analysis.

What is M Language in Power Query?

The M language, also known as the Power Query Formula Language, is used by Power Query to perform data extraction, transformation, and loading. It is functional in nature and designed to handle data from a variety of sources such as Excel files, databases, APIs, and web pages.

Unlike traditional Excel formulas, M is built for querying data and can manipulate entire tables, not just individual cells. M language is used behind the scenes whenever you apply steps in Power Query Editor, but you can also write and customize M scripts directly.

Why Learn M Language?

  • Customize transformations beyond GUI capabilities.
  • Create reusable and parameterized queries.
  • Efficiently clean and shape large datasets.
  • Integrate data from multiple and diverse sources.
  • Automate repetitive data processing tasks.

Basic M Language Syntax

Key Characteristics

  • Case-sensitive language.
  • Functions are defined using let ... in structure.
  • Data types are strictly enforced.

Basic Structure

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], FilteredRows = Table.SelectRows(Source, each [Sales] > 1000), SortedRows = Table.Sort(FilteredRows,{{"Date", Order.Ascending}}) in SortedRows

Explanation

  • let: Begins a sequence of steps.
  • Source: Loads data from an Excel Table named Table1.
  • FilteredRows: Filters rows where Sales > 1000.
  • SortedRows: Sorts the filtered data by Date ascending.
  • in: Returns the final result of SortedRows.

Common Data Types in M Language

  • Number: Represents numeric values.
  • Text: Represents text or string data.
  • Logical: Boolean values TRUE or FALSE.
  • List: Ordered sequences of items.
  • Record: A collection of fields like a dictionary.
  • Table: Structured data like Excel tables.
  • Function: Executable code logic that can be invoked with parameters.

Basic Examples of M Language

Example 1: Adding a Custom Column

let Source = Excel.CurrentWorkbook(){[Name="SalesData"]}[Content], AddColumn = Table.AddColumn(Source, "Bonus", each [Sales] * 0.10) in AddColumn

Example 2: Removing Duplicates

let Source = Excel.CurrentWorkbook(){[Name="Employees"]}[Content], RemoveDuplicates = Table.Distinct(Source) in RemoveDuplicates

Example 3: Changing Data Types

let Source = Excel.CurrentWorkbook(){[Name="SalesData"]}[Content], ChangeType = Table.TransformColumnTypes(Source, {{"Date", type date}, {"Sales", type number}}) in ChangeType

Understanding Lists in M Language

Lists are single-column sequences of data in M Language.

Creating a List

{1, 2, 3, 4, 5}

Accessing Items in a List

myList{2}

Returns the third item in myList (since indexing starts at 0).

Working with Records

Records in M Language are similar to dictionaries in programming.

Example of Record

[ Name = "Alice", Age = 30, Department = "Finance" ]

Accessing Record Fields

myRecord[Name]

Returns Alice.

Advanced M Language Functions

Table.SelectRows

Filters rows based on a condition.

Table.SelectRows(Source, each [Sales] > 1000)

Table.AddColumn

Adds a calculated column.

Table.AddColumn(Source, "Commission", each [Sales] * 0.05)

Table.Group

Groups data and performs aggregations.

Table.Group(Source, {"Department"}, {{"TotalSales", each List.Sum([Sales]), type number}})

Working with Parameters in M Language

Parameters in Power Query allow for dynamic queries.

Example of a Parameterized Query

let SalesThreshold = 500, Source = Excel.CurrentWorkbook(){[Name="SalesData"]}[Content], FilteredRows = Table.SelectRows(Source, each [Sales] > SalesThreshold) in FilteredRows

Combining Multiple Queries

Using Table.Combine to merge tables:

let Table1 = Excel.CurrentWorkbook(){[Name="JanData"]}[Content], Table2 = Excel.CurrentWorkbook(){[Name="FebData"]}[Content], CombinedTables = Table.Combine({Table1, Table2}) in CombinedTables

Error Handling in M Language

To handle errors gracefully, you can use try ... otherwise:

let Source = try Excel.CurrentWorkbook(){[Name="MissingTable"]}[Content] otherwise Table.FromRows({}, {"Column1", "Column2"}) in Source

Debugging M Language Code

  • Step through the Applied Steps in Power Query Editor.
  • Use intermediate variables to check outputs at each step.
  • Leverage the Advanced Editor to trace and correct errors.

Best Practices for Writing M Language

  • Use meaningful variable names for each step.
  • Comment your code using // for clarity.
  • Avoid hardcoding; use parameters where possible.
  • Combine transformations efficiently to reduce processing time.
  • Test performance on large datasets before deployment.

Optimizing Power Query M Code

  • Filter data as early as possible.
  • Remove unnecessary columns to optimize memory.
  • Minimize the number of steps when possible.
  • Use native database queries when connecting to SQL sources.

Real-World Use Cases of M Language in Power Query

  • Data Cleaning: Removing duplicates, fixing data types, replacing nulls.
  • Data Transformation: Pivoting, unpivoting, and reshaping tables.
  • Data Merging: Joining tables from multiple sources.
  • ETL Automation: Creating parameterized data pipelines.
  • Data Aggregation: Grouping and summarizing sales data by regions.

The M Language in Power Query is an indispensable tool for anyone working with data in Excel or Power BI. Whether you are a data analyst, business intelligence professional, or Excel power user, mastering the M language can significantly enhance your data transformation capabilities. From basic filtering to complex data manipulation, M language empowers you to automate and optimize data processes efficiently within Excel’s ecosystem.

By combining M Language knowledge with Power Query's GUI and Excel formulas, you can create powerful, automated data models and analyses that drive better decision-making and insights.

Related Tutorials

Frequently Asked Questions for Microsoft Excel

Go to View β†’ Freeze Panes to keep a row or column visible while scrolling.

Select data β†’ Click Insert β†’ Chart β†’ Choose a chart type (bar, line, pie, etc.).

=IF(A1>10, "High", "Low") returns "High" if A1 is greater than 10; otherwise, it returns "Low".

Relative (A1): Changes when copied.

Absolute ($A$1): Remains fixed when copied.

Select data β†’ Click Insert β†’ PivotTable β†’ Choose where to place it.

VLOOKUP: Searches vertically in columns.

HLOOKUP: Searches horizontally in rows.

VLOOKUP only searches left to right.
INDEX-MATCH is more flexible and allows searches in any direction.

Click File β†’ Save As, choose a location, enter a filename, and select a format (e.g., .xlsx, .csv).

Select column β†’ Click Data β†’ Text to Columns β†’ Choose delimiter (e.g., comma, space).

Use =SUM(A1:A5) to add values in the range A1 to A5.

Use =COUNTIF(A1:A10, ">50") to count numbers greater than 50 in A1:A10.

Select data β†’ Click Data β†’ Remove Duplicates.

Count numbers: =COUNT(A1:A10)

Count non-empty cells: =COUNTA(A1:A10)

Select cells β†’ Click Conditional Formatting in the Home tab β†’ Choose a rule (e.g., highlight values greater than 50).

#DIV/0! β†’ Division by zero error.
#VALUE! β†’ Invalid data type in formula.
#REF! β†’ Cell reference is missing or deleted.

Click the Pivot Table β†’ Click Refresh under the PivotTable Analyze tab.

Select a cell β†’ Data β†’ Data Validation β†’ Set rules (e.g., allow only numbers or dropdown lists).

Ctrl + C β†’ Copy
Ctrl + V β†’ Paste
Ctrl + Z β†’ Undo
Ctrl + Shift + L β†’ Apply/Remove filter
Ctrl + T β†’ Convert data to a table

Click Review β†’ Protect Sheet, set a password, and select allowed actions.

Excel is a spreadsheet software used for data analysis, calculations, graphing, and automation.

Check for typos in the formula.
Ensure cells referenced contain valid data.
Remove extra spaces in text values.

It searches for a value in the first column of a range and returns a value from another column.

Example: =VLOOKUP(101, A2:C10, 2, FALSE) looks up 101 in column A and returns the corresponding value from column 2.

Use =A1 & " " & B1 or =CONCATENATE(A1, " ", B1).

line

Copyrights © 2024 letsupdateskills All rights reserved