03.4Nesting functions

In the previous lesson, you already placed one function inside another, probably without noticing. Here is how to write and read them without getting lost.

03.4.1One function inside another

An argument can be another function. The functions are then said to be nested. The result of the function placed inside becomes the value of that argument.

Here, the second argument of DIVIDE is SUM( Sales[Qty] ):

DIVIDE( [Revenue], SUM( Sales[Qty] ) )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table

SUM returns 75, and DIVIDE divides the revenue by 75.

03.4.2Reading from the inside out

To read a nested formula, start from the innermost function, then work your way out. Each one hands its result to the function around it.

This measure returns the share of New York in the revenue:

New York Share = DIVIDE( CALCULATE( SUM( Sales[Amount] ), Customers[City] = "New York" ), SUM( Sales[Amount] ) )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table

CALCULATE returns the revenue of New York, 900,000. The second SUM returns the total revenue, 1,700,000. DIVIDE divides the first by the second, which gives 0.53.

Every opening parenthesis must be closed. The indentation shows where each function ends.

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

What does DIVIDE( CALCULATE( SUM( Sales[Amount] ), Customers[City] = "Toronto" ), COUNTROWS( Sales ) ) return?

Question 2

Which of these measures does Power BI reject?

Question 3

In COUNTROWS( FILTER( Products, Products[Category] = "SUV" ) ), what does COUNTROWS receive?

Key takeaways
  • An argument can be another function. Its result becomes the value of that argument.
  • You read a nested formula starting from the innermost function.
  • Every opening parenthesis must be closed, and the indentation helps you check it.