CUBEs For Dessert

Microsoft sneaked a couple of ne


A few weeks ago I showed how to create a fully dynamic table using CUBE formulae against a PowerPivot model in Cooking With Cubes.This example presented data from the AdventureWorks database of sales of product categories over years, all integrated with a Fiscal Year slicer.

I recently had a similar need, but with the further challenge of presenting two levels in a hierarchy (actually, this was related tables in a snowflake schema). I needed to show all of the items in the first level, and for each of these items, all of the associated items at the second level including those items that have no sales value. The AdventureWorks database has a similar setup with products, where each sale is for a particular product, each product is of specific product sub-category, and each product sub-category is of a specific product category. Within this model, we would be showing sub-categories of Mountain Bikes and Road Bikes against the category of bikes, but not against any other category.


I will demonstrate my solution using the same AdventureWorks data. Whilst this will be similar to the previous post in showing how to manage the presentation using Excel formulae, it is extended to multiple data levels and some more complex formatting. Additionally, and the main point of this post, I show a technique to display the related values in that hierarchy, not simple with straight CUBE formulae, by adding some MDX trickery in the CUBE formulae.

Figure 1 shows the final results of all of the formulae and formatting.

Figure 1 – Final Results

Preparing With Good Ingredients

First I need to prepare the PowerPivot model so that I have have product name, product sub-category name, and product category name all on the Product table. To achieve this, I added two calculated columns

Formula 1 – PowerPivot Model Calculated Columns

Mixing It Up

The data is ready, so let’s build the required data. I need the following:

  1. a text definition of the cube
  2. a set of all categories
  3. a count if the items in the category set
  4. a list of all of the items in the year set, from the slicer or the dimension if no slicer selections
  5. a count if the items in the year set
  6. a list of all of the items in the category set (in columns)
  7. for each listed category, a set of all associated sub-category items
  8. for each sub-category set, a count of all items in that set
  9. a running total of the sub-category set counts
  10. for each sub-category set, a list of items (in rows)
  11. a reference cell for the sales measure

Taking these in the order given, showing cell(s), defined name, definition:

1. Cube Definition

This is simple text for the connection argument to the cube functions.
J1:[_model]PowerPivot Data
I am using Excel 2010 for this example, Excel 2013 would use the value ThisWorkbookdataModel.
In the previous example, I named this cell _cube, here I use _model.

2. Category Set

This is the set of all categories in the model. This is being taken from the calculated column for product category name in the Products table.

3. Category Set Count

A simple count of the items in the category set, used in later dynamic formulae.
I subtract two from the actual count because this set contains the All member and blank, I am not interested in these.

4. Year Set

This is the set of years in the model, constrained by the slicer.
CUBESET(_model,Slicer_FiscalYear,”Slicer Years”),
CUBESET(_model,”[Date].[FiscalYear].Children”,”Dimension Years”))

As in the previous example, to show all years with no slicer selection (not show All), I check the slicer to see the first set member is All, getting the set from the Date dimsnsion if so, otherwise get the set from the slicer.

6. Year Set Count

A simple count of the items in the year set.

6. List of Category Set Items

A list of the items in the category set, for use in building the sub-category sets.
The name definition uses a dynamic formula
This is a simple dynamic OFFSET formula to build a list of the categories.

7. Sets of Subcategory Items

Sets of sub-category items, one set per category.
This is where the MDX magic kicks in. The EXISTS function … Returns the set of tuples of the first set specified that exist with one or more tuples of the second set specified …, so we can use it within a CUBESET function to get all of the product subcategory names for the category above, in _listCategory.
Using this technique, instead of building a set of all subcategories, we build a set of the subcategories for a particular category; in our case the category passed to the CUBESET function from the cells in _listCategory.
I prefix the text shown with the text Products: for readability
The name definition uses a dynamic formula

8. List of Subcategory Set Counts

A list of counts for the sub-category sets, for use in the layout.
The name definition uses a dynamic formula

9. List of Subcategory Set Count Totals

A list of running totals for the sub-category set counts, for use in the layout.
The name definition uses a dynamic formula

10. Lists of Sub-category Sets Items

Lists of each set sub-category items, within the relevant category.
This is using the same technique to list the set items as in the previous blog.
The name definition uses a dynamic formula
Unlike the other dynamic formulae, this one is multi-column, for the multiple categories.

11. Sales Measure

Finally, we have a cell defining the Sales measure
J7:[_sales]=CUBEMEMBER(_model,”[Measures].[Sum of SalesAmount]”,”Sales”)
This is to save repeated definition within the value formulae.

Figure 2 and Figure 3 below show all of the helper cells populated, and the nanmes defined in the NameManager.

Figure 2 – Populated Helper Cells

Figure 3 – Defined Names

It’s All About The Presentation

All that needs to be done now is to lay it out, allowing for a variable number of categories and a differing variable number of sub-categories within each category.

In my layout, I am repeating each category a number of times equal to to the number of subcategores for that category, but only showing the first instance. The years are shown as column headings, using the year slicer to filter the columns. The values are displayed referencing the category, sub-category, and year headings.

The formulae are as follows:

Category items

This matches the ordinal row number within the list of categories against the running totals for sub-category set counts, so as to determine which of the categories we are listing.


Sub-category items

This uses the same matching technique to determine which sub-category set to list.


Filtered Years

Although the slicer controls the selected years, the year set in _setYear outsorts the data, the filtering is done there.


Sales Totals

This determines the sum of the values for the current row, be that just a category row, or a category and sub-category row, whilst respecting the slicer selection. The IF in the formula caters for that possibility of a category row or a category and sub-category row.


Sales Values

Similar to the sales totals, but using the year heading instead of the slicer, beacuse the year heading has already take the slicewr into account.


Conditional formatting is added to the results to improve the viusual effect and readability.

Product Category Labels

The list of product categories is repeated on every row, changing at the appropriate point of course. This value is needed for the value calculation, but I feel the look is improved if the repeated items are suppressed, so I use conditional formatting to set the font colour to white on repeated items. This uses a simple formula of


Product Category Totals

I list the product category one more time that its associated sub-categories. This is so that I can show category totals as well as category and sub-category totals. These need highlighting so that the totals stand out. To achieve this, I have made them bold with a blue font, borders above and below. The conditional formatting formula for this is


But Is It All Too Rich?

One question that may occur to anyone reading this who is skilled with pivot tables is, why bother? Couldn’t the same effect be achieved by formatting a pivot table showing the product category and sub-category and years with the format set to Show Tabular Form and Repeat All Item Labels? This would give a similar look, but it would only show product categories and sub-categories that have a value, it would not show those that are not selling. Agreed, there is a pivot option to Show items with no data on rows which does address this, but the problem here is that it will show all product sub-categories under EVERY product category, my method only shows the relevant sub-categories.

In Summary

In many ways this is similar to the previous post on CUBE formulae, with an extra level of data and more complex formatting. But what I hope that it shows is that by utilising some fairly straightforward MDX the formulae can do far more complex operations, and extend the data displayed. And who knows how far this can be taken as one’s MDX skills increase?

It is possible that many of the formulas could be replaced with DAX measures in the model, maybe that is a post for another time.

You can download the workbook here


Allan Folting of Microsoft first showed me this technique, I am grateful for his insights.

Chris Webb has a series of excellent blog posts on MDX For PowerPivot Users, which are worth reading if this technique interests you.

Leave a Reply

Your email address will not be published. Required fields are marked *