Personally, I struggled with this for a while. I could tell when the most current record in the database file was, but that’s sometimes not enough. As we run a golf course, we have days with no revenue due to snow or heavy rain. And in that case, we actually want to know that no revenue was present, so picking up the last posted transaction just doesn’t cut it.
The solution to this is actually really easy though. It consists of three steps:
- Create a simple SQL query
- Create a measure
- Create an OLAP Cube formula
(Please note that the steps listed are for PowerPivot 2012, but an alternate tip for 2008 is provided at the bottom of the article.)
Creating the SQL QueryTo begin with, you need to go into PowerPivot and create a connection to your database. These steps assume that you already have a connection in place to one (or more) of these databases.
To start with:
- Go to PowerPivot-->Design-->Existing Connections
- Select your existing PowerPivot Data Connection and click “Open”
- Choose “Write a query that will specify the data to import” and click “Next”
- Enter the appropriate SQL Query as shown below:
|SQL Server:||SELECT GETDATE() AS Updates|
|Microsoft Access:||SELECT NOW() AS Updates|
- Click Validate to ensure your SQL is well formed
- Click Finish
At this point, you’ll have a one cell table that looks something like this:
The nice thing here is that every time you update your PowerPivot data, the query will run and return the date/time stamp that you did so.
Creating The MeasureThe next step is that we need to create a measure to return the max value from the Updates column. Now granted, there will only ever be one value in there, but creating the measure allows us to talk to the OLAP formula later.
Go to the measures pane (at the bottom of the table) and enter the following formula in the first cell of the first column:
Returning LastUpdated To ExcelNow, you could build a one cell PivotTable on your worksheet to return this info, but then you’d have to refresh it all the time. And that’s way too much work. Instead, drop the following CUBE formula into a cell:
=CUBEVALUE("PowerPivot Data",CUBEMEMBER("PowerPivot Data","[Measures].[LastUpdated]"))
- Right click the cell
- Choose Format Cell-->Number-->Custom
- Place the following in the “Type” area: yyyy-mm-dd h:mm:ss AM/PM
- Click OK
Of course, you could also skip all that, and just place the following formula in a cell too:
="Last Updated "&TEXT(CUBEVALUE("PowerPivot Data",CUBEMEMBER("PowerPivot Data","[Measures].[LastUpdated]")),"yyyy-mm-dd h:mm:ss AM/PM")
Notes For Developing With PowerPivot 2008Unfortunately it’s not quite as easy to create a measure in the first version of PowerPivot. While steps 1 and 3 remain the same, you’ll need to create a PivotTable to get your measure.
The basic steps are to create a PivotTable, drop the Updates field in to the Values area, and change it to show the Max. You’ll then be able to reference the name of that measure in the OLAP formula.