## Introduction

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

[ProductSubcategoryName]

**=RELATED(ProductSubcategory[ProductSubcategoryName]**

[ProductCategoryName]

**=RELATED(ProductCategory[ProductCategoryName]**

**Formula 1 – PowerPivot Model Calculated Columns**

## Mixing It Up

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

- a text definition of the cube
- a set of all categories
- a count if the items in the category set
- a list of all of the items in the year set, from the slicer or the dimension if no slicer selections
- a count if the items in the year set
- a list of all of the items in the category set (in columns)
- for each listed category, a set of all associated sub-category items
- for each sub-category set, a count of all items in that set
- a running total of the sub-category set counts
- for each sub-category set, a list of items (in rows)
- 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 valueThisWorkbookdataModel.

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

table.Products

J2:[_setCategory]=CUBESET(_model,”[Product].[ProductCategoryName].Members”,”Categories”)

### 3. Category Set Count

A simple count of the items in the category set, used in later dynamic formulae.

J3:[_setCategoryCount]=CUBESETCOUNT(_setCategory)-2

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.

J4:[_setYear]

=IF(CUBERANKEDMEMBER(_model,Slicer_FiscalYear,1)<>”All”,

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.

J3:[_setYearCount]=CUBESETCOUNT(_setYear)

### 6. List of Category Set Items

A list of the items in the category set, for use in building the sub-category sets.

K2:N2:[_listCategory]=IFERROR(CUBERANKEDMEMBER(_model,$J$2,COLUMN()-COLUMN($J$2)+2),””)

The name definition uses a dynamic formula

=OFFSET(_setCategory,0,1,1,_setCategoryCount)

This is a simple dynamicOFFSETformula to build a list of the categories.

### 7. Sets of Subcategory Items

Sets of sub-category items, one set per category.

K3:N3:[_setsSubcategory]=IFERROR(CUBESET(_model,”EXISTS([Product].[ProductSubcategoryName].Members,”&K$2&”)”,”Products:”&K$2),””)

This is where the MDX magic kicks in. TheEXISTSfunction …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 aCUBESETfunction 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 theCUBESETfunction from the cells in_listCategory.

I prefix the text shown with the textProducts:for readability

The name definition uses a dynamic formula

=OFFSET(_setCategory,1,1,1,_setCategoryCount)

### 8. List of Subcategory Set Counts

A list of counts for the sub-category sets, for use in the layout.

K4:N4:[_setsSubcategoryCounts]=INDEX(CUBESETCOUNT(_setsSubcategories),COLUMN()-COLUMN(_setCategory))

The name definition uses a dynamic formula

=OFFSET(_setCategory,2,1,1,_setCategoryCount)

### 9. List of Subcategory Set Count Totals

A list of running totals for the sub-category set counts, for use in the layout.

K5:O5:[_setsSubcategoryTotals]=SUM($K$4:K4)-K$4+1

The name definition uses a dynamic formula

=OFFSET(_setCategory,3,1,1,_setCategoryCount+1)

### 10. Lists of Sub-category Sets Items

Lists of each set sub-category items, within the relevant category.

K6:N30:[_listSubcategoryies]=IFERROR(CUBERANKEDMEMBER(_model,K$3,ROW()-ROW(_setCategory)-2),””)

This is using the same technique to list the set items as in the previous blog.

The name definition uses a dynamic formula

=OFFSET(_setCategory,4,1,MAX(_setsSubcategoryCounts)-1,_setCategoryCount))

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**

A7:An:=IFERROR(INDEX(_listCategory,MATCH(ROW()-ROW(_start),_setsSubcategoryTotals,1)),””)

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**

B7:Bn:=IFERROR(IF(A7<>A6,””,INDEX(_listSubcategories,COUNTIF($A$7:A7,A7)-1,MATCH(ROW()-ROW(_start),_setsSubcategoryTotals,1))),””)

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

**Filtered Years**

D6:H6:=IFERROR(CUBERANKEDMEMBER(_model,_setYear,COLUMN()-COLUMN(_start)-2),””)

Although the slicer controls the selected years, the year set in_setYearoutsorts the data, the filtering is done there.

**Sales Totals**

C7:C50:=IFERROR(CUBEVALUE(_model,_sales,IF($A7<>$A6,$A7,$A7:$B7),Slicer_FiscalYear),””)

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. TheIFin the formula caters for that possibility of a category row or a category and sub-category row.

**Sales Values**

D7:H50:=IFERROR(CUBEVALUE(_model,_sales,D$6,IF($A7<>$A6,$A7,$A7:$B7)),””)

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

A7:A50:=A7=A6

**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

A7:H50:=AND($A7<>””,$A7<>$A6,ROW($A7)-ROW(_start)-1<=MAX(_setsSubcategoryTotals),COLUMN(A6)-COLUMN(_start)-3<=_setYearCount)

## 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

## Acknowledgements

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.