top of page
Writer's picturekavin18d

Power Query M: Uses, Types, Functions, and Errors

Introduction

Power Query M is a functional programming language specifically designed to transform and manipulate data within Microsoft Power Query. Used primarily in Microsoft Excel and Power BI, Power Query M enables users to extract, transform, and load (ETL) data from various sources. Understanding its uses, characteristics, functions, types, and common errors can help users fully leverage its capabilities in data analytics and preparation.

Power Query M: Uses, Types, Functions, and Errors

What is Power Query M?

Power Query M is a scripting language that allows for advanced data transformation. It’s integral to Power Query, the data connection technology used in Microsoft’s analytics tools like Excel and Power BI. This language is responsible for the various steps performed during data transformation and cleaning in the background. Power Query M scripts can be viewed and modified directly in Power Query’s advanced editor, allowing for highly customized and efficient data manipulation processes.


Uses of Power Query M

Power Query M is widely used in data transformation processes for the following tasks:


  • Data Extraction: Power Query M can pull data from various sources, such as databases, websites, cloud services, and flat files like CSVs or Excel sheets.

  • Data Transformation: The language provides tools to clean, restructure, and aggregate data, preparing it for analysis. This includes operations like filtering rows, changing data types, splitting and merging columns, removing duplicates, and more.

  • Data Loading: After transforming the data, Power Query M loads it into the desired destination, whether it's an Excel spreadsheet or a Power BI data model.

  • Automation of ETL Processes: M code allows for automating data workflows, making it ideal for users who need to routinely perform the same transformation steps on updated datasets.

  • Custom Calculations: Power Query M can perform complex calculations and transformations that are not easily achievable using the standard Power Query GUI options.


Types in Power Query M

Power Query M has several data types, each with its specific characteristics:

Primitive Types :
  • Text : Represents string values.

  • Number : Includes both integer and decimal values.

  • Logical : Boolean values (`true` or `false`).

  • Date, Time, and DateTime : Represent date and time values.

  • Duration : Represents time intervals.


Structured Types:

Record: A collection of fields, similar to a row in a table, where each field has a name and a value.

  • List: An ordered sequence of values.

  • Table: A two-dimensional table of data, made up of rows and columns.

  • Function: Represents a reusable set of operations.

Null: Represents the absence of a value.

Binary: Used to represent binary data, such as files or images.


Functions in Power Query M

Power Query M has a wide variety of functions that fall into several categories, helping with data transformation and manipulation. Below are some of the most commonly used types:


Text Functions
  • Text.Replace(text, oldText, newText): Replaces instances of `oldText` with `newText`.

  • Text.Combine(list, delimiter) : Combines text values from a list, separating them with a specified delimiter.


Number Functions
  • Number.Round(value, digits): Rounds a number to a specified number of digits.

  • Number.Abs(value): Returns the absolute value of a number.


Date Functions
  • Date.AddDays(date, days): Adds a specified number of days to a date.

  • DateTime.FromText(text): Converts a text string into a DateTime object.


List and Table Functions
  • Table.SelectRows(table, condition): Returns rows from a table that satisfy a condition.

  • List.Sum(list): Returns the sum of the values in a list.


Conditional Functions
  • if...then...else`: Conditional logic that allows different outputs based on a logical condition.

  • Example:

if [Column1] > 0 then "Positive" else "Negative"

Function Values
  • M allows users to create custom functions using the `=>` syntax.

  • Example of a custom function that doubles a number:

DoubleNumber = (x) => x * 2



Common Errors in Power Query M

Working with Power Query M sometimes leads to errors. Here are a few common ones and how to address them:


Expression.Error: The name wasn’t recognized
  • This occurs when a function or variable is misspelled or doesn’t exist.

  • Solution: Double-check the spelling of functions and variables, ensuring that case sensitivity is considered.


Expression.Error: A cyclic reference was encountered during evaluation
  • This error occurs when a circular reference is created, where a function or expression indirectly refers to itself.

  • Solution: Review the transformation steps to avoid recursive dependencies.


Type Error
  • This error occurs when an operation is performed on an incompatible data type, such as trying to sum text values.

  • Solution: Ensure proper data types are assigned to variables before performing operations.


Expression.SyntaxError: Token Literal expected:
  • A syntax error indicating that the query language was unable to interpret the expression due to missing elements.

  • Solution: Review the code and ensure all tokens, such as commas, parentheses, and operators, are correctly placed.


Conclusion

Power Query M is a powerful and versatile scripting language designed for data transformation and preparation tasks. Its functional nature, wide array of built-in functions, and support for multiple data types make it an invaluable tool for data professionals working with Microsoft Excel or Power BI. By understanding its uses, characteristics, and common errors, users can more effectively leverage Power Query M to automate and customize their data workflows.

Comments


bottom of page