← 30 Days Power BI Series
Day 21 of 30AdvancedM Language

Day 21: Power Query M Language Basics

5 questions · Power BI Interview Preparation

Q1

What is M language in Power BI?

M (also called Power Query Formula Language) is the functional language behind Power Query transformations in Power BI and Excel. Every step you create in the Power Query Editor through the graphical interface generates M code behind the scenes. You can view and edit this code via View → Advanced Editor in the Power Query Editor. M is case-sensitive, lazy-evaluated, and functional — each step is an expression that transforms the previous step's output. Understanding M allows you to write transformations that the graphical interface cannot express.

💡 Interview tip: You do not need to be an M expert for most analyst roles, but being able to read and tweak M code in the Advanced Editor is a valued skill that separates intermediate from advanced users.
Q2

How do you write a basic M query?

Every M query follows the let ... in structure: let Source = Excel.Workbook(File.Contents("C:\data.xlsx"), null, true), Sheet1 = Source{[Item="Sales",Kind="Sheet"]}[Data], #"Promoted Headers" = Table.PromoteHeaders(Sheet1), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Revenue", type number}}) in #"Changed Type" Each named step references the previous step. The final step after "in" is what gets loaded. Comments use // for single line.

💡 Interview tip: Understanding the let...in structure is the foundation of M. Each step name is a variable containing the result of that transformation. The "in" clause specifies which step to output.
Q3

How do you add a conditional column using M instead of the graphical interface?

In the Advanced Editor, add a custom step after your existing steps: #"Added Custom" = Table.AddColumn(#"Previous Step", "Category", each if [Revenue] > 100000 then "High" else if [Revenue] > 50000 then "Medium" else "Low", type text) Table.AddColumn takes: the previous table, the new column name, a function using "each" (shorthand for a row-by-row function), and optionally the column type. The "each" keyword and the underscore (_) or field reference ([Revenue]) pattern is fundamental M syntax.

💡 Interview tip: "each" in M is shorthand for (x) => x. [ColumnName] inside "each" references the current row's value in that column — equivalent to the current row in a SUMX iteration in DAX.
Q4

How do you parameterise a Power Query to make it dynamic?

Power Query Parameters let you create reusable values that queries reference. Home → Manage Parameters → New Parameter → set Name, Type, and Default Value. Use in queries by referencing the parameter name as a variable. Common use: a file path parameter so the same query works on different machines: Source = Excel.Workbook(File.Contents(FilePath), null, true) where FilePath is the parameter. In Power BI Service, parameters can be updated when refreshing — useful for changing the date range or environment (dev/prod database) without editing the query.

💡 Interview tip: Parameters are essential for making queries portable and environment-aware. A query hardcoding "C:\Users\Prashant\Downloads\data.xlsx" breaks on every other machine.
Q5

How do you write a custom function in M?

A custom function in M is a query that takes parameters and returns a transformed table. Example — a function to load and clean any monthly sales file: (filePath as text) => let Source = Csv.Document(File.Contents(filePath)), #"Promoted Headers" = Table.PromoteHeaders(Source), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers", {{"Date", type date}, {"Revenue", type number}}) in #"Changed Type" Save this as a query named "CleanSalesFile". Then in another query: Table.AddColumn(FileList, "Data", each CleanSalesFile([Path])). This is how the "Combine Files from Folder" feature works internally.

💡 Interview tip: Custom functions in M are the key to the Combine Files from Folder pattern. Understanding them lets you customise the combination logic for non-standard file formats.
← Day 20All DaysDay 22

Want live Power BI coaching?

Join EVIKA Academy for hands-on Power BI training with real projects and placement support in Delhi NCR.

Book Free Demo →
Best Data Analytics Course in Noida Delhi NCR | EVIKA ACADEMY