02.1Types of calculations in Power BI
Your tables are ready, and now you are going to calculate. In Power BI, the formula bar is used for three kinds of calculations, and picking the right one will save you plenty of detours. The examples use the dealership’s tables, Sales, Customers and Products. The yellow button, at the bottom right, shows them at any time.
02.1.1Measure, calculated table and calculated column
In Power BI, a DAX formula is mainly used to create three things, a , a calculated table or a calculated column. Here is an example of each.
A measure produces a single value. It can be a number, a text or a date.
To the left of the equal sign is the name of the measure, “Revenue.” To the right is its formula. SUM adds up the values, just like SUM in Excel. Its result is 1,700,000.
Sales[Amount] refers to the Amount column of the Sales table.
A calculated table produces, as its name says, a table. This one keeps the customers from New York:
New York Customers = FILTER( Customers, Customers[City] = "New York" )| Customer ID | Name | Type | City |
|---|---|---|---|
| C001 | Orvane | Company | New York |
| C002 | Kumora | Company | New York |
| C003 | Talvio | Company | New York |
This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.
The word “calculated” sets it apart from two other kinds of tables:
- The physical table, which comes from the data source, an Excel file or a database. Sales, Customers and Products are physical tables.
- The virtual table, which serves as an intermediate step while a formula is being calculated.
In this measure, FILTER creates a virtual table, the customers from New York. Then COUNTROWS counts the rows of that table:
The measure returns 3. The virtual table will never show up among the other tables.
A calculated column is added to a table. It produces a value on each row. This one, added to the Sales table, calculates the price in thousands:
On row V001, the price is 25,000, and the column shows 25. On each of the 12 rows, the same calculation applies to the price of that row.
The word “calculated” sets it apart from physical columns, which come from the data source, like “Qty” or “Price.” You can add a calculated column to a physical table as well as to a calculated table.
A calculated table can have just one column and one row. That does not make it a calculated column, it is still a table. We speak of a calculated column when the column is added to a table.
Each of these three calculations has its own place where you create it. In Report view, the Modeling tab of the ribbon has a button for each one:
- New measure creates a measure.
- New table creates a calculated table.
- New column creates a calculated column, in the selected table.
Every calculation has a name. It is required. The name comes first. It is followed by the “=” operator, then by the formula. A click on one of the three buttons opens the formula bar with a temporary name, “Measure,” “Table” or “Column,” already followed by the equal operator. You replace this temporary name with the name of your calculation, then you write the formula after the equal operator.
A few tips for choosing that name:
- A name that says what the calculation produces, like “Revenue,” “Number of Sales” or “Price in Thousands.” With several measures called “Measure,” “Measure 2” and “Measure 3,” nobody knows any more what each one calculates.
- Whole words rather than abbreviations. “Number of Sales” reads better than “NbS.” Spaces are allowed.
- A name meant for the person who reads the report. The name of a measure shows up in the visuals, at the top of a column or in the legend of a chart.
- A unique name. Two measures cannot share the same name, and neither can two tables.
Each button sets the kind of result it expects. Power BI only accepts a formula if its final result is of the expected kind:
- New measure expects a single value.
- New table expects a table.
- New column expects a value on each row of the table.
The formula for the customers from New York produces a table. Written with New measure, it is rejected by Power BI, since a measure must produce a single value.
Each of these three calculations will get its own lesson, later.
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
Where does Power BI accept the formula New York Customers = FILTER( Customers, Customers[City] = "New York" )?
Question 2
Which of these formulas does Power BI accept with the New measure button?
Question 3
What is the result of the measure Number of New York Customers = COUNTROWS( FILTER( Customers, Customers[City] = "New York" ) )?
Question 4
You add the calculated column Price in Thousands = Sales[Price] / 1000 to the Sales table. What does it produce?
Question 5
Among these tables, which ones are physical tables? There are two, find them.
- A DAX formula is mainly used to create a measure, a calculated table or a calculated column.
- A measure produces a single value, a number, a text or a date. A calculated table produces a table. A calculated column, added to a physical or calculated table, produces a value on each row.
- Each of these three calculations has its own place where you create it. Power BI only accepts a formula there if its final result is of the expected kind.
- A virtual table serves as a step inside a formula. Written on its own with New table, the same formula creates a calculated table.
- Every calculation must have a name, followed by the “=” operator, then by the formula.