In this post, we will explore the syntax and requirements of the DAX language.
A DAX formula always starts with an equal sign (=). After the equals sign, you can provide any formula that evaluates to a scalar, or an expression that can be converted to a scalar. These include the following:
A scalar constant, or expression that uses a scalar operator (+,-,*,/,>=,…,&&, …)
- References to columns or tables. The DAX language always uses tables and columns as inputs to functions, never an array or arbitrary set of values.
- Operators, constants, and values provided as part of an expression.
- The result of a function and its required arguments. Some DAX functions return a table instead of a scalar, and must be wrapped in a function that evaluates the table and returns a scalar; unless the table is a single column, single row table, then it is treated as a scalar value.Most DAX functions require one or more arguments, which can include tables, columns, expressions, and values. However, some functions, such as PI, do not require any arguments, but always require parentheses to indicate the null argument. For example, you must always type PI(), not PI. You can also nest functions within other functions.
- Expressions. An expression can contain any or all of the following: operators, constants, or references to columns.
For example, the following are all valid formulas.
|If you use this formula within the Sales table, you will get the value of the column Amount in the Sales table for the current row.
|Three percent of the value in the Amount column of the current table.
|The value of the constant pi.
A data model often contains multiple tables. Together the tables and their columns comprise a database stored in the in-memory analytics engine (VertiPaq). Within that database, all tables must have unique names. The names of columns must also be unique within each table. All object names are case-insensitive; for example, the names SALES and Sales would represent the same table.
Each column and measure you add to an existing data model must belong to a specific table. You specify the table that contains the column either implicitly, when you create a calculated column within a table, or explicitly, when you create a measure and specify the name of the table where the measure definition should be stored.
When you use a table or column as an input to a function, you must generally qualify the column name. The fully qualified name of a column is the table name, followed by the column name in square brackets: for examples, ‘U.S. Sales'[Products]. A fully qualified name is always required when you reference a column in the following contexts:
- As an argument to the function, VALUES
- As an argument to the functions, ALL or ALLEXCEPT
- In a filter argument for the functions, CALCULATE or CALCULATETABLE
- As an argument to the function, RELATEDTABLE
- As an argument to any time intelligence function
An unqualified column name is just the name of the column, enclosed in brackets: for example, [Sales Amount]. For example, when you are referencing a scalar value from the same row of the current table, you can use the unqualified column name.
If the name of a table contains spaces, reserved keywords, or disallowed characters, you must enclose the table name in single quotation marks (‘table name’). You must also enclose table names in quotation marks if the name contains any characters outside the ANSI alphanumeric character range, regardless of whether your locale supports the character set or not. For example, if you open a workbook that contains table names written in Cyrillic characters, such as ‘Таблица’, the table name must be enclosed in quotation marks, even though it does not contain spaces.
- Table names are required whenever the column is from a different table than the current table. Table names must be unique within the database.
- Table names must be enclosed in single quotation marks if they contain spaces, other special characters or any non-English alphanumeric characters.
- Measure names must always be in brackets.
- Measure names can contain spaces.
- Each measure name must be unique within a model. Therefore, the table name is optional in front of a measure name when referencing an existing measure. However, when you create a measure you must always specify a table where the measure definition will be stored.
Column names must be unique in the context of a table; however, multiple tables can have columns with the same names (disambiguation comes with the table name).
In general, columns can be referenced without referencing the base table that they belong to, except when there might be a name conflict to resolve or with certain functions that require column names to be fully qualified.
If the name that you use for a table is the same as an Analysis Services reserved keyword, an error is raised, and you must rename the table. However, you can use keywords in object names if the object name is enclosed in brackets (for columns) or quotation marks (for tables).
The following characters and character types are not valid in the names of tables, columns, or measures:
- Leading or trailing spaces; unless the spaces are enclosed by name delimiters, brackets, or single apostrophes.
- Control characters
- The following characters that are not valid in the names of objects:
Examples of object names
The following table shows examples of some object names:
|If the table name does not contain spaces or other special characters, the name does not need to be enclosed in quotation marks.
|If the name contains spaces, tabs or other special characters, enclose the name in single quotation marks.
|Fully qualified column name
|The table name precedes the column name, and the column name is enclosed in brackets.
|Fully qualified measure name
|The table name precedes the measure name, and the measure name is enclosed in brackets. In certain contexts, a fully qualified name is always required.
|Unqualified column name
|The unqualified name is just the column name, in brackets. Contexts where you can use the unqualified name include formulas in a calculated column within the same table, or in an aggregation function that is scanning over the same table.
|Fully qualified column in table with spaces
|The table name contains spaces, so it must be surrounded by single quotes.
The syntax required for each function, and the type of operation it can perform, varies greatly depending on the function. In general, however, the following rules apply to all formulas and expressions:
- DAX formulas and expressions cannot modify or insert individual values in tables.
- You cannot create calculated rows by using DAX. You can create only calculated columns and measures.
- When defining calculated columns, you can nest functions to any level.
DAX has several functions that return a table. Typically, you use the values returned by these functions as input to other functions, which require a table as input.