Common DAX functions and expressions for a SQL Server Tabular Database
I have a Tabular Database and I'd like to know how to use some common functions and expressions used in DAX as well as how to format the display of the data?
In this tip, we will show you some common expressions in DAX using a Tabular Database as well as how to format columns.
- SQL Server 2016 (most of the functions will work on SQL Server 2014, but some are exclusive for SQL Server 2016 and future versions).
- SSDT should be installed.
- A Tabular Instance installed.
- Some Tables with Dates, Strings and Numbers.
Here are some typical DAX expression and function examples for a Tabular Database.
DISTINCTCOUNT DAX Expression
A very common function is the DISTINCTCOUNT. We can count the number of distinct rows. In this example, we will count the number of different Products using ProductKey:
Count Products:=DISTINCTCOUNT('Internet sales'[ProductKey])
SUM DAX Expression
With SUM, you can add numbers of a specified column. The following example adds the values of the TaxAmt column in the SalesOrderHeader table:
DATEDIFF DAX Expression
DATEDIFF is a new function in SQL Server 2016. It shows the difference between two dates. In this example, we will show the number of years of an OrderDate. It shows the difference between Today and the OrderDate:
Concatenate Strings DAX Expression
We can concatenate strings or other values using the &. The following example concatenates the FirstName and LastName with a space between them:
IF DAX Expression
IF allows conditional operations. The following example will show LOW if the TaxAmt is lower than 10, MEDIUM if the value is lower than 100, else HIGH:
SWITCH DAX Expression
IF is used for conditional operations of 2 or 3 values usually. For more values, use SWITCH. The following example shows the Region Description according to a specific numeric value. For example if Territory ID is 1, the value will be Northwest, if the value is 2 then Northeast and so on:
INT DAX Expression
The INT function displays the integer value of a specified number:
ROUND DAX Expression
If you need to round to the closest integer value, you can use the ROUND function. The following function rounds to an integer value with 0 decimals:
The following example rounds to a value with 3 decimals:
Parsing Strings DAX Expression
The following example gets the lastname from the fullname. The FIND function detects the position of the space and the function returns the string starting from the space until the end of the string:
=LEFT([Full Name],(FIND(" ",[Full Name])))
DATEADD DAX Expression
The example shows how to add 2 years to a specified data using the DATEADD function:
Formatting Column Display DAX Expression
Format Decimal Places
To format the display output there are several functions. However, the easiest way to handle the format is to go to properties. In this example, we are selecting 2 decimal places for TaxAmt:
Format Currency Display in DAX
It is also possible to select the currency symbol using the properties. In this example, we are using British pounds as the currency symbol:
Change Data Format and Type in DAX
You can change the format from currency to decimal or other data types:
DAX includes functions and expressions very similar to Excel. The main idea of DAX was to create a more familiar language to create functions and expressions. DAX is simple for basic calculations, but it can be complex for some calculations.
Next StepsFor more information, refer to the following links:
- DAX Function Reference
- DAX Operator Reference
- Getting Started with the DAX queries for SQL Server Analysis Services
- New DAX functions in SQL Server 2016
About the author
View all my tips