360 Practical PowerPivot & DAX Formulas for Excel 2010
The formula is counting the number of rows in the current filter context for the
product name. If the number of rows is 1, then the current context is at the individual
product level. The third parameter for the IF() function is missing. That’s the false
result, and if it’s missing, it returns a blank—or you could explicitly use BLANK(). The
result is shown in Figure 12-13.
An interesting problem arises when you aggregate UnitsInStock across time. Here, you
may want to see a total. The most meaningful total for a product would be equal to the
last stock-take figure for that product. In SSAS, there are two useful aggregations called
LastChild and LastNonEmpty for working with inventory figures. Fortunately, there is
a DAX equivalent of LastNonEmpty, the DAX function LASTNONBLANK(). In order to
demonstrate this, we need inventory figures across time. The Northwind UnitsInStock
column is a static picture, and the stock-take is not dated. However, if you have the SQL
Server sample database, ContosoRetailDW (you can download it from www.microsoft.
com—search on ContosoRetailDW), you can look at inventory figures across time—
you’ll need the DimDate and FactInventory tables. The practical shows how to handle
numbers that do not add up over time. Here are the step-by-step instructions:
1. Add the DimDate, DimProduct, and FactInventory tables from ContosoRetailDW
to a PowerPivot model. DimDate contains dates, and FactInventory includes
stock-take figures for different dates and products. DimProduct is added so you
can see the product name, rather than a product key. You may need to verify the
relationships between these tables. FactInventory should be related to DimDate
through the DateKey column. It should also be related to DimProduct through
the ProductKey column. When you add tables separately, the relationships in the
source data are not discovered by PowerPivot.
2. Insert a pivot table and add CalendarYear (from DimDate) to Column Labels,
ProductName (from DimProduct) to Row Labels, and OnHandQuantity
(from FactInventory) to Values. Set a label filter on the rows to adventure
works laptop12, using Label Filters, Begins With (and I thought Adventure
Works only made bikes!)—the result is shown in Figure 12-14.
Figure 12-13 Correct total for inventory?