There are four groups of operators in DAX:
- Arithmetic
- Comparison
- Concatenation
- Logical
Table 1-7 shows the different types of operator available within the arithmetic group, along with an example illustrating typical use:
Operator
|
Meaning
|
Example
|
+
|
Addition
|
3 + 7 = 10
|
-
|
Subtraction or sign
|
10 - 7 = 3
|
*
|
Multiplication
|
10 * 7 = 70
|
/
|
Division
|
10 / 5 = 2
|
^
|
Exponentiation
|
3 ^ 4 = 81
|
Table 1-7: DAX arithmetic operators
When using arithmetic operators it is important to consider the order in which they need to be applied. If necessary, use parentheses to override the precedence of an operator. Table 1-8 shows the order of precedence for each of the different DAX arithmetic operators:
Operator
|
Description
|
^
|
Exponentiation
|
-
|
Sign
|
* and /
|
Multiplication and division
|
+ and -
|
Addition and subtraction
|
Table 1-8: DAX arithmetic operator precedence
The following gives an example of where parenthesis can be used to override the precedence of an operator:
5*2+6 = 16
Here, the 5 is multiplied by the 2 to give 10, before the 6 is added to give 16. The multiplication operator (*) has higher precedence than the addition operator (+), so that part of the calculation is calculated first.
However, take a look at this example:
5*(2+6) = 40
Here, the use of the parentheses around 2+6 gives it higher precedence and causes it to be calculated before the result is multiplied by 5.
Table 1-9 shows the different types of comparison operators available, with an example illustrating the operator being used:
Operator
|
Meaning
|
Example
|
=
|
Equal to
|
[Firstname] = "Ian"
|
==
|
Strictly equal to
|
[Number] == 0
true only when number equals 0
and false if blank
|
>
|
Greater than
|
[Number] > 100
|
<
|
Less than
|
[Number] < 100
|
>=
|
Greater than or equal to
|
[Number] >= 100
|
<=
|
Less than or equal to
|
[Number] <= 100
|
<>
|
Not equal to
|
[Firstname] <> "Ian"
|
Table 1-9: DAX comparison operators
When using comparison expressions, you should consider the following points:
- Boolean values are treated as greater than string values.
- String values are treated as greater than numeric or date/time values.
- Numeric and date/time values are treated the same.
Table 1-10 shows the concatenation operator, with some examples illustrating how it is used:
Operator
|
Meaning
|
Example
|
&
|
Joins two values together to form one text value
|
"abcd" & "efg" = "abcdefg"
2 & 3 = "23"
|
Table 1-10: DAX concatenation operator
It is important to note that, as we have seen in the previous section on data types, when using the concatenation operator, DAX will implicitly convert numeric values to string values.
Table 1-11 shows the different types of logical operators available, with examples of each operator being used:
Operator
|
Meaning
|
Example
|
&&
|
Logical AND: If both expressions are TRUE, return TRUE; otherwise return FALSE.
|
(true) && (true) = true
(true) && (false) = false
|
||
|
Logical OR: If either expression is TRUE, return TRUE; when both expressions are FALSE, return FALSE.
|
(true) || (true) = true
(true) || (false) = true
(false) || (true) = true (false) || (false) = false
|
IN
|
Logical OR: Creates a logical OR condition between each value included in a list of values.
|
Channel(ChannelName) IN (‘Store’, ‘Online’, ‘Catalog’)
|
Table 1-11: DAX logical operators
In addition to the preceding logical operators, DAX also has the logical AND and OR functions that replicate the functionality of the AND operator (&&) and OR operator (||) respectively.
The advantage of using these functions over the equivalent operators in a complex expression is that it is easier to format and read the code. However, one drawback is that the functions only accept two arguments, restricting you to comparing two conditions only. To be able to compare multiple conditions, you will need to nest the functions. In this case, it might be better to use the AND operator (&&) instead.
The following gives an example of the syntax for the AND function:
Measure 1-3 =
IF (
AND (
20 > 10,
-20 < -10
),
"All true",
"One or more false"
)
The following gives an example showing the syntax of the AND function nested to compare three conditions:
Measure 1-4 =
IF (
AND (
AND (
10 > 9,
5 < 10
),
20 > 10
),
"All true",
"One or more false"
)
The following gives an alternative example of the one given, using the equivalent AND operator (&&):
Measure 1-5 =
IF (
10 > 9
&& 5 < 10
&& 20 > 10,
"All true",
"One or more false"
)
Any column in a table can have blank values, which are the result of the data source containing NULL in values. How a blank value affects the result of a DAX expression depends on the data type expected and the operator being used. In some instances, a blank value will be converted into a zero or an empty string, while in others, it will propagate through as a blank. Table 1-12 shows how different DAX operators handle blank values:
Expression
|
DAX
|
BLANK + BLANK
|
BLANK
|
BLANK & "Hello"
|
Hello
|
BLANK + 2
|
2
|
BLANK * 2
|
BLANK
|
2 / BLANK
|
Infinity
|
0 / BLANK
|
NaN
|
BLANK / BLANK
|
BLANK
|
FALSE OR BLANK
|
FALSE
|
FALSE AND BLANK
|
FALSE
|
TRUE OR BLANK
|
TRUE
|
TRUE AND BLANK
|
FALSE
|
BLANK OR BLANK
|
BLANK
|
BLANK AND BLANK
|
BLANK
|
Table 1-12: Handling blank values in DAX
The BLANK data type represents nulls, blank values, empty cells, and missing values. The BLANK function is used to generate blanks, while the ISBLANK function is used to verify a blank value.