top of page
  • Writer's pictureHarini Mallawaarachchi

Mastering M Language

Are you ready to take your M language skills to the next level? Whether you're a seasoned M developer or just starting out, this comprehensive guide is here to help you unlock the full potential of M language. From essential code snippets to advanced tips, we've got you covered!


Understanding M Language and Power Query


M Language, also known as Power Query Formula Language, is a powerful tool used for data transformation and manipulation in Microsoft Power Query. It's a versatile scripting language that empowers users to transform raw data into insightful visualizations. Whether you're working with Excel, Power BI, or other Microsoft tools, mastering M Language can significantly enhance your data processing capabilities.

In this guide, we'll take you through the fundamental concepts of M Language and provide you with practical code snippets to solidify your understanding.


What is M Language?

M Language is a functional language developed by Microsoft for data transformation and manipulation. It's primarily used in Power Query, a data connectivity and transformation tool available in Excel, Power BI, and other Microsoft products. M Language allows users to define custom data extraction, transformation, and loading (ETL) operations.


Basic Syntax and Structure

M Language uses a step-based approach, where each step represents a data transformation operation. Steps are defined using the "let" keyword followed by an expression. Here's a simple code snippet:

let
    Source = Excel.CurrentWorkbook(),
    FilteredRows = Table.SelectRows(Source, each [Sales] > 1000),
    TransformedData = Table.TransformColumns(FilteredRows, {"Price", each _ * 1.1})
in
    TransformedData

Connecting SQL Server DB:

let
    Source = Sql.Databases("azbicfgsql1"),
    IFSDW = Source{[Name="IFSDW"]}[Data],
    dbo_Dm_DIM_PROJ_FORECAST = IFSDW{[Schema="dbo",Item="Dm_DIM_PROJ_FORECAST"]}[Data]
    
in
    dbo_Dm_DIM_PROJ_FORECAST 

Data Types and Variables

M Language supports various data types such as text, numbers, dates, lists, records, and tables. Variables are declared using the "let" keyword. Example:

let
    UserName = "John",
    Age = 30,
    Birthdate = #1980-01-15,
    UserRecord = [Name = UserName, Age = Age, Birthdate = Birthdate]
in
    UserRecord

Functions and Expressions

M Language offers both built-in and custom functions. Built-in functions include mathematical operations, text manipulations, date functions, etc. Custom functions can be defined using the "let" keyword as well. Example:

let
    MultiplyByTwo = (x) => x * 2,
    Result = MultiplyByTwo(5)
in
    Result

Working with Lists

Lists are a collection of values in M Language. You can use functions like List.Transform, List.Sum, and List.Select to manipulate lists. Example:

let
    Numbers = {1, 2, 3, 4, 5},
    DoubledNumbers = List.Transform(Numbers, each _ * 2),
    Total = List.Sum(DoubledNumbers)
in
    Total

Conditional Logic

M Language supports conditional operations using functions like if-then-else and List.FirstN. Example:

let
    Value = 8,
    Result = if Value > 5 then "Greater" else "Less or equal"
in
    Result

Error Handling

You can handle errors using functions like try otherwise. Example:

let
    Numerator = 10,
    Denominator = 0,
    Result = try Numerator / Denominator otherwise "Error: Division by zero"
in
    Result

Advanced Techniques

Learn advanced techniques like working with record structures, defining table relationships, and creating recursive functions to solve complex data challenges.


Tips for Efficient M Code

  • Use meaningful variable and function names.

  • Break down complex transformations into smaller steps.

  • Leverage query folding to optimize performance.

  • Comment your code for better understanding.


Conclusion

M Language is a powerful tool for data transformation in Microsoft Power Query. By understanding its syntax and functions, you can efficiently transform and manipulate data according to your needs. With the provided code snippets and concepts, you're now on the path to becoming a proficient M Language user.

Remember, practice is key. Experiment with different scenarios and datasets to fully grasp the capabilities of M Language. Happy coding!

3 views0 comments

Recent Posts

See All

Comments


bottom of page