The other way you can extend your data model is by using measures (also referred to as calculated fields in Excel 2013). Unlike calculated columns, which are evaluated row by row using the context of the current row, measures are used to summarize data by aggregating the values of rows in a table. They work within the current filter context, which means they are affected by the current filters, slicers applied, and the highlighted sections of charts or visuals.
There are two types of measures: implicit and explicit. Implicit measures are created behind the scenes when you drag a field to the Values area of the PivotTable Fields list, as shown in Figure 1-7:
Figure 1-7: Creating an implicit measure in the PivotTable Fields dialog
They are also created when you drag a visual on to the desktop of Power BI, as shown in Figure 1-8:
Figure 1-8: Creating an implicit measure with a visual in Power BI Desktop
An explicit measure, on the other hand, is a measure that is specifically created by you.
A measure must be created if you want to conduct an operation on aggregate values instead of values on a row-by-row basis. For example, if you need to calculate the percentage ratio of two columns, you will need to create a measure that calculates the ratio based on the sum of each column. The following measure calculates the percentage of returns to sales by dividing the sum of items returned by the sum of items sold:
Return % =
DIVIDE (
SUM ( Sales[ReturnQuantity] ),
SUM ( Sales[SalesQuantity] )
)
Measures are calculated once for everywhere they are used in a report. They are re-calculated every time a report page loads or a user changes a filter or slicer or highlights part of a chart:
Figure 1-9: A measure being used in a column chart
In Figure 1-9, a measure is used to calculate the number of returns over the number of sales. This is used for the value in the column chart, with the sales channel used for the axis. In this example, the measure is calculated four times, once for each time the filter context changes to reflect each of the four different stores.
To create a new measure in Power BI Desktop, follow these steps:
- Start on the report page and highlight the table that you want to add the new measure to, from the list of tables shown in the Fields pane on the right-hand side.
- Next, right-click on the table name and select New measure from the menu, as shown in Figure 1-10:
Figure 1-10: Creating a new measure from the Fields pane
To create a new measure in Excel Power Pivot and Analysis Services, we do the following:
- Go into your data model and select the table you want to add the measure to.
- Select a blank cell in the calculation area.
- In the formula editor, write the expression that defines your new measure. Figure 1-11 shows a couple of measures in the calculation area of an Excel table:
Figure 1-11: Measures in the calculation area of a Excel Power Pivot table
The syntax used to create a measure differs slightly depending on the tool you are using. With Power BI, you use the = assignment operator, whereas with Excel and Analysis Services, you use the := assignment operator. If you use the := assignment operator in Power BI, it will automatically be converted into the = operator.
So, for example, Figure 1-12 shows an example of the syntax used to create a measure in Power BI Desktop:
Figure 1-12: A measure being created using the formula editor in Power BI Desktop
On the other hand, Figure 1-13 shows an example of the syntax used to create a similar measure in Excel:
Figure 1-13: A measure being created using the formula editor in Excel Power Pivot
Although DAX requires measures to be defined within a table, they can be moved between tables without affecting their functionality. In fact, it is good practice to keep general measures under one table with a name such as Key Measures.