Search code examples
ssasmdxcube

MDX last non empty over multiple dimensions


I would geatly appreciate if someone could help me with the problem. I have the following fact table:

+---------+--------+-----------+----------+------------+---------------+-------------+----------------+
| EntryNo | ItemNo | CompanyId | BranchId | LocationId | ValuationDate | ValuatedQty | ValuatedAmount |
+=========+========+===========+==========+============+===============+=============+================+
| 1       | Item1  | 1         | 1        | 1          | 2016-03-01    | 0           | 0              |
+---------+--------+-----------+----------+------------+---------------+-------------+----------------+
| 2       | Item1  | 1         | 2        | 1          | 2016-03-01    | 4           | 400            |
+---------+--------+-----------+----------+------------+---------------+-------------+----------------+
| 3       | Item1  | 1         | 1        | 1          | 2016-03-02    | 10          | 1000           |
+---------+--------+-----------+----------+------------+---------------+-------------+----------------+
| 4       | Item2  | 1         | 1        | 2          | 2016-03-02    | 4           | 200            |
+---------+--------+-----------+----------+------------+---------------+-------------+----------------+
| 5       | Item2  | 2         | 2        | 2          | 2016-03-02    | 6           | 300            |
+---------+--------+-----------+----------+------------+---------------+-------------+----------------+
| 6       | Item1  | 2         | 2        | 1          | 2016-03-03    | 0           | 0              |
+---------+--------+-----------+----------+------------+---------------+-------------+----------------+
| 7       | Item3  | 1         | 2        | 3          | 2016-03-03    | 0           | 0              |
+---------+--------+-----------+----------+------------+---------------+-------------+----------------+
| 8       | Item1  | 2         | 2        | 3          | 2016-03-03    | 9           | 450            |
+---------+--------+-----------+----------+------------+---------------+-------------+----------------+

There are two measures that represent "overstocked" items on a particular day. Is it possible to create a calculated member that will allow for slicing data on the all linked dimensions (Items, Companies, etc.) ? I guess the LastNonEmpty agregration would be useful here except it is not available in the standard edition.

Given the example the results should be as follows:

By Company:

+---------+-------------+----------------+
| Company | ValuatedQty | ValuatedAmount |
+=========+=============+================+
| 1       | 14          | 1200           |
+---------+-------------+----------------+
| 2       | 15          | 750            |
+---------+-------------+----------------+

By Date:

+------------+-------------+----------------+
| Date       | ValuatedQty | ValuatedAmount |
+============+=============+================+
| 2016-03-01 | 4           | 400            |
+------------+-------------+----------------+
| 2016-03-02 | 16          | 1300           |
+------------+-------------+----------------+
| 2016-03-03 | 9           | 450            |
+------------+-------------+----------------+

By Item:

+-------+-------------+----------------+
| Item  | ValuatedQty | ValuatedAmount |
+=======+=============+================+
| Item1 | 9           | 450            |
+-------+-------------+----------------+
| Item2 | 6           | 300            |
+-------+-------------+----------------+
| Item3 | 0           | 0              |
+-------+-------------+----------------+

Solution

  • Two functions that come to mind for your requirements are:

    Tail: https://msdn.microsoft.com/en-us/library/ms146056.aspx

    Bottomcount: https://msdn.microsoft.com/en-us/library/ms144864.aspx

    So with Tail something like the following is possible:

    WITH SET [LastYearPerSubCat] AS
    GENERATE(
      [Product].[Product Categories].[SubCategory].members AS S,
      S.CURRENTMEMBER
      *
      TAIL(
        NONEMPTY(
           [Date].[Calendar Year].[Calendar Year].MEMBERS,
           S.CURRENTMEMBER
        )
      )
    )
    SELECT  
       [Measures].[Reseller Gross Profit]  ON 0  
       ,[LastYearPerSubCat] ON 1
    FROM [Adventure Works];