CHAPTER 37
Adding Formulas to Power Pivot

When analyzing data with Power Pivot, you'll often find that you need to expand your analysis to include data based on calculations that are not in your original data set. Power Pivot has a robust set of functions called data analysis expressions, or DAX functions, which allow you to perform mathematical operations, recursive calculations, data lookups, and much more.

This chapter introduces you to DAX functions, and it provides the ground rules for building your own calculations in Power Pivot data models.

icon

Most of the examples in this chapter are available on this book's website at www.wiley.com/go/excel2019bible. The filename is Power Pivot Formulas.xlsx.

Enhancing Power Pivot Data with Calculated Columns

Calculated columns are columns that you create to enhance a Power Pivot table with your own formulas. Calculated columns are entered directly in the Power Pivot window, becoming part of the source data you use to feed your PivotTable. Calculated columns work at the row level; that is, the formulas that you create in a calculated column perform their operations based on the data in each individual row. For example, imagine you have a Revenue column and a Cost column in your Power Pivot table. You could create a new column that calculated [Revenue] minus [Cost]. This calculation is simple and is valid for each row in the data set.

Calculated measures are used to perform more complex calculations that work on an aggregation of data. These calculations are applied directly to your PivotTable, creating a sort of virtual column that can't be seen in the Power Pivot window. Calculated measures are needed when you need to calculate based on an aggregated grouping of rows, such as the sum of [Year2] minus the sum of [Year1].

Creating your first calculated column

Creating a calculated column works very much like building formulas in an Excel table. Let's continue the walk-through with some sample data; follow these steps to create a calculated column:

  1. Open the Pivot Formulas.xlsx sample file, activate the Power Pivot window (by clicking the Manage button on the Power Pivot Ribbon tab), and then select the Invoice Details tab.
  2. In the table, you will see an empty column on the far right labeled Add Column. Click the first blank cell in that column.
  3. In the Formula bar (see Figure 37.1), enter the following formula:
    =[UnitPrice]*[Quantity]
    Screenshot of the Formula bar to start a calculated column by entering the desired operation.

    FIGURE 37.1 Start your calculated column by entering your desired operation in the Formula bar.

  4. Press Enter to see your formula populate the entire column.
  5. Power Pivot will automatically rename the column to Calculated Column 1. Double-click the column label, and rename the column Total Revenue.

Each calculated column that you create will automatically be available in any PivotTable connected to the Power Pivot data model. You don't have to take any action to get your calculated columns into the PivotTable. Figure 37.2 illustrates the Total Revenue calculated column in the PivotTable Fields list. These calculated columns can be used just as you would any other field in your PivotTable.

Screenshot illustrating the Total Revenue calculated column in the PivotTable Fields list, which can be used as any other field in the PivotTable.

FIGURE 37.2 Calculated columns automatically show up in your PivotTable Fields list.

icon

See Chapter 35, “Introducing Power Pivot,” for a refresher on how to create a PivotTable from Power Pivot.

Formatting your calculated columns

You'll often need to change the formatting of your Power Pivot columns to match the data within them appropriately. For example, you might want to show numbers as currency, remove decimal places, or display dates in a certain way.

You are by no means limited to formatting just calculated columns. These steps can be used to format any column that you see in your Power Pivot window:

  1. In the Power Pivot window, click the column that you want to format.
  2. Go to the Home tab of the Power Pivot window and find the Formatting group (see Figure 37.3).
    Screenshot for using the formatting tools found on the Power Pivot window’s Home tab to format any column in the data model.

    FIGURE 37.3 You can use the formatting tools found on the Power Pivot window's Home tab to format any column in the data model.

  3. Use the option to alter the formatting of the column as you see fit.

Referencing calculated columns in other calculations

Like all calculations in Excel, Power Pivot allows you to reference a calculated column as a variable in another calculated column. Figure 37.4 illustrates this with a new calculated column called Gross Margin. Notice in the Formula bar that the calculation uses our previously created [Total Revenue] calculated column.

Screenshot displaying the new Gross Margin calculation that uses the previously created [Total Revenue] calculated column.

FIGURE 37.4 The new Gross Margin calculation uses the previously created [Total Revenue] calculated column.

Hiding calculated columns from end users

Because calculated columns can reference each other, you can imagine creating columns simply as helper columns for other calculations. You may not want your end users to see these columns in your client tools. In this context, the term client tools refers to PivotTables, Power View dashboards, and Power Map.

Similar to hiding columns on an Excel worksheet, Power Pivot allows you to hide any column (it doesn't have to be a calculated column). To hide columns, simply select the columns you want hidden, right-click the selection, and then choose the Hide from Client Tools option (illustrated in Figure 37.5).

Screenshot of an Excel worksheet enabling to hide columns from the Client Tools option.

FIGURE 37.5 Right-click and select Hide from Client Tools.

When a column is hidden, it will not show as an available selection in your PivotTable Fields list. However, if the column you are hiding is already part of the PivotTable report, meaning that you already dragged it onto the PivotTable, hiding the column in the Power Pivot window will not automatically remove it from the report. Hiding merely affects the ability to see the column in the PivotTable Fields list.

You'll note in Figure 37.6 that Power Pivot recolors columns based on their attributes. Hidden columns are subdued and grayed out, while calculated columns that are not hidden have a darker (black) header.

Screenshot displaying Hidden columns in a shaded display, and calculated columns appear as a darker heading.

FIGURE 37.6 Hidden columns are grayed out, and calculated columns have a darker heading.

Utilizing DAX to Create Calculated Columns

Data Analysis Expression (DAX) is essentially the formula language Power Pivot uses to perform calculations within its own construct of tables and columns. The DAX formula language comes with its own set of functions. Some of these functions can be used in calculated columns for row-level calculations, while others are designed to be used in calculated measures for aggregate operations.

In this section, we'll touch on some of the DAX functions that can be leveraged in calculated columns.

Identifying DAX functions safe for calculated columns

In the previous section, you used the Formula bar within the Power Pivot window to enter calculations. Next to that Formula bar, you may have noticed the Insert Function button labeled fx. This is similar to the Insert Function button found in Excel. Clicking this button will activate the Insert Function dialog box shown in Figure 37.7. This dialog box allows you to browse, search for, and insert the available DAX functions.

Screenshot of the Insert Function dialog box that allows to browse, search for, and insert the available data analysis expression (DAX) functions.

FIGURE 37.7 The Insert Function dialog box shows you all available DAX functions.

As you look through the list of DAX functions, you'll notice that many of them look like the Excel functions with which you are already familiar. Make no mistake, however; these are not Excel functions. Where Excel functions work with cells and ranges, these DAX functions are designed to work at the table and column levels.

To understand what that means, start a new calculated column on the Invoice Details tab. Click the Formula bar, and enter a good old SUM function: =SUM([Gross Margin]). Figure 37.8 shows the result.

Screenshot displaying the DAX SUM ([Gross Margin]) function that can only sum the column as a whole.

FIGURE 37.8 The DAX SUM function can only sum the column as a whole.

As you can see, the SUM function sums the entire column. This is because Power Pivot and DAX are designed to work with tables and columns. Power Pivot has no construct for cells and ranges. It doesn't even have column letters on its grid. Where you would normally reference a range, as in an Excel SUM function, DAX basically takes the entire column.

The bottom line is not all DAX functions can be used with calculated columns. Because a calculated column evaluates at the row level, only DAX functions that evaluate single data points can be used in a calculated column.

A good rule of thumb is if the function requires an array or a range of cells as an argument, then it not viable in a calculated column. Therefore, functions such as SUM, MIN, MAX, AVERAGE, and COUNT don't work in calculated columns. Functions that require only single data point arguments work quite well in calculated columns, such as YEAR, MONTH, MID, LEFT, RIGHT, IF, and IFERROR.

Building DAX-driven calculated columns

To demonstrate the usefulness of employing a DAX function to enhance calculated columns, let's return to our walk-through example. Go to the Power Pivot window, and click the InvoiceHeader tab. If you accidentally closed the Power Pivot window, you can activate it by clicking the Manage button on the Home tab of the Power Pivot Ribbon.

The InvoiceHeader table, shown in Figure 37.9, contains an InvoiceDate column. Although this column is valuable in the raw table, the individual dates aren't convenient when analyzing the data with a PivotTable. It would be beneficial to have a column for Month and a column for Year. This way, you could aggregate and analyze your data by month and year.

Screenshot displaying the DAX functions that help enhance the InvoiceHeader data with Year and Month time dimensions.

FIGURE 37.9 DAX functions can help enhance the InvoiceHeader data with Year and Month time dimensions.

For this endeavor, you use the YEAR(), MONTH(), and FORMAT() DAX functions to add some time dimensions to your data model. Follow these steps:

  1. In the InvoiceHeader table, click the first blank cell in the empty column labeled Add Column on the far right.
  2. In the Formula bar, type =YEAR([InvoiceDate]), and then press Enter.
  3. Power Pivot will automatically rename the column to Calculated Column 1. Double-click the column label, and rename the column Year.
  4. Starting the next column, click the first blank cell in the empty column labeled Add Column on the far right.
  5. In the Formula bar, type =MONTH([InvoiceDate]) and then press Enter.
  6. Power Pivot will automatically rename the column to Calculated Column 1. Double-click the column label, and rename the column Month.
  7. Starting the next column, click the first blank cell in the empty column labeled Add Column on the far right.
  8. In the Formula bar, type =FORMAT([InvoiceDate],"mmm") and then press Enter.
  9. Power Pivot will automatically rename the column to Calculated Column 1. Double-click the column label and rename the column Month Name.

After going through these steps, you should have three new calculated columns similar to the ones shown in Figure 37.10.

Screenshot for using DAX functions to supplement a table with Year, Month, and Month Name columns.

FIGURE 37.10 Using DAX functions to supplement a table with Year, Month, and Month Name columns

As mentioned, creating calculated columns will automatically make them available through your PivotTable Fields list (see Figure 37.11).

Screenshot displaying DAX calculations that are immediately available in any connected PivotTable Fields list.

FIGURE 37.11 DAX calculations are immediately available in any connected PivotTable.

Month sorting in Power Pivot–driven PivotTables

One of the more annoying things about Power Pivot is that it doesn't inherently know how to sort months. Unlike standard Excel, Power Pivot doesn't use the built-in custom lists that define the order of month names. When you create a calculated column like [Month Name] and place it into your PivotTable, Power Pivot will put those months in alphabetical order (see Figure 37.12).

Screenshot for creating a calculated column like [MonthName] and placing it into the PivotTable, and Power Pivot arranges those months in alphabetical order.

FIGURE 37.12 Month names in Power Pivot–driven PivotTables don't automatically sort in month order.

The fix for this is fairly easy. Activate the Power Pivot window and select the Home tab. There you will find the Sort by Column button. Upon clicking the button, the Sort by Column dialog box shown in Figure 37.13 opens.

Screenshot of the Sort by Column dialog box that allows to define how the columns are being sorted.

FIGURE 37.13 The Sort by Column dialog box lets you define how your columns are sorted.

The idea is to select the column you want sorted and then select the column by which you want to sort. In this scenario, we want to sort Month Name by month.

Once you confirm the change, it will initially appear as though nothing happened. This is because the sort order that you defined is not for the Power Pivot window. The sort order is applied to your PivotTable. You can flip over to Excel and see the result in the PivotTable (see Figure 37.14).

Screenshot of a PivotTable displaying the sorted order of the names of months alphabetically.

FIGURE 37.14 Your month names now show in correct month order.

Referencing fields from other tables

Sometimes the operation you are trying to perform with a calculated column requires you to utilize fields from other tables within the Power Pivot data model. For instance, you may need to account for a customer-specific discount amount from the Customers table (see Figure 37.15) when creating a calculated column in the InvoiceDetails table.

Screenshot displaying the Discount Amount value in the Customers table that can be used in a calculated column in another table.

FIGURE 37.15 The Discount Amount value in the Customers table can be used in a calculated column in another table.

To accomplish this, you can use a DAX function called RELATED. Similar to VLOOKUP in standard Excel, the RELATED function allows you to look up values from one table to use them in another.

Take a moment to follow these steps to create a new calculated column that displays a discounted revenue for each transaction in the InvoiceDetails table:

  1. In the InvoiceDetails table, click the first blank cell in the empty column labeled Add Column on the far right.
  2. In the Formula bar, type the following:

        =RELATED(

    As soon as you enter the open parenthesis, a menu of available fields (shown in Figure 37.16) will be displayed. Note that the items in the list represent the table name followed by the field name in brackets. In this case, we're interested in the Customers[Discount Amount] field.

    Screenshot displaying the RELATED function to search a field from another table. The items in the list represent the table name followed by the field name in brackets.

    FIGURE 37.16 Use the RELATED function to look up a field from another table.

  3. Double-click the Customers[Discount Amount] field and then press Enter.
  4. Power Pivot will automatically rename the column to Calculated Column 1. Double-click the column label and rename the column Discount%.
  5. Starting the next column, click the first blank cell in the empty column labeled Add Column on the far righ t.
  6. In the Formula bar, type =[UnitPrice]*[Quantity]*(1-[Discount%]) and then press Enter.
  7. Power Pivot will automatically rename the column to Calculated Column 1. Double-click the column label and rename the column Discounted Revenue.

The reward for your efforts will be a new column that uses the discount percent from the Customers table to calculate discounted revenue for each transaction. Figure 37.17 illustrates the new calculated column.

Screenshot of the final Discounted Revenue calculated column using the Discount% column from the Customers table.

FIGURE 37.17 The final Discounted Revenue calculated column using the Discount% column from the Customers table

Nesting functions

In the previous example, you first created a Discount% column using the RELATED function and then used that column in another calculated column to calculate the discounted revenue.

It's important to note that you don't necessarily have to create multiple calculated columns to accomplish a task like this. You could, instead, nest the RELATED function into the discounted revenue calculation. This would be the syntax for the nested calculation:

=[UnitPrice]*[Quantity]*(1-RELATED(Customers[Discount Amount]))

As you can see, nesting simply means embedding desired functions within a calculation. In this case, instead of using the RELATED function in a separate Discount% field, you can embed it directly in your discounted revenue calculation.

Nesting functions can definitely save time and even improve performance in larger data models. On the other hand, complicated nested functions can be harder to read and understand.

Understanding Calculated Measures

You can enhance the functionality of your Power Pivot reports with another kind of calculation called a calculated measure. Calculated measures are used to perform more complex calculations that work on an aggregation of data. These calculations are not applied to the Power Pivot window like calculated columns. Instead, they are applied directly to your PivotTable, creating a sort of virtual column that can't be seen in the Power Pivot window. You use calculated measures when you need to calculate based on an aggregated grouping of rows.

Imagine you wanted to show the difference in unit costs between the years 2007 and 2006 for each of your customers. Think about what technically has to be done to achieve this calculation. You'd have to figure out the sum of unit costs for 2007, then you'd have to get the sum of unit costs for 2006, and finally you'd have to subtract the sum of 2006 from the sum of 2007. This is a calculation that simply can't be done using calculated columns. Using calculated measures is the only way to get the cost variance between 2007 and 2006.

Follow these steps to create a calculated measure:

  1. Start with a PivotTable created from a Power Pivot model.
  2. Click the Power Pivot tab in the Excel Ribbon, and select MeasuresNew Measure. This will open the Measure dialog box shown in Figure 37.18.
    Screenshot of the Measure dialog box for creating a new calculated measure to get the cost variance between 2007 and 2006.

    FIGURE 37.18 Creating a new calculated measure

  3. In the Measure dialog box, input the following information:
    • Table name    Choose the table that you want to contain the calculated measure when looking at the PivotTable Fields list. Don't sweat this decision too much. The table you select has no bearing on how the calculation works. It's just a preference on where you want to see the new calculation within the PivotTable Fields list.
    • Measure name    Give your calculated measure a descriptive name.
    • Formula    Enter the DAX formula that will calculate the results of your new field.
    • Formatting Options    Specify the formatting for the calculated measure results.

    In this example, we are using the following DAX formula:

    =CALCULATE(

        SUM(InvoiceDetails[UnitCost]),

        YEAR(InvoiceHeader[InvoiceDate])=2007

        )

    This formula uses the CALCULATE function to sum the UnitCost column from the InvoiceDetails table, where the Year column in the InvoiceHeader is equal to 2007.

  4. Click the Check Formula button to ensure that there are no syntax errors. If your formula is well formed, you will see the message “No errors in formula.” If there are errors, you will see a full description of the errors.
  5. Click the OK button to confirm your changes and close the Measure dialog box. You will immediately see your newly created calculated measure in the PivotTable.
  6. Repeat steps 2–5 for any other calculated measure that you need to create.    In this example, we need a measure to show the 2006 cost:

    =CALCULATE(

        SUM(InvoiceDetails[UnitCost]),

        YEAR(InvoiceHeader[InvoiceDate])=2006

        )

    We will also need a measure to calculate the variance:

    =[2007 Revenue]-[2006 Revenue]

Figure 37.19 illustrates the newly created calculated measures. The calculated measures are applied to each customer, displaying the variance between their 2007 and 2006 costs. As you can see, each calculated measure is available for selection in the PivotTable Fields list.

Screenshot illustrating the newly calculated measures that can be seen in the PivotTable Fields list, displaying the variance between their 2007 and 2006 costs.

FIGURE 37.19 Calculated measures can be seen in the PivotTable Fields list.

Editing and deleting calculated measures

You may find that you need either to edit or to delete a calculated measure. You can do so by following these steps:

  1. Click anywhere inside your PivotTable and then click the Power Pivot tab in the Excel Ribbon and select MeasuresManage Measures. This will open the Measure dialog box shown in Figure 37.20.
    Screenshot of the Manage Measures dialog box that allows to edit or delete calculated measures.

    FIGURE 37.20 The Manage Measures dialog box lets you edit or delete your calculated measures.

  2. Select the target calculated measure, and click either the Edit or Delete button.

Clicking the Edit button will activate the Measure dialog box where you can make changes to the calculation setting.

Clicking the Delete button will activate a message box asking you to confirm that you want to remove the measure. After confirming with Yes, the calculated measure will be removed.

Using Cube Functions to Free Your Data

Cube functions are Excel functions that can be used to access the data in a Power Pivot data model outside the constraints of a PivotTable. Although Cube functions are technically not used to create calculations themselves, they can be used to free Power Pivot data so that it can be used with formulas that you may have in other parts of your Excel spreadsheet.

One of the easiest ways to start exploring Cube functions is to allow Excel to convert your PivotTable into Cube functions. The idea is to tell Excel to replace all cells in the PivotTable with a formula that connects back to the Power Pivot data model.

Follow these steps to create your first set of Cube functions:

  1. Start with a PivotTable created from a Power Pivot model.
  2. Select any cell inside the PivotTable and then select PivotTable Tools AnalyzeOLAP ToolsConvert to Formulas, as demonstrated in Figure 37.21.    After a second or two, the cells that used to house a PivotTable are now homes for Cube formulas. The Formula bar shown in Figure 37.22 illustrates a Cube function.
    Screenshot of the PivotTable tools task pane to select the Convert to Formulas option to convert PivotTable to Cube formulas.

    FIGURE 37.21 Select the Convert to Formulas option to convert your PivotTable to Cube formulas.

    Screenshot displaying the Formula bar illustrating a series of Cube functions.

    FIGURE 37.22 These cells are now a series of Cube functions!

If your PivotTable contains a report filter field, the dialog box shown in Figure 37.23 activates. This dialog box gives you the option of converting your filter drop-down selectors to Cube formulas. If you select this option, the drop-down selectors are removed, leaving a static formula.

Screenshot of the Convert to Formulas task pane, where Excel gives the option of converting the report filter fields.

FIGURE 37.23 Excel gives you the option of converting your report filter fields.

If you need to have your filter drop-down selectors intact so that you can continue to change the selections in the filter field interactively, be sure to leave the Convert Report Filters option unchecked when clicking the Convert button.

Now that the values you see are no longer part of a PivotTable object, you can insert rows and columns, you can add your own calculations, or you can combine the data with other formulas in your spreadsheet. Cube functions give you the flexibility to free your Power Pivot data from the confines of a PivotTable and use it in all sorts of ways by simply moving formulas around.

..................Content has been hidden....................

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