Excel: Reporting Text in a Pivot TableBy
Pivot tables are excellent for summarizing numbers. At one of my Power Excel seminars recently, someone wanted to show a text field in the Values area of a pivot table. Thanks to the Data Model and the new DAX function CONCATENATEX introduced in 2017, you can build such a pivot table.
The attendee said, “I have a data set showing the prior and current status for support tickets. I want to report the text from the Status field in the Values area of a pivot table.” While the Data Model, introduced in Excel 2013, and CONCATENATEX provide a solution, these calculations are only available in Windows versions of Excel. They won’t work in Excel for Android, Excel for iOS, or Excel for Mac.
DAX stands for Data Analysis eXpressions. The DAX formula language is a new set of functions for creating calculated fields in a pivot table. While many of the functions are similar to the functions in regular Excel, there are several powerful additions that allow calculations previously impossible in a pivot table.
In order to use DAX in a pivot table, follow these steps:
1. Select one cell in your data set and press Ctrl+T (or go to Home, Format as Table).
2. By default, the new table will be called Table1. Click on the Table Tools Design tab in the Ribbon and assign the table a name. The table name can’t have spaces. A name such as “TicketData” would work.
3. Select one cell in the table. From the Insert Tab, choose Pivot Table.
4. In the Create Pivot Table dialog, choose the box for “Add this data to the Data Model.”
5. A new worksheet will appear with the Pivot Table Fields list. Start to build your pivot table by dragging fields to the Rows and Columns area. When your pivot table is based on the Data Model, there will be a few subtle differences in the Pivot Table Fields list. First, the words “Active and All” allow you to add more data sets to the pivot table. Second, the name of the table appears at the top of the fields from that table.
6. Right-click the name of the table and choose Add Measure.
Note: The word “Measure” is a database professional’s word for Calculated Field. When the Power Pivot add-in debuted in Excel 2010, the calculated fields were called Measures. Microsoft tried to soften the word in Excel 2013, and the menu choice in Figure 1 appeared as Insert Calculated Field. This was designed to be more familiar for people using Excel. But Excel pivot tables already offer a different feature called Calculated Fields. To avoid confusion, the term changed to “Measure” in Excel 2016. I would have preferred a completely new term, such as “Super Amazing Calculated Field.”
7. In the Measure dialog, type a measure name such as “StatusResults.”
8. Enter the formula =CONCATENATEX(TicketData,[Status],”, “).
9. Click the Check DAX Formula button to make sure the syntax is correct.
10. You can specify the number format for Measures, which I think is great. For a text result, however, the only valid choice is General, so leave the number format as General.
Note: The syntax for CONCATENATEX is (Table Name, Expression, Delimiter). In the formula in Step 8, TicketData corresponds to the name that you used in Step 2, and [Status] is the name of the field in the source data. To use the AutoComplete feature in the Create Measure dialog, type a left square bracket. The AutoComplete list will show a list of fields from your data set. Click on one name, and press Tab.
11. Click OK to create the new calculated field. The calculation won’t show up in the pivot table automatically. Instead, a new field will appear in the Pivot Table Fields list. Drag the new field to the Values area.
The Subtotal and Grand Total results for a text field might be unexpected. The Grand Total Column of Apple and Quince will be “Apple, Quince.” The Subtotal for the Central region will be “Orange, Apple, Apple, Vanilla, Elderberry, Mango, Watermelon.” As you can imagine, the bottom-right cell containing the grand total will contain one word for every row in the original data. These results cause the pivot table columns to be unreasonably wide.
In many cases, the pivot table will look better if you remove the grand totals and subtotals:
12. Select Design, Grand Totals, Off For Rows and Columns.
13. Right-click on the first Subtotal label in Column A and unselect Subtotal Region.
The result is a pivot table with text in the Values area.
Just like any other pivot table, you can rearrange the fields in the Rows and Columns area and the results will recalculate.
Download this month’s workbook to try it out for yourself: SF-2018-07-TextValues.
Update from Bill Jelen: After using this method for a few weeks, I and others noticed that in some data sets, the concatenated values would contain duplicates. You can remove the duplicates by changing =CONCATENATEX(TicketData,[Status],”, “) to =CONCATENATEX(Values(TicketData[Status]),TicketData[Status],”, “). (Thanks to Rob Collie at PowerPivotPro.com for suggesting the updated formula.)
The VALUES function returns a new table with the unique values found in a column.
The DAX formula language offers many new calculations for your pivot tables.