As the name suggests Data Analysis eXpressions or DAX in Power BI is nothing but collection of operators, functions and constants which we use in writing formula or expressions to return value/values. It is a native language for data analytics tools of Microsoft. DAX is also a highly versatile and functional language with the capacity to work with a relational database.
DAX helps us to dig into the data we already have in our hand to explore new information. It helps us to perform dynamic aggressions, slice and dice the data. It is different from Power Query with M language at its core. Power Query performs the data extraction from different sources. Whereas DAX is applied to the extracted data source for analysis purpose.
It is very common to confuse between DAX and Power Query. You can refer this article to know a detailed comparison between Power Query and DAX.
Excel formula is similar to the DAX formula. Anyone with experience in writing Excel formula finds it easy to write DAX formula. However, DAX is far advanced than the Excel worksheet formula.
DAX is mainly used to create “Measures” and “Calculated Columns”. Below is an example of creating a measure using DAX.
Example of DAX formula
Page contents
Writing effective DAX formula is the key. An effective DAX formula helps us to get the most out of the data. Writing the DAX formula in Power BI is easy. Power BI DAX editor has a smart complete feature, which automatically prompts us with probable options.
Now let’s try writing a DAX formula to perform a simple calculation. I already have a data set in the Power BI desktop on the rainfall of different Indian subdivisions. The data was scraped from the web using the data scraping tool of Power BI. You can get the details of how to do it in this article.
Below is an example of how a DAX measure has been created on the Power BI desktop. The screenshots from my Power BI desktop shows the steps of creating a measure. The purpose of the measure is to create total annual rainfall.
First of all to create a new measure, right-click on the “Fields
” pane of the Power BI desktop report/data window and then choose “New measure
“.
The default name of the measure is “Measure
“. I have changed it to “Rainfall
“. As you start writing the function name Power BI starts suggesting with relevant functions name. Here I have selected “CALCULATE
“. It is a very popular and frequently used function of DAX.
As we enter into the “CALCULATE
” function, it starts to prompt us to show that it will accept an expression followed by filters. I have selected the “SUM
” function and the “ANNUAL
” column of the “rainfall_india
” table inside it as we want to calculate the total annual rainfall.
With this, the measure has been created. We can check the “Rainfall
” measure in the “Fields
” pane under the “rainfall_india
” table.
Nested function in DAX
Inside the “CALCULATE
” function again I have chosen the “SUM
” function. This is an example of a nested function, which is a function within another function. Nested functions help us to narrow down the query to achieve the desired result.
DAX can have up to 64 nested functions. Although using this many numbers of nested functions is very uncommon as debugging of such complex functions is very tough and the execution time of such functions is also high.
Using a measure in another measure
Another useful feature of the DAX formula is it allows using a measure already created within another measure. For example, if want to further narrow down the result to calculate the total annual rainfall of any particular subdivision, we can use the “Rainfall
” measure we already created. Let’s see how to do it.
For example, we want to know the total annual rainfall of the state “Kerala
“. The measure “Rainfall
” calculates the total annual rainfall. So, we need to provide a filter within the calculate function along with the “Rainfall
” measure.
See the above image where I have nested one measure within another. A table and a bar chart are also created to compare the total annual rainfall and Kerala_rainfall just show how the measures are performing.
Row context and filter context of DAX
These two concepts of context are very important for the effective use of DAX. Context refers to the dynamic analysis of the data.
Row context is related to functions while applying filters to identify a single row from the table. In most of the cases, we even dont realize that we are applying the concept of row context.
Filter context is a more complex concept than row context. It applies to narrow down the data. For example, here you can see how the column “SUBDIVISION
” of “rainfall_india
” has filtered the context and helped us to get the annual rainfall of a particular subdivision.