Welcome to MSDN Blogs Sign in | Join | Help

Microsoft Excel

The team blog for Microsoft Excel and Excel Services.
PivotTables: Calculated Items

Today's author: David Gainer, a Program Manager on the Excel team.

PivotTables are designed to help users make sense of large amounts of data by providing an easy way to build a summarized report. In addition, PivotTables can be rearranged easily, so that once you have some summary data in a PivotTable, you can look at the same information in many different ways with only a few mouse clicks. For a slightly less brief overview of PivotTables as well as a list of the improvements made to the application in Excel 2007, please check out this blog post.

Today I wanted to take a look at calculated items in PivotTables … specifically, what they are, how to create them, and some examples of how they can be useful. I find them to be very helpful, so I wanted to do a little evangelism, as well as point out some little-known features.

Let's start with what they are. According to Microsoft Knowledge Base Article 161882, "A calculated item is a user-defined item in a PivotTable field that can perform calculations using the contents of other fields and items in the PivotTable." While that's technically accurate, it may not be clear. Perhaps a simpler way to think about calculated items is that it is a feature that allows you to create new items (that are not part of the original source data) that can appear in fields in your PivotTable. Here's an example (lifted from the aforementioned KBA) – a picture may be worth 1000 words.

Assume that you have some sales data that you want to summarize in a PivotTable. You have data for four different sales regions (North, South, East, West) for a few months sales.

With a few clicks, you could create a summary PivotTable that looks like this.

So far so good, but let's now pretend there has recently been a reorganization, and the North and East regions are now the NorthEast region. How can you reflect that in your PivotTable without having to modify all the source data? By creating a calculated item which represents the NorthEast region, of course. To do that, you need to first click one of the "Regions" in the PivotTable report. This gives Excel the context of what you are going to add a calculated item to … specifically, that you are going to add an item to "Regions". Next, on the PivotTable "Options" contextual ribbon, select the "Formulas" drop-down and then select "Calculated Item …".

This should present you with a dialog thus:

This dialog is used to create calculated items (unsurprisingly enough). While it may look a bit cryptic, it is pretty straightforward. The bottom-left control shows you the fields available in your PivotTable, the bottom-right control shows you the items in those fields, and the top controls allow you to create your calculated items. For example, to create a new NorthEast region, you simply need to type "NorthEast" in the Name control, = North + East in the Formula control …

… press the Add button, and then press OK to dismiss the dialog. Now you should see your PivotTable with a new item – NorthEast – listed with the rest of the Regions. You can see that Excel simply aggregated the values for the two regions in questions, which is what we asked Excel to do with our "= North + East" formula. Essentially, you have added new information to your PivotTable, and that information behaves just like all the other data in the PivotTable … you can sort it, filter it, pivot it, etc., all without restriction.

If I now use standard PivotTable filtering functionality to filter out the North and East regions, I have a simple three-line PivotTable that reflects the reorganized sales regions, not the underlying source data.

The fun is not quite over yet, though. Once you have created a calculated item, you can continue to modify it without necessarily returning to the dialog we just looked at. For starters, you can type over the caption "NorthEast" and Excel will remember the new caption. Additionally, if you click on one of the calculated fields, you will see that you can see the formula you entered in the dialog is also available (and editable) in the Excel formula bar. Here is a shot, for example, of what happens when I select one of the cells reporting data for the new NorthEast region and hit F2 (the hotkey to edit a formula).

From here I could change the formula (=North+South), add a constant (=North+East+100), or use any of Excel's native functions, and the value in the cell would update when I pressed Enter. It is worth bearing in mind that this changes the formula for that intersection only (in this case, "Apr" of the "NorthEast" region only, so if you want to change all the cells in one fell swoop, best to return to the Calculated Items dialog.

So that is an overview of the feature set. Now let's look at a few examples of more interesting usage cases that were created by one of my co-workers. First, let's look at an example where calculated items are used to show relationships between different classes of products.

As you can see the PivotTable above summarizes sales, cost of sales, and margin for different classes of products – alcoholic beverages, dairy, beverages, etc. In this case, the report author wanted to add another piece of information – the percentage of overall sales that were related to alcoholic beverages. They accomplished this by adding a calculated item to the "Product Department" field that looked like this: =IF('Alcoholic Beverages'=0,NA(),'Alcoholic Beverages'/SUM('Alcoholic Beverages',Beverages,Dairy)). You can see this calculation uses a few Excel functions (IF, NA, SUM) in addition to referencing other items in the field in order to calculate the percentage in question. The reason for the IF and the NA functions is to return #NA if there are no alcoholic sales instead of a divide by 0 error.

Here is another interesting example. It turns out you can use a special syntax to refer to "next" and "previous" items in a field.

In this example, the PivotTable author had actual financial results for FY02 – FY04, and wanted to create a forecast for the next fiscal year based on the actual for those three years. You can see in the screenshot above that they used the following formula: =IF(Year[-1]=0,Year[-2],2*Year[-1]-Year[-2]). While at first this may look pretty cryptic, it is actually fairly straightforward. The formula says "if last year's sales (which is what "Year[-1]" means) is 0, the projection is sales from 2 years ago, else the projection is two times last year's sales minus the sales from two years ago." The [-1] modifiers are simply used to tell Excel to refer to previous and next values in that field, and it is particularly (but not exclusively) useful for financial calculations involving time periods.

That summarizes the calculated items feature. There is a closely related feature called "calculated fields" which should be simple enough to figure out after reading this post (and there are other articles out there on MSFT and other web sites you can refer to if you are interested). If you have any feedback on this feature, we are all ears. Thanks!

Posted: Thursday, May 08, 2008 10:07 PM by Joseph Chirilov
Filed under: ,

Comments

cbosleeds said:

I really like the forecasting equation - very slick. I might have a look at playing with something like this myself.

# May 9, 2008 3:58 AM

TechieBird said:

Thanks for a great post - I use Pivots all the time and thought I was a dab hand, so I'm scratching my head over how I didn't find this feature before!

You've now prompted me to have a proper dig around to see what else I'm missing...

# May 9, 2008 6:40 AM

Chyrsostom said:

This is a nice feature, but it's performance seems slows against an external SQL 2005 table that has about 150k rows and 15 columns. It took about 5 minutes to update a budget variance calculation in this scenario. Without the calculated item, the performance is sub-second.

# May 9, 2008 10:04 AM

Ivan Trofimov said:

quote: "They accomplished this by adding a calculated item to the "Product Department" field that looked like this: =IF('Alcoholic Beverages'=0,NA(),'Alcoholic Beverages'/SUM('Alcoholic Beverages',Beverages,Dairy)). You can see this calculation uses a few Excel functions (IF, NA, SUM) in addition to referencing other items in the field in order to calculate the percentage in question. The reason for the IF and the NA functions is to return #NA if there are no alcoholic sales instead of a divide by 0 error."

this does not make sense, couse the #DIV/0! error will appear only if the consequent of a dovision is null, i.e. sum of all classes. Value for true event as described in formula will be zero by itself.

ps the other stuff is cute

# May 12, 2008 4:36 AM

John said:

Using this same concept would it be possible to add Sparklines to a Pivot Table with the REPT() function?

# May 12, 2008 9:59 AM

Pat said:

This is great.  Does this function work with cube (SQL) based pivot tables?  Adding new calculations only seems to be manual.

# May 13, 2008 6:39 PM

sam said:

David ...this  is nothing new....Its been there right from Excel 97....so thats for about 10 years...probably before your times

We were hoping to see some improvements to this feature in 2007.. for example

a) Ability to have both calculated field and Calculated Items in the same pivot....

b) Ability to refer to a field in a formula in a caluclated field or item

Example if('Product Family' = "BC", 10%*Sales,0)

c)Ability to use names and array formulas in a caulated filed or item...

Sam

# May 16, 2008 7:46 AM

David Gainer said:

Chyrsostom - thanks, I will take a look.

John - unfortunately no, since this feature cannot support references in the calculation

Pat - unfortunately, no.  That's an area we are thinking about.

Sam - you would be suprised at how old I am :-)

You are correct that this feature is not new, but it is one of those features that is not well known, so I figured a little evangelism couldn't hurt.  Your feedback is on-target, and we appreciate it.

# May 19, 2008 6:49 PM

Colin Banfield said:

Dave, it's good to see you back here after your sabbatical :-) I agree that the article could benefit the many folks who aren't familiar with these PivotTable tools. For the rest of us, calculated measures (items & fields) reminds me of a sensitive tooth - you get so used to workarounds that eventually you ignore the problem (in this case, the limitations).

This is changing the subject, but in terms of PivotTable features with limitations, nothing comes close to PivotCharts.  In fact, I'll go out on a limb to say that if PivotCharts were able to properly represent multidimensional PivotTable data visually, it would be one of the most powerful feature in all of Excel.  

# May 20, 2008 4:14 PM

Dating said:

Today's author: David Gainer, a Program Manager on the Excel team. PivotTables are designed to help users make sense of large amounts of data by providing an easy way to build a summarized report. In addition, PivotTables can be rearranged easily, so

# May 31, 2008 11:58 AM

Weddings said:

Today's author: David Gainer, a Program Manager on the Excel team. PivotTables are designed to help users make sense of large amounts of data by providing an easy way to build a summarized report. In addition, PivotTables can be rearranged easily, so

# June 5, 2008 6:19 AM
New Comments to this post are disabled
Page view tracker