Power Pivot & DAX course · Lesson 15 of 16

Cube functions: free-form reports from the Data Model

Build flexible Excel reports on Power Pivot with CUBEVALUE, CUBEMEMBER, CUBESET and CUBERANKEDMEMBER: Convert to Formulas, slicers and top-N lists.

Advanced 18 min read

A PivotTable is a superb tool for exploring, but a rigid one for reporting. You cannot insert a blank row between two categories, put a comment next to one number, or mix figures from different layouts in one neat table. Cube functions solve this. They are ordinary worksheet functions that fetch individual values from the Data Model, so each number can sit in any cell, with any formatting, next to any other formula. They are the professional way to build pixel-perfect Excel reports on a Power Pivot model.

The seven cube functions are CUBEVALUE, CUBEMEMBER, CUBESET, CUBERANKEDMEMBER, CUBESETCOUNT, CUBEMEMBERPROPERTY and CUBEKPIMEMBER. Unlike the DAX functions in the rest of this course, they are Excel functions typed in worksheet cells. In practice you need the first four.

The fastest start: Convert to Formulas

  1. Build a PivotTable on the Data Model with Products[Category] in Rows and the Revenue and Orders measures in Values (from Your first DAX measures: SUM, AVERAGE, COUNTROWS, DISTINCTCOUNT).
  2. Click inside it and choose PivotTable Analyze › Calculations › OLAP Tools › Convert to Formulas.
  3. If the PivotTable has report filters, Excel asks whether to convert them too. Click Convert.

The PivotTable disappears and every cell becomes a formula that returns the same value. Row labels become CUBEMEMBER formulas and values become CUBEVALUE formulas. You can now insert rows, move cells, delete the grand total or add a column of comments: each formula keeps working because it no longer depends on its position.

B3fx=CUBEVALUE("ThisWorkbookDataModel",$A3,B$1)
ABC
1CategoryRevenueOrders
2Accessories7454
3Computers49504
4Displays9452
5Total664010

After Convert to Formulas: A2:A4 contain CUBEMEMBER formulas, B1 and C1 CUBEMEMBER formulas for the measures.

Result: 4950

The formula in B3 reads: from the connection ThisWorkbookDataModel, get the value at the intersection of the member in A3 (Computers) and the member in B1 (the Revenue measure). The result, 4,950, is the same as the PivotTable showed. Format the cells with the usual number formats; cube formulas return raw numbers.

The building blocks

The connection name

Every cube function starts with a connection. For the workbook's own model it is always the text "ThisWorkbookDataModel". (Cube functions can also connect to external Analysis Services cubes, which is where the name cube comes from.)

Member expressions

Members are identified with MDX-style names in square brackets: [Table].[Column].[Item]. The ampersand version [Table].[Column].&[Key] refers to the item by its key and is what Convert to Formulas produces. Measures live in a special dimension called [Measures].

DAX
=CUBEMEMBER("ThisWorkbookDataModel","[Products].[Category].&[Computers]")
=CUBEMEMBER("ThisWorkbookDataModel","[Measures].[Revenue]")
=CUBEMEMBER("ThisWorkbookDataModel","[Measures].[Sum of Amount]")
Excel formulas: a category member, an explicit measure and an implicit measure.

A CUBEMEMBER cell displays the member's caption (Computers, Revenue) but behaves as a reference to the member when other cube formulas point at it. This is why the reports are so flexible: change A3 to refer to a different category and every value in that row follows.

CUBEVALUE with several filters

CUBEVALUE accepts any number of member expressions after the connection; together they define the filter context, exactly like the row, column and slicer filters of a PivotTable. You can type them directly instead of pointing at CUBEMEMBER cells:

DAX
=CUBEVALUE("ThisWorkbookDataModel","[Measures].[Revenue]","[Products].[Category].&[Computers]","[Customers].[Region].&[North]")
Returns 2,320: computer revenue from North customers (orders 1001 and 1009).

The DAX measure is evaluated in that filter context, so everything you learned about Filter context and row context in DAX applies. A measure using ALL ( Products ) in its denominator still returns a share of the total when called from a cube formula.

Tip. Type =CUBEVALUE("ThisWorkbookDataModel","[ and Excel's IntelliSense lists the tables, then the columns, then the items of the model. Accept each suggestion with Tab. This avoids typing errors in long member names.

Connecting slicers to cube formulas

Insert a slicer with Insert › Filters › Slicer, choose the Data Model tab in the Existing Connections dialog and select Customers[Region]. Every slicer has a name, shown in Slicer › Slicer Settings (for example Slicer_Region). Pass that name as an extra argument to CUBEVALUE and the formula follows the slicer:

DAX
=CUBEVALUE("ThisWorkbookDataModel",$A3,B$1,Slicer_Region)
Slicer name without quotes, as an extra member argument.

With the slicer set to North, Computers shows 2,320, Accessories 175 and Displays shows nothing, because Anna Weber never bought a monitor.

Top N lists with CUBESET and CUBERANKEDMEMBER

A sorted top-three list is a classic report element that a PivotTable can do only with filters. With cube functions it takes three formulas:

DAX
E1: =CUBESET("ThisWorkbookDataModel","[Products].[Product].[All].Children","Products by revenue",2,"[Measures].[Revenue]")
F2: =CUBERANKEDMEMBER("ThisWorkbookDataModel",$E$1,D2)
G2: =CUBEVALUE("ThisWorkbookDataModel",F2,"[Measures].[Revenue]")
D2:D4 hold 1, 2, 3. Copy F2:G2 down to row 4.
  • CUBESET defines a set (all products), gives it a caption that the cell displays, and sorts it: 2 means descending by the sort measure given in the last argument.
  • CUBERANKEDMEMBER returns the n-th member of the set: rank 1 is the best seller.
  • CUBEVALUE fetches the revenue for that member. CUBESETCOUNT ( E1 ) would return the number of products in the set.
ABC
1RankProductRevenue
21Laptop2850
32Tablet2100
43Monitor945

Top three products, from CUBESET, CUBERANKEDMEMBER and CUBEVALUE (shown here as values).

When the data changes, refresh with Data › Refresh All; the ranking updates automatically. While values are being retrieved, cells briefly display #GETTING_DATA.

Cube functions or GETPIVOTDATA?

GETPIVOTDATA also pulls numbers out of a PivotTable, but it depends on the PivotTable existing and showing the item. Cube functions talk to the model directly: no PivotTable is needed, hidden items can be retrieved, and any combination of filters is possible. The trade-off is longer formulas and a refresh step. For one-off lookups GETPIVOTDATA is fine; for a report that will be reused every month, cube functions are more robust.

Troubleshooting

SymptomLikely cause
#N/AA member name is misspelled or the item does not exist (for example a category renamed in the source). Check the expression with IntelliSense.
#VALUE!A cell referenced as a member is not a CUBEMEMBER or CUBESET cell, or a connection name is wrong.
Empty cellThe combination has no data; the measure returned blank.
Old numbersCube formulas do not recalculate data on their own. Use Data › Refresh All after the source changes.
Watch out. Building cube-formula reports requires the Data Model, so in practice you create them in Excel for Windows. Keep the measure names stable: renaming a measure in Power Pivot breaks every [Measures].[Old name] reference in the workbook.

The final lesson, Project: a sales dashboard with Power Pivot and DAX, uses cube formulas for the headline figures of a dashboard.

Check yourself

1. What is the connection name for the workbook's own Data Model?

2. How do you turn an existing PivotTable into cube formulas?

3. How is a measure written in a member expression?

4. Which function returns the n-th item of a sorted set?

5. How do you make a CUBEVALUE formula respond to a slicer?

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 11 KB