DAX (Data Analysis Expression) was first introduced with PowerPivot as part of SQL Server 2008 R2 release. It is an expression language following the similar syntax of Excel formula, enabling Excel power users and BI developers the ability to write calculations inside PowerPivot workbook to extend PowerPivot’s analytical capability. DAX can be used to create a calculated column inside a PowerPivot table or a calculated measure dynamically evaluated inside the PivotTable against the PowerPivot data source. The details and implementation of this will be for future blog posts. In this blog post:
- Introduce DAX
- Enhancements with SQL Server 2012
- Functions available with DAX
- How are they different from Excel Functions
- How are they different from MDX functions
In V1 release of SQL Server 2008 R2, DAX supports functions which perform:
- Common mathematic and statistical functions, such as CEILING(), FLOOR(), ABS(), AVERAGE(), MIN(), MAX()
- Logical functions, such as IF(), TRUE(), FALSE()
- Informational functions, such as ISERROR(), ISBLANK()
- Date and Time functions, such as YEAR(), DATE(), MONTH()
- Filter function, such as FILTER()
- Functions that handle relationship of the tables, such as RELATED(), RELATEDTABLE()
- Functions that control the current context of the evaluation, such as VALUES(), EARLIER(), EARLIEST(), CALCULATE().
- Time Intelligence functions to allow analysis over time, such as PARALLELPERIOD(), CLOSINGBALANCEMONTH(), OPENNINGBALANCEMONTH()
What is new in SQL Server 2012
SQL Server 2012 has enhanced DAX functionality and removed some of the limitations from the V1 release to meet the following goals:
- Extend the functions available with SQL Server 2008 R2 release. These new functions not only expand the analytical capabilities, but also make authoring DAX calculations easier.
- Supports Power View report which uses BISM tabular database as the backend. This includes the new table queries, parameters and variables.
The list below offers a quick glance on the new functions in SQL Server 2012. The details on these functions will be covered in the later blogs.
The following new functions are added to create parent child relationship:
Basic syntax of DAX
A DAX expression always begins with an equal sign followed by the formula. The formula can contain constant values, DAX functions or the combination of the two. For instance,
= 1.1 * SUM(‘FactInternetSale’[SaleAmount])
= ‘Customer’[FName] & ‘, ‘ & ‘Customer’[LName]
All DAX functions share the common syntax as below,
DAXfunctionName(argument1, argument2, ….)
Depending on the function, argument can be a table expression (that returns a table), a column name or an explicit value. Table name in DAX needs to be enclosed by the single quotation ‘table name ‘ if the table name contains a white space between the characters, a special character or a reserved word. Column name must always be enclosed in . It is not required to precede column name with the table name as in ‘FactSales’[SaleAmount], but it is highly recommended to fully qualify the column name to avoid any potential ambiguity when resolving a column name.
Data type supported in DAX
The data type for a column can be:
- Whole number
- Boolean (True/False).
Difference between DAX and Excel formula
Even though DAX follows the same syntax convention as the Excel formula, there are fundamental differences between the two.
- DAX will only work with PowerPivot workbook and PivotTable created from the PowerPivot workbook. DAX cannot be consumed by regular non PowerPivot workbook or non PowerPivot PivotTable. Similarly, the regular Excel functions that are not implemented in DAX cannot be used in PowerPivot workbook or PowerPivot PivotTable.
- DAX is built upon the relational database concept, i.e. table and table relationship. Consequently, functions utilizing relationship provided in DAX. There are no such functionalities in Excel.
- DAX operates on entire table or entire column, i.e. DAX cannot operate on an individual cell, or a range/array of cells.
- To work with In Memory data, DAX has its own implementation on the common native Excel aggregation functions, such as SUM, AVERAGE, MIN, MAX, COUNT, which only takes column as the argument, unlike the native Excel functions which can take a list of ranges of cells as the function arguments.
- DAX has new aggregation functions that aggregates an expression over the rows of a table, such as SUMX, AVERAGEX, MINX, MAXX, COUNTX.
- DAX calculated measure provides dynamic aggregation based on the current filter context as defined by the members in the Row label, Column label, Value field, Slicers and Report Filter in the PivotTable.
- DAX provides Time Intelligence capabilities.
- DAX functions that returns a table can be used as the intermediate result to other DAX function that returns a single scalar value.
- The return data type may be different for the functions that share the same name in DAX and Excel, for instance, date and time functions return datetime data type in DAX. The same set of functions return integer in Excel.
- DAX supports several different data types such as Integer, Double, Datetime, String, Currency, Decimal, etc. In Excel, everything is either a number (double) or a string. Numbers are then formatted to appear as dates or as currency or with a fixed number of decimals. This use of formatting in Excel is different from having distinct data types as in PowerPivot.
Difference between DAX and MDX
- MDX is primarily a query language which asks for the specific data from the multi-dimensional Analysis Services database. MDX can also be used to create calculations in the traditional Analysis Services database, including calculated measures, calculated members, scope assignment, custom rollup etc.
- DAX is an expression language which allows Excel power user to create a calculated column in PowerPivot workbook or a calculated measure in Pivot table based on the PowerPivot workbook, using the familiar Excel formula syntax. DAX is not designed to replace MDX.
- MDX is based on multi-dimensional objects such as dimensions, measure groups, attributes, and hierarchies. MDX syntax includes notation for sets and tuples.
- DAX is based on relational constructs such as tables, columns, and relationships. DAX syntax is designed to mimic the syntax of Excel formulas.
- Inside PivotTable against PowerPivot data, Excel generates and sends MDX query to SSAS engine when user drags and drops members onto Row, Column, and Value fields.
- A DAX calculated measure or calculated column can only return one data type, while MDX treats measure as variant data type and one measure can return different data type based on the calculation condition. For example, inside the IF statement If (condition, expr1, expr2) in DAX, both expressions need to return the same data type, while in MDX, one expression can return a number and another returns a string.
This is just a start to many more blog posts to come on DAX. Do let me know if you learnt something new today. I have seen customers ask these questions time and again when I show them Excel and DAX functionality. Thought this will be a dump of these conversations for reference to all. Thanks for reading this far.Share this article
This entry was posted on Monday, May 27th, 2013 at 08:46 and is filed under Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.