Understanding context for measures

To demonstrate how measures are different compared to calculated columns, we will execute the following steps:

  1. Create a new page by right-clicking the Page 2 tab and selecting Duplicate Page. A new page called Duplicate of Page 2 will be created.
  2. Remove the table on the left by selecting the table and then clicking the Del key. This deletes the table visual from the canvas. Use the sizing handles to expand the remaining table so that it takes up the entire page.

To create a measure, right-click the Hours table in the FIELDS pane and choose New measure. The formula bar is activated. In the formula bar, enter the following formula:

Measure = 
SUMX(
FILTER(
'Hours',
'Hours'[Category] = "Billable"
),
'Hours'[Hours]
)

Note that this formula is extremely similar to the formula for our TotalBillableHours column from the Creating calculated columns for utilization section. The main differences are that we prefix column references with the name of the table in which those columns exist and that we have removed our EmployeeId filter clause.

While not strictly required in all circumstances, since measures can exist in any table, it is good practice to refer to any column within a measure using both the table name and column name. If no table name is specified for a column, the measures assume the current table, and this can cause problems when moving measures between tables or if duplicate column names exist in multiple tables.

It is important to understand that, unlike calculated columns, measures have no implicit row context, only a filter context. This means that, when measures refer to a column in a table, that column must be referenced within an explicit aggregation function such as SUM, AVERAGE, MAX, MIN, and so on. This also means that, while measures can be used within a calculated column formula, the row context for that calculated column simply becomes the initial filter context for the measure calculation.

We can now use this measure in our visualization by performing the following steps:

  1. Place this Measure into our table visualization and note that the results that are displayed are exactly the same numbers that we have for the average of our calculated column, TotalBillableHours.
  2. Shrink this table visualization horizontally and add a Slicer visualization by clicking on a blank area of the canvas.
  3. Select the Slicer visualization from the VISUALIZATIONS pane.
  4. Place Month from our Calendar table into the Field for our slicer.
  5. Select March from within the slicer.

By doing this, we can see how calculated columns and measures are different. We can observe that the value for the average of TotalBillableHours did not change, but the value for Measure is now much lower. If we select February or January in our slicer, again, the calculated column values do not change, while the values for our Measure do.

To understand what is happening, it is important to recognize that, while the slicer filters the rows in the Hours table for both the calculated column and the measure, since the calculated column has the same value for every row per employee, the average of those values never changes. Using ALL in the formula for our calculated column, TotalBillableHours, effectively removes the filter context from our slicer and hence, the calculation of that column only includes the filters that are specified directly in the calculated column definition, that is, Category and EmployeeId.

With the measure, the summation calculation specified in the formula is performed within the combined context of the visualizations and the DAX formula to create a truly dynamic evaluation context.

This evaluation context actually comes from three different sources:

  • The first source is the table visualization itself. Because we have the Name of the employee in our table, this creates a filter context where the measure that's displayed within a row of the table visualization will only consider rows within the Hours table that are related to the Name of the employee from the People table.
  • The second source is the Slicer visualization. By selecting a particular Month in the slicer, this adds additional filter context where the measure also only considers rows within the Hours table that are related to the Month that's selected from our Calendar table in the slicer.
  • Finally, our explicit definition of the measure adds a final filter context where the measure only considers rows in the Hours table that have a Category of Billable.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.109.4