Power BI –Power Query M functions versus DAX

Using the right tool in the Power BI Desktop can bring efficiency wins and productivity gains to users.

This blog will give you a clearer understanding about how and why this is a common issue and how to solve it. “When should you use Power Query M Functions(M), and when is it better to use Data Analysis Expressions (DAX)?”.

In short

M: Data engineering or prep

DAX: Data analysis.

First, some limitations

Only the data model owner/developer can use Power Query M functions. If you create your Power BI desktop reports from a data model published by someone else, and you do not have admin permission to change the data model, you cannot use Power Query M. If you are not the data model owner/developer and you do not have the right permission, you cannot create column calculations using the DAX functions.

Power Query M functions and DAX functions can only be used in Power BI desktop, so if you create your reports in Power BI service (the online version) you cannot use any of them.

It can be confusing because you can do a lot of the same tasks equally in Power Query M and DAX.

Example where both are applicable

Imagine the scenario, your intended audience want to see the last 4 years-worth of data. We want to work efficiently, so cleaning the report by deleting the oldest month every month is not something we should consider doing. We also do not want to manually update all the visuals each month.

 

In the report below, you can see two card visuals showing the expenses from the last day previous month, and the 48 months before.

 

The first card result is done by DAX.

 

TotalExp =

CALCULATE (

SUM ( Expenses[Expenses] ),

DATESBETWEEN (

Dates[Date],

DATE ( YEAR ( NOW () ) – 4, MONTH ( NOW () ), 1 ),

EOMONTH ( TODAY (), -1 )

)

)

The logic in the DAX is that you want to sum the expenses for all dates between the date current year minus 4, the current month, and start from the first of the month.

The example below will return the exact same result but by using Power Query M.

In the advanced query editor, you can see the code the query editor writes when you connect to the sources and do tasks (also called steps).

To achieve your goal here, you do not need to write all the codes you see above. The query editor is able to write most of it for you. You just need to connect it to the source data.

The following code was added.

#”Filtered Rows” = Table.SelectRows(#”Changed Type”, each [Date] >= #date(Date.Year(DateTime.LocalNow())-4, Date.Month(DateTime.LocalNow()), 1) and

[Date] < #date(Date.Year(DateTime.LocalNow()), Date.Month(DateTime.LocalNow()), 1))

The logic in the code is very similar to the DAX code.

Check each date. If it is greater than and equal to the date, current year minus 4, current month, and the first day of the month and less than the year now, the month now, and the first of the month. It will always find the last day of previous month because it must be less than.

So which of the two options above is better practice?

Well, it depends on the situation.

As you saw at the top of this post – the most important piece to take forwards.

Power Query M functions are used for data engineering (also called data prep), and DAX functions for data analysis.

It is a huge advantage to use the Power Query M solution if the whole report only needs to show the last four years of data and if all reports created from the same data model only need the last 4 years of data. The Power Query M solution will filter the data and only store the data you need for your report through the process connecting the data source to the data model. Power BI will automatically update the report every month.

If you use the DAX solution, you filter the data from the data you already store in the data model. You may have 20 years of data in the data source. If you store a lot of data in the data model, it might load very slowly. It is always best practice to keep the data set as small as possible. If you do not filter the data in the connection, the data source will continually grow.

Conclusion

There is no clear answer to the question, should you use Power Query M or DAX, it all depends on the situation, but it is important to remember that if you are using a data model owned or developed by someone else, you will need to have admin permission to use Power Query M.

Power BI DAX and Power Query trainer, Jens Bonde, gives his opinion:

“Based on my experience, I use Power Query M to do the tasks I need for the preparation of the data sources. But after the data sources has been imported and the data model has been built, I use DAX. Living by the rule ‘M for Prep, DAX for analysis’”

STL has an Excel Power Query course and a Power Pivot course where we create data models in Excel and calculate measures using DAX.