02.2Function syntax
If you have ever written a formula like =SUM(A1:A10) in Excel, you already know the essentials. A DAX function is written the same way, a name, parentheses, and what you give it to work on.
02.2.1The syntax of a function
Every DAX has its , its instructions for use. It lists its , the places to fill in between the parentheses, and it tells you in which order to fill them.
Here, there are two arguments. Arguments are separated by a comma.
DIVIDE divides the first argument by the second one. 1,700,000 divided by 4 gives 425,000.
There are two kinds of arguments, required arguments and optional arguments.
Required arguments must always be given. Without them, Power BI rejects the formula.
Optional arguments can be left out. The function then uses a value set in advance. In a syntax, you recognize an optional argument by its square brackets [ ].
For example, in the syntax of DIVIDE:
DIVIDE has two required arguments, <numerator> and <denominator>, and one optional argument, <alternate_result>, in square brackets. When you leave it out, also remove the comma in front of it. DIVIDE( 1700000, 4 ) and DIVIDE( 1700000, 4, 0 ) are therefore both correct.
In other resources, you may see the comma inside the square brackets, just before the name of the optional argument:
These resources put the comma inside the brackets because it is only needed when the optional argument is written. izyDAX finds that this way of writing can be confusing. So we write the comma before the bracket, since we have already said that a comma separates the arguments.
Some functions have no arguments. Their parentheses are still required, and empty:
TODAY returns today’s date. In the same way, BLANK() returns a blank value.
A function whose arguments are all optional can also be written with empty parentheses. The syntax of COUNTROWS has a single argument, in square brackets:
So you can write COUNTROWS(), with nothing inside the parentheses. It then counts the rows of the table where the measure is stored, in the Data pane. If the measure is stored in the Sales table, COUNTROWS() returns 12. This behavior is worth knowing, but izyDAX recommends writing the table every time, as in COUNTROWS( Sales ).
And in the syntax of FILTER:
The FILTER function needs two arguments, both required, a table and then a condition. It has no optional argument.
Two other signs come up in syntaxes. The ellipsis, …, means that the argument can be repeated. In the syntax of CALCULATE, you can write as many filters as you like, separated by commas:
The vertical bar, |, means “or.” You write one or the other, not both. In the syntax of ALLSELECTED, the first argument is a table or a column:
So you can write ALLSELECTED( Customers ), ALLSELECTED( Customers[City] ) or ALLSELECTED(), but never a table and a column in that first place.
02.2.2The kind of each argument
Each has a kind, in other words what you must put in its place.
Let’s go back to the syntax of FILTER:
The syntax announces the kind of each argument with the word between angle brackets. <table> expects a table, <condition> a condition.
Here is the same function, filled in:
Here, <table> is replaced by the Sales table, and <condition> by a condition, which keeps the sales whose amount is over 100,000. Each argument receives what its place asks for.
To get started, we distinguish six kinds of arguments. Don’t try to work out yet what these functions do. Look only at the highlighted part.
| A column | SUM( Sales[Amount] ) |
|---|---|
| A table | COUNTROWS( Sales ) |
| A condition | FILTER( Sales, Sales[Amount] > 100000 ) |
| A name | ADDCOLUMNS( Sales, "Price in Thousands", Sales[Price] / 1000 ) |
| A keyword | TOPN( 1, Sales, Sales[Amount], DESC ) |
| An expression | DIVIDE( SUM( Sales[Amount] ), 4 ) |
The last one, the expression, is the subject of the next part.
02.2.3The expression
An is anything that gives a result.
The simplest one is a number written as it is:
This expression gives 3.
A text written between double quotes is also an expression:
This expression gives the text New York.
A calculation is also an expression:
This expression gives 6.
A function also gives a result. So it is an expression too. Here, each function calculates over its whole table.
This expression gives 1,700,000, the total of the Amount column, over the whole Sales table.
This expression gives 4, the number of rows in the Products table.
All these results are a single value, a number or a text. We say they are .
An expression can also give a whole table. That is the case with FILTER:
This expression gives a table, the one with the 8 sales whose amount is over 100,000.
A result that is a table is called . A measure, on the other hand, always returns a scalar result.
An expression can be used as an argument. Let’s go back to the division from the start of the lesson:
Its two arguments are numbers written as they are. So they are already expressions.
You saw above that SUM( Sales[Amount] ) gives 1,700,000 and that COUNTROWS( Products ) gives 4. So you can write these two expressions in place of the numbers:
The result is still 425,000. One function has slipped inside another.
For DIVIDE, the calculation starts from the inside. First SUM and COUNTROWS, then DIVIDE, which receives their results.
Quiz
The questions often rely on the course tables. Keep them in view in their floating window, which you can move and enlarge.
Question 1
The average amount of a sale, rounded to the nearest thousand. ROUND rounds a number, just like ROUND in Excel:
Before you look, how many arguments does ROUND receive?
Question 2
What is the first argument of DIVIDE( SUM( Sales[Amount] ), 12 )?
Question 3
The syntax of DIVIDE is DIVIDE( <numerator>, <denominator>, [<alternate_result>] ). Which of these does Power BI reject?
Question 4
What does the expression FILTER( Sales, Sales[Amount] > 100000 ) produce?
- The syntax of a function lists the arguments it expects, their kind and their order.
- An argument in square brackets in the syntax is optional. When you leave it out, also remove the comma in front of it.
- An expression is anything that produces a result, like a number, a text, a calculation or a function.
- A result made of a single value is called scalar. A result that is a table is called tabular.
- A function can be used as an argument of another one. The calculation then starts from the inside.