This article shows you how to create a measure based on data in the Data Analysis Expressions (DAX) sample workbook. The workbook includes bike-related data from the AdventureWorks database. For information about where to get the sample workbook, see Get Sample Data for PowerPivot in the TechNet Library. For more information about formulas, see Building Formulas for Calculated Columns and Measures in the TechNet Library.
In this article:
Creating and Editing Measures
A measure is a formula that is created specifically for use in a PivotTable (or PivotChart) that uses PowerPivot data. Measures can be based on standard aggregation functions, such as COUNT or SUM, or you can define your own formula by using DAX. A measure is used in the Values area of a PivotTable. If you want to place calculated results in a different area of a PivotTable, use a calculated column instead (see Create a Calculated Column in the TechNet Library).
When you create a measure, you associate it with a table in the workbook; the measure definition is saved with this table. It appears in the PowerPivot Field List (TechNet Library link) and is available to all users of the workbook.
↑ Back to top
Before you create a measure, you must first add a PivotTable or Pivot Chart to your PowerPivot workbook. When you add the measure, the formula is evaluated for each cell in the Values area of the PivotTable. Because a result is created for each combination of row and column headers, the result for the measure can be different in each cell.
After you have added a PivotTable or Pivot Chart to your PowerPivot workbook, use the Measure Settings dialog box to add a measure that contains a formula. The formula defines a sum, average, or other calculation using the columns and tables in the PowerPivot window. You create standard aggregations the same way that you do in Excel—by dragging fields to the Values field area and then choosing one of the standard aggregation methods: COUNT, SUM, AVERAGE, MIN, or MAX. Custom aggregations are discussed in the next section.
The measure that you create can be used in more than one PivotTable or PivotChart. The name of the measure must be unique within a workbook, and you cannot use the same name that is used for any of the columns in a workbook.
In this example, you will create a custom aggregation that uses one of the new DAX aggregation functions, SUMX, and the function ALL, which in this case returns all values from a column regardless of that column's context. The example uses the following columns from the DAX sample workbook:
DateTime[CalendarYear]
ProductCategory[ProductCategoryName]
ResellerSales_USD[SalesAmount_USD]
The example uses a PivotTable that has CalendarYear as a row label and ProductCategoryName as a column label; SalesAmount_USD is used in the measure formula. The example answers the question: what percentage of total sales from 2001-2004 did each year and product category contribute? This enables you to see, for example, what percentage of the total was contributed by bike sales in 2003. In order to answer this question, we use the following measure formula:
=SUMX(ResellerSales_USD, ResellerSales_USD[SalesAmount_USD]) / SUMX(ALL(ResellerSales_USD), ResellerSales_USD[SalesAmount_USD])
The formula is constructed as follows:
The numerator, SUMX(ResellerSales_USD, ResellerSales_USD[SalesAmount_USD]), is the sum of the values in ResellerSales_USD[SalesAmount_USD] for the current cell in the PivotTable. Having the context of CalendarYear and ProductCategoryName means that this value will be different for each combination of year and product category. For example, the total number of bikes sold in 2003 is different from the total number accessories sold in 2004.
For the denominator, you start by specifying a table, ResellerSales_USD, and use the ALL function to remove all context on the table. This ensures that the value will be the same for each combination of year and product category: the denominator will always be total sales from 2001-2004.
You then use the SUMX function to sum the values in the ResellerSales_USD[SalesAmount_USD] column. In other words, you get the sum of ResellerSales_USD[SalesAmount_USD] for all reseller sales.
Note: In Windows Vista and Windows 7, features in the PowerPivot window are available on a ribbon, which is discussed in this article. In Windows XP, features are available from a set of menus. If you are using Windows XP and want to see how the menu commands relate to the ribbon commands, see The PowerPivot UI in Windows XP in the TechNet Library.
In the PowerPivot window, click the Home tab, and in the Reports group, click PivotTable.
In the Create PivotTable dialog box, verify that New Worksheet is selected, and click OK.
PowerPivot creates a blank PivotTable in a new Excel worksheet and displays the PowerPivot Field List on the right side of the workbook.
In the Excel window, use the PowerPivot Field List to add columns to the PivotTable:
Find the DateTime table, and drag the column CalendarYear to the Row Labels area of the PivotTable.
Find the ProductCategory table, and drag the column ProductCategoryName to the Row Labels area of the PivotTable.
In the Excel window, on the PowerPivot tab, in the Measures group, click New Measure.
In the Measure Settings dialog box, for Table name, click the down arrow, and select ResellerSales_USD from the dropdown list.
The choice of table determines where the definition of the measure will be stored. It is not required for the measure to be stored with a table that the measure references.
For Measure Name (All Pivot Tables), type AllResSalesRatio.
This name is used as an identifier for the measure; therefore, it must be unique within the workbook, and cannot be changed.
For Custom Name (This PivotTable), type All Reseller Sales Ratio.
This name is used only within the current Pivot Table, for display purposes. For example, you might reuse the measure, AllResSalesRatio, in other PivotTables but give it a different name, or use a different language.
In the Formula text box, position the cursor after the equal sign (=).
Type SUMX, and then a parenthesis.
=SUMX(
As you type, the tooltip beneath the Formula textbox indicates that the SUMX function requires two arguments: the first argument is a table or an expression that returns a table, and the second argument is an expression that provides the numbers that can be summed.
Type Res, and then select ResellerSales_USD from the list, and press TAB.
The column name is inserted into the formula as follows:
=SUMX(ResellerSales_USD
Type a comma.
The tooltip updates to show that the next argument required is expression. An expression can be a value, a reference to a column, or some combination of those. For example, you could create an expression that sums two other columns. For this example, you will provide the name of a column that contains the sales amount for each reseller.
Type the first few letters of the name of the table that contains the column that you want to include. For this example, type Res, and select the column ResellerSales_USD[SalesAmount_USD] from the list.
Press TAB to insert the column name into the formula, and add a closing parenthesis, as shown here:
=SUMX(ResellerSales_USD, ResellerSales_USD[SalesAmount_USD])
Type a forward slash, and then type or copy and paste the following code into the Measure Settings dialog box:
SUMX(ALL(ResellerSales_USD), ResellerSales_USD[SalesAmount_USD])
Note how the ALL function is nested inside the SUMX function. The entire formula now appears as follows:
Click Check formula.
The formula is checked for syntax or reference errors. Resolve any errors that might have been found, such as a missing parenthesis or comma.
Click OK.
The measure now populates the PivotTable with values for each combination of calendar year and product category.
Format the table:
Select the data in the PivotTable, including the Grand Total row.
On the Home tab, in the Number group, click the percent button (%) once, and then click the increase decimal button (<- .0 .00) twice.
The finished table should appear as below. You can now see the percentage of total sales for each combination of product and year. For example, bike sales in 2003 accounted for 31.71% of all sales from 2001-2004.
To view the definition of an existing measure, you use the PowerPivot Field List (TechNet Library link). The PowerPivot Field List contains a list of all tables in the current PowerPivot window, including columns of raw data, calculated columns, and any measures that you might have defined. You can right-click on the definition of any measure and select Edit formula to open a dialog box that lets you view and modify the measure definition.
In the Excel window, click anywhere in the PivotTable or PivotChart area to display the PowerPivot Field List.
In the PowerPivot Field List, locate the table that contains the measure that you created.
Each table can contain base columns, calculated columns and measures. Measures are indicated by a small calculator icon to the right of the measure name.
For this example, right-click SumAmtByReseller, and click Edit Formula.
In the Measure Settings dialog box, edit the formula.
You can also change the measure name or custom name, and the associated table.
Ed Price MSFT edited Revision 9. Comment: Updated "TechNet Library" link list at the bottom.
Ed Price MSFT edited Revision 8. Comment: Added " in the TechNet Library" a few times.