If your Excel file contains an Excel data model, you have the possibility to query this data model using so-called CUBE formulas within the Excel cells. This allows a high degree of flexibility when creating Excel reports based on the Excel data model. However, if you are working with Excel and the Excel data model, I hope you are familiar with the following two resources:
- Mastering the CUBE Functions in Excel 2013 by Peter Myers and
- Introduction to MDX for PowerPivot users by Chris Webb.
A customer recently asked me how I can query periods from the Excel data model using entries in cells. What he wanted to achieve should look like this:
From my point of view, the solution presented here can be divided into three sub-steps, which I will show below. You can download my example file from here: Download Example file.
Step #1: Reference date values correctly in CUBE formulas
Referencing a date column of the data model within the
CUBEVALUE() function did not initially produce a result for me. I got the following message all the time, when trying a syntax like this:
To find the correct syntax, I created a PivotTable from the data model and converted it into CUBE formulas to see how Excel converts it internally. To do this, follow these steps:
- Switch to Power Pivot window
- Create a PivotTable from the data model (press Home → PivotTable)…
- …in a new Worksheet
- Drag the field date from the fields pane to the line caption of the PivotTable
- Put the Measure Sum Values into the values area
- If the rows of the PivotTable are grouped by years, quarters, … expand at least one year, so that you can see a specific day
- With the PivotTable selected to Analyze in the ribbon and choose OLAP Tools → Convert to formulas
With the 2nd of January 2008 selected, you can see this formula:
The interesting part of the formula is the one marked red. It shows how a date reference should be used in a corresponding CUBE formula. The correct syntax is thus structured as follows: „
YYYY-MM-DDThh:mm:ss„, where the time here always contains zero values. If I want to display the value of my measure „Sum Values“ for January 2nd in a
CUBEVALUE() formula, the formula could look like this:
So I know at this point how to correctly reference date values in CUBE formulas. But what about periods?
Step #2: Reference seamless time periods in CUBE formulas
The thing about time periods in CUBE formulas is that I can’t create them in Excel logic (via >=/ <=), but have to resort to another CUBE function: On CUBESET(). Since I am not an MDX person, I explain the behavior of the
CUBESET() function, related to my example, with my own non-technical words: It let’s me define a (seamless) time period, which I can send to the data model and let’s me retrieve my measure for this time period. A solution for the period January 1st 2018 to May 1st 2018 could look like this:
While the first parameter of the
CUBESET() function is the connection to the data model, the second parameter let’s me specify the period. The colon is particularly important because it defines a time period from the start value (January 1st 2018) to the end value (May 1st 2018). This function now returns correct values, but is not yet linked to the cells in Excel. I’ll take care of that now.
Step #3: Making the CUBE formula refer to cell entries
Linking the CUBE formula to the Excel cells is not very difficult. But you have to pay attention to the correct formatting of the dates and here I have two possibilities:
- I can correctly format the date in the CUBE formula, or
- include auxiliary cells.
For didactic reasons I use variant b).
The values in column E refer to the values in column D which are in the ‚ordinary‘ (German) date format which I have to convert to be suitable for the CUBE functions.
I can now reference these auxiliary cells in my
CUBESET() function to make the result react on the entries in cells D4 and D5:
I hope this is usefull for one or the other 🙂
Many greetings from Germany,
Lars ist Berater, Entwickler und Trainer für Microsoft Power BI. Er ist zertifizierter Power BI-Experte und Microsoft Trainer. Für sein Engagement in der internationalen Community wurde Lars seit 2017 jährlich durch Microsoft der MVP-Award verliehen. Lies hier mehr…