What is Power Query M?

Itexamtools
3 min readFeb 16

--

How power query m language programming is useful?

Power Query M language programming is useful for a variety of data-related tasks, including data transformation, data cleaning, and data integration. Some of the key benefits of using Power Query M language programming include:

  1. Automation: Power Query M allows you to automate data transformation tasks, making it easy to update your data with new sources, refresh your data, and keep it up-to-date.

2. Reusability: Power Query M is a functional programming language, which means that you can define and reuse functions across different queries. This allows you to create complex data transformations that can be used across multiple projects.

3. Data Integration: Power Query M supports a wide range of data sources, including Excel files, CSV files, databases, and web services. This means that you can easily integrate data from multiple sources into a single data model.

4. Flexibility: Power Query M provides a flexible programming environment that allows you to define and customize data transformations in a way that fits your specific needs. For example, you can use M functions to perform complex calculations, merge data from different sources, or handle data cleaning tasks.

5. Performance: Power Query M is designed to handle large datasets and complex transformations efficiently. M uses a lazy evaluation model, which means that data is only loaded and transformed when it is needed. This can improve the performance of your data transformations and make it easier to work with large datasets.

Overall, Power Query M language programming is a powerful tool for data analysts and other data professionals who need to work with large, complex datasets.

By using M, you can automate and streamline data transformation tasks, create reusable code, and integrate data from a variety of sources into a single data model.

Example coding for power query M

here’s an example of a simple Power Query M code that loads data from a CSV file, filters the data, and performs some basic transformations:

Suppose you have a CSV file containing data about sales, and you want to load the data into Power Query and filter it to only show sales for a specific region. Here’s how you can use Power Query M to accomplish this:

  1. Open Power Query in Excel or Power BI.
  2. Click on “Get Data” in the Home tab and select “CSV” from the list of data sources.
  3. Browse to the location of your CSV file and select it.
  4. In the “Navigator” window, select the table or tables you want to load and click “Edit”.
  5. In the “Query Editor” window, select the column that contains the region data and click on the filter icon in the header row.
  6. In the “Filter Rows” dialog, select the region you want to filter by, such as “North America”, and click OK.
  7. Click on the “Transform” tab in the ribbon and select “Replace Values”.
  8. In the “Replace Values” dialog, enter the value you want to replace, such as “Unknown”, and the new value, such as “Other”.
  9. Click OK to apply the changes.
  10. Click “Close & Load” to load the filtered and transformed data into Excel or Power BI.

Here is an example of the M code that Power Query generates to perform these transformations:

let
Source = Csv.Document(File.Contents(“C:\SalesData.csv”),[Delimiter=”,”, Columns=4, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#”Promoted Headers” = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#”Filtered Rows” = Table.SelectRows(#”Promoted Headers”, each ([Region] = “North America”)),
#”Replaced Value” = Table.ReplaceValue(#”Filtered Rows”,”Unknown”,”Other”,Replacer.ReplaceValue,{“Product”})
in
#”Replaced Value”

This code loads the CSV file, promotes the first row to headers, filters the rows based on the region column, and replaces the value “Unknown” in the Product column with “Other”.

You can modify this code to suit your specific data transformation needs, such as adding more filters, grouping data, or performing calculations.

Learn more at https://itexamtools.com/

--

--

Itexamtools

At ITExamtools.com we help IT students and Professionals by providing important info. about latest IT Trends & for selecting various Academic Training courses.