M or DAX? That is the Question!

doors-1690423_1280

What is the main difference between M and DAX? Why we can do a calculated column in two different places? What are pros and cons of each? Which one should I use for creating a profit column? Why I cannot do all of it in only one; DAX or M! Why two different languages?! Why structure of these two are so different? … If any of these are your questions, then you need to read this post. In this post I’ll go through differences of these two languages, and explain why, when, where of it. Normally I don’t get this question asked from students of my Power BI course, because I elaborate the difference in details. However, if you have this question, this is a post for you. If you would like to learn more about Power BI; read Power BI book; from Rookie to Rook Star.

What is M?

M is the scripting language behind the scene for Power Query. M is the informal name of this language. the formal name is: Power Query Formula Language! Which is really long, and even Microsoft refer it to M. M stands for many things, but one of the most common words of it is Mashup. Which means this language is capable of data mashup, and transformation. M is a functional language. and structure of M script can be similar to this:

5

Source: Day Number of Year Function in Power Query

M is a step by step language structure. Usually (Not always), every line in M script is a data transformation step. and the step after that will use the result of previous step. It is usually easy to follow the structure of M language for a programmer. because it is understandable with programming blocks of Let and In, and some other programming language features alike.

What is DAX?

DAX is Data Analysis eXpression Language. This is the common language between SQL Server Analysis Services Tabular, Power BI, and Power Pivot in Excel. DAX is an expression language, and unlike M, it is very similar to Excel functions. In fact, DAX has many common functions with Excel. However DAX is much more powerful than Excel formula in many ways. Here is an example DAX expression:

2017-03-02_21h43_35

Source: Secret of Time Intelligence Functions in Power BI

DAX calculations are built in a way that makes sense mostly for Excel users. Normally Excel users are very comfortable with this language. Everything goes through functions. DAX doesn’t have programming blocks in it, and is combination of function uses, filters, and expressions.

Example Usage of M

M can be used in many data transformation scenarios. As an example, it can be used to Pivot or Unpivot Data, To Group it based on a number of columns. Here is how a Pivot/Unpivot can work in Power Query;

2017-03-02_22h43_23

Example Usage of DAX?

DAX can be used for many calculation for analyzing data. For example, calculating Year To Date, Calculating Rolling 12 Months Average, or anything like that. Here is an example which based on a selection criteria in the report and few simple DAX expressions we can do a customer retention case with DAX;

2016-11-22_09h07_06

Calculated Column Dilemma

The main question of choosing between DAX and M comes from calculated column dilemma in my opinion. You can create many calculated columns in both M or DAX, and it is confusing where is the best place to do it, or why there are two different places to do it?! As an example; you can create a full name which is concatenated of FirstName and LastName column. You can do that in M, and also in DAX. So this question comes up that: Why two different places? Which one is best to use? can we always use one language?

To answer this question, I would like to use another example; There are many types of knives, and you can use almost all of them to cut cheese!

reference: http://forkitchen.blogspot.co.nz/2008/10/what-are-different-types-of-kitchen.html

Almost every knife in above picture can be used for cutting cheese except one of them! So why there are so many knives for cutting cheese?! The answer is that; these are not knives for cutting cheese! each knife is good for doing one special case. for cutting bread, bread knife gives you the best result. for cutting a fillet you normally need another type of knife. but as you agree, for some cases (such as cutting cheese!) you can use many of these knifes. Let’s know go back to the original question;

Why I can create same calculated column in DAX or M?

These two languages built independently. They built in a way that they can handle most of business related solutions. So as a result there are some use cases that both languages are capable of doing it. As an example both of these languages can easily be used to create a concatenated column of two other columns.

Which one is best?

Quick answer is Depends! Depends on type of usage. If you want to create a concatenated column; Power Query (M) is better option in my view, because that is normally like the ETL part of your BI solution, you can simply build your model and data sets in a way you like it to be. But if you want to create something like Year To Date; Obviously you can do that in Power Query or M, but it will be lots of code, and you have to consider many combinations of possibilities to create a correct result, while in DAX you can simply create that with usage of TotalYTD function. So the answer is; there is no best language between these two. The type of usage identifies which one is best. Normally any changes to prepare the data for the model is best to be done in M, and any analysis calculation on top of the model is best to be done in DAX.

Two Languages for Two different Purposes

There are many programming languages in the world, each language has its own pros and cons. JavaScript is a language of web scripting, which is very different from ASP.NET or PHP. Same thing happens here. When M born, it meant to be a language for data transformation, and it is still that language. DAX was created to answer business analysis questions.

What Questions DAX Can Answer?

DAX is the analytical engine in Power BI. It is best language to answer analytical questions which their responses will be different based on the selection criteria in the report. For example; You might want to calculate Rolling 12 Months Average of Sales. It is really hard if you want to calculated that in M, because you have to consider all different types of possibilities; Rolling 12 months for each product, for every customer, for every combinations and etc. However if you use a DAX calculation for it, the analytical engine of DAX take care of all different combinations selected through Filter Context in the report.

What Questions M Can Answer?

M is Data Transformation engine in Power BI. You can use M for doing any data preparation and data transformation before loading that into your model. Instead of bringing three tables of DimProduct, DimProductSubcategory, and DimProductCategory, you can merge them all together in Power Query, and create a single DimProduct including all columns from these tables, and load that into the model. Loading all of these into the model and using DAX to relate these with each other means consuming extra memory for something that is not required to be in the model. M can simply combine those three tables with each other and based on “Step Based” operational structure of M, they can be simply used to create a final data set.

As a Power BI Developer Which Language Is Important to Learn?

Both! With no hesitation!  M is your ETL language, and DAX is the analytical language. You cannot live with only one. If you want to be an expert in Power BI, you should be an expert in both of these languages. There are some cases, that one of the languages will be used more than the other one. However, you will need very good understanding of both languages to understand which one is best for which purpose, and easily can use it in real-world scenarios.

Save

Save

Save

Reza Rad on FacebookReza Rad on LinkedinReza Rad on TwitterReza Rad on Youtube
Reza Rad
Trainer, Consultant, Mentor
Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.

16 thoughts on “M or DAX? That is the Question!

  • Nice article, it does shed the light and helps one to learn the language even further. Thanks a lot. Your book is exceptional and it helps to deepen my knowledge with Power BI using both Excel and Power BI. It is even better than some out there. Pictures and explanations are awesome and configurations of each process properly detailed. Thank you very much.

  • If you can do it in M and DAX, then do it in M. I’m a big advocate of pushing transforms and calculations as far upstream as possible. Ideally you leave DAX to do the absolute minimum – especially when data volumes get large.

    Do the processing once as you read the data into your model, rather than over and over in your model. It will result in faster, more efficient reports.

  • Both languages are important for Power BI Developer, that’s very true. Having both ETL and Data Modelling components in one tool makes Power BI the best BI tool I’ve ever used.

  • Nice article, Thanks. And I am totally agree with Phil, “If you can do it in M and DAX, then do it in M.” It will keep the model simple and have a better performance.

  • I’m using Excel Power Query and Power BI
    Let’s take Excel.
    I have a workbook that has many tabs consuming the same data. Some of them show different set of columns , some different pivots connected with slicers.
    The same data many times.
    If I define calculated column in the Excel (DAX or similar to DAX ; example: Apr_month_over_month=[@[Apr_value]]-[@[Mar_value]]) and then I have to connect pivot table to this Excel table not the Query.
    If I need this column in the second tab I have to do the same second time….
    This leads me to define what is possible in the Query so it can be reused in many sheets.
    This Problem in Power BI doesn’t exist because I can reuse DAX calculated column, but it is a hint….
    What more.
    Possibly I would like to move a report from Excel and Power Query to Power BI. Power BI can import Excel report definition. Possibly it will take Excel Table calculated columns and defines them in DAX but I don’t know. If I have everything in M it is not a problem.

  • Thank you for this article – I’m just starting to look at M and DAX, and it’s very enlightening!

  • My guideline
    1. SQL first. M second. DAX last
    2. Involves 1 table, M
    3. Involves 2+ tables, DAX

    Majority of the work will be in SQL & M.
    Use DAX only when necessary

    • Hi Mark
      I’d change that to use DAX; when it is better (not necessary). There are certain things that can be done better with DAX, although it can be still done with M.
      It is a matter of finding the right tool for each use case.
      Cheers
      Reza

  • Thanks Rad,
    for clarifying really well the question of why have both M and DAX.
    This was not really explained in a PowerBI manual I am studying (hence had to search the Net).
    Tim Boulton

Leave a Reply