This document provides a comprehensive overview of the DAX (Data Analysis Expressions) syntax used within SQL Server Analysis Services (SSAS) Tabular models.
DAX is a collection of functions, operators, and constants that can be used in formulas to create custom calculations in Analysis Services, Power BI, and Power Pivot in Excel.
A typical DAX formula follows this structure:
<Result> = <Expression>
Where:
<Result> is the name of the measure, calculated column, or calculated table.= is the assignment operator.<Expression> is the DAX formula that defines the calculation.DAX offers a rich library of functions categorized by their purpose:
SUM( ) : Sums all the numbers in a column.AVERAGE( ) : Computes the average of all numbers in a column.MAX( ) : Finds the largest value in a column.MIN( ) : Finds the smallest value in a column.COUNT( ) : Counts the number of rows that contain non-blank values in a column.COUNTROWS( ): Counts the number of rows in a table.
Logical Functions
IF( , , [value_if_false] ) : Returns one value if a logical expression is true and another value if it's false.
AND( , ) : Returns TRUE if all arguments are TRUE.
OR( , ) : Returns TRUE if any argument is TRUE.
Text Functions
CONCATENATE( , ) : Joins two text strings.
LEFT( , ) : Returns the specified number of characters from the start of a text string.
RIGHT( , ) : Returns the specified number of characters from the end of a text string.
LEN( ) : Returns the number of characters in a text string.
Date and Time Functions
TODAY(): Returns the current date.
NOW(): Returns the current date and time.
CALENDAR( , ) : Returns a table with a single column of dates.
Filter Functions
FILTER( , ): Returns a table that has been filtered.
ALL( [ | [, ]...] ): Returns all the rows in a table, or all the values in a column, ignoring any filters that might have been applied.
CALCULATE( [, [, ...] ] ) : Evaluates an expression in a modified filter context.
DAX Operators
DAX supports various operators for performing operations:
- Arithmetic Operators:
+, -, *, /
- Comparison Operators:
=, >, <, >=, <=, <> (not equal to)
- Text Concatenation Operator:
&
- Logical Operators:
&& (AND), || (OR), NOT()
DAX Syntax Examples
Here are some common DAX syntax examples:
Example 1: Simple Measure (Total Sales)
Total Sales = SUM( 'Sales'[SalesAmount] )
Example 2: Measure with Filter Context (Sales Last Year)
Sales Last Year =
CALCULATE(
[Total Sales],
SAMEPERIODLASTYEAR( 'Date'[Date] )
)
Example 3: Calculated Column (Full Name)
Full Name = 'Customers'[FirstName] & " " & 'Customers'[LastName]
Example 4: Calculated Table (Product Categories)
Product Categories =
SUMMARIZE(
'Products',
'Products'[Category],
"Product Count", COUNTROWS( 'Products' )
)
Best Practices
- Use clear and descriptive names for measures and columns.
- Leverage existing measures whenever possible.
- Understand filter context and row context.
- Optimize performance by writing efficient DAX expressions.
- Use comments to explain complex logic.
Further Learning
For more in-depth information, please refer to the official DAX documentation.