Search code examples
ssasmdxolap

Minimal value in last three months in mdx query


I am trying to write MDX query that selects minimal sale price of product over the last three months, but I can't get it working.

This is my prototype code which isn't working at all:

WITH 
     SET [CurrentMonth] AS StrToMember('[Date].[Calendar].[Month].&[' + FORMAT(Now(), "yyyyMM") + ']')
     SET [LastThreeMonths] AS TAIL({NULL: [CurrentMonth].ITEM(0)}, 3)
SELECT 
    {
        min(Tail([LastThreeMonths],3), iif([Measures].[Net sale price] = 0, null, [Measures].[Net sale price]))
    } ON COLUMNS
    ,{
         [Product].[Product code].MEMBERS
    } ON ROWS 
FROM  [Sales]
WHERE ( 
         { [Department].[Department name].&[WRO] }
      );

It returns following error: function "" expects a tuple set expression for the 1 argument

If I try something like that:

WITH 
     SET [CurrentMonth] AS StrToMember('[Date].[Calendar].[Month].&[' + FORMAT(Now(), "yyyyMM") + ']')
     SET [LastThreeMonths] AS TAIL({NULL: [CurrentMonth].ITEM(0)}, 3)
SELECT 
    {
        (Tail([LastThreeMonths],3), [Measures].[Net sale price])
    } ON COLUMNS
    ,{
         [Product].[Product Code].MEMBERS
    } ON ROWS 
FROM  [Sales]
WHERE ( 
         { [Department].[Department name].&[WRO] }
      );

It works but this isn't what I've wanted - it shows Net sale price in each of last three months. I'm newbie in MDX so please forgive me stupid questions.


Solution

  • I gave some time for myself to think about that and I solved it. Here's the solution:

    WITH 
         SET [Current month] AS StrToMember('[Date].[Calendar].[Month].&[' + FORMAT(Now(), "yyyyMM") + ']')
         SET [Last three months] AS LastPeriods(3, [Current month].item(0)) 
         MEMBER [Min price] AS MIN([Last three months], [Measures].[Net sale price])
    SELECT 
        {
            [Min price]
        } ON COLUMNS
        ,{
             [Product].[Product name].MEMBERS
        } ON ROWS 
    FROM  [Sales]
    WHERE ( 
             { [Department].[Department name].&[WRO] }
          );