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.
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
- Build a PivotTable on the Data Model with
Products[Category]in Rows and theRevenueandOrdersmeasures in Values (from Your first DAX measures: SUM, AVERAGE, COUNTROWS, DISTINCTCOUNT). - Click inside it and choose PivotTable Analyze › Calculations › OLAP Tools › Convert to Formulas.
- 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.
| A | B | C | |
|---|---|---|---|
| 1 | Category | Revenue | Orders |
| 2 | Accessories | 745 | 4 |
| 3 | Computers | 4950 | 4 |
| 4 | Displays | 945 | 2 |
| 5 | Total | 6640 | 10 |
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].
=CUBEMEMBER("ThisWorkbookDataModel","[Products].[Category].&[Computers]")
=CUBEMEMBER("ThisWorkbookDataModel","[Measures].[Revenue]")
=CUBEMEMBER("ThisWorkbookDataModel","[Measures].[Sum of Amount]")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:
=CUBEVALUE("ThisWorkbookDataModel","[Measures].[Revenue]","[Products].[Category].&[Computers]","[Customers].[Region].&[North]")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.
=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:
=CUBEVALUE("ThisWorkbookDataModel",$A3,B$1,Slicer_Region)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:
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]")CUBESETdefines a set (all products), gives it a caption that the cell displays, and sorts it:2means descending by the sort measure given in the last argument.CUBERANKEDMEMBERreturns the n-th member of the set: rank 1 is the best seller.CUBEVALUEfetches the revenue for that member.CUBESETCOUNT ( E1 )would return the number of products in the set.
| A | B | C | |
|---|---|---|---|
| 1 | Rank | Product | Revenue |
| 2 | 1 | Laptop | 2850 |
| 3 | 2 | Tablet | 2100 |
| 4 | 3 | Monitor | 945 |
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
| Symptom | Likely cause |
|---|---|
| #N/A | A 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 cell | The combination has no data; the measure returned blank. |
| Old numbers | Cube formulas do not recalculate data on their own. Use Data › Refresh All after the source changes. |
[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?
Cube functions use ThisWorkbookDataModel for the internal model.
2. How do you turn an existing PivotTable into cube formulas?
Convert to Formulas replaces the PivotTable with CUBEMEMBER and CUBEVALUE formulas.
3. How is a measure written in a member expression?
Measures belong to the [Measures] dimension.
4. Which function returns the n-th item of a sorted set?
CUBERANKEDMEMBER takes a set and a rank.
5. How do you make a CUBEVALUE formula respond to a slicer?
Slicer names can be passed as member arguments.