How to query Date field in MDX with and without aggregate group

不问归期 提交于 2019-12-11 13:07:28

问题


I have a seemingly simple requirement to create a query of price data that will return four sets of price data for different dates:

  1. The lowest date of the range
  2. The highest date of the range
  3. The aggregate sum for the entire range

The closest I have is the following query:

WITH set [Range] as {[Effective Date].[Date].&[2015-12-01T00:00:00] : [Effective Date].[Date].&[2015-12-31T00:00:00]}
select
NON EMPTY{
    filter([Effective Date].[Date].&[2015-12-01T00:00:00], [Measures].[Plan Price]),
    filter([Effective Date].[Date].&[2015-12-31T00:00:00], [Measures].[Plan Price]),
    nonempty([Measures].[Money In], [Range]),
    nonempty([Measures].[Money Out], [Range])
} on 0,
NON EMPTY{
[Region Manager].[Full Name].[Full Name]
} on 1
from [Cube]

However, the date ranges as such return the error:

Two sets specified in the function have different dimensionality.

The "filter" or "nonempty" statements will work individually but I cannot get them to work in a single query. How can I do this? Will it be helpful to use a sub query?


回答1:


Try this:

WITH 
  SET [Range] AS 
    {
      [Effective Date].[Date].&[2015-12-01T00:00:00] 
    : [Effective Date].[Date].&[2015-12-31T00:00:00]
    }
SELECT
NON EMPTY
    {
      ([Effective Date].[Date].&[2015-12-01T00:00:00], [Measures].[Plan Price])
     ,([Effective Date].[Date].&[2015-12-31T00:00:00], [Measures].[Plan Price])
     ,{nonempty([Range]*[Measures].[Money In])}
     ,{nonempty([Range]*[Measures].[Money Out])}
    }
ON 0,
NON EMPTY [Region Manager].[Full Name].[Full Name] ON 1
FROM [Cube];

But NonEmpty is not really required so this is more concise:

WITH 
  SET [Range] AS 
    {
      [Effective Date].[Date].&[2015-12-01T00:00:00] 
    : [Effective Date].[Date].&[2015-12-31T00:00:00]
    }
SELECT
NON EMPTY
    {
      ([Effective Date].[Date].&[2015-12-01T00:00:00], [Measures].[Plan Price])
     ,([Effective Date].[Date].&[2015-12-31T00:00:00], [Measures].[Plan Price])
     ,{[Range]*[Measures].[Money In]}
     ,{[Range]*[Measures].[Money Out]}
    }
ON 0,
NON EMPTY [Region Manager].[Full Name].[Full Name] ON 1
FROM [Cube];

To aggregate you need a new custom measure:

WITH 
  SET [Range] AS 
    {
      [Effective Date].[Date].&[2015-12-01T00:00:00] 
    : [Effective Date].[Date].&[2015-12-31T00:00:00]
    }
  MEMBER [Effective Date].[Date].[All].[AggRange] AS 
    Aggregate([Range])
SELECT
NON EMPTY
    {
      ([Effective Date].[Date].&[2015-12-01T00:00:00], [Measures].[Plan Price])
     ,([Effective Date].[Date].&[2015-12-31T00:00:00], [Measures].[Plan Price])
     ,([Effective Date].[Date].[All].[AggRange],[Measures].[Money In])
     ,([Effective Date].[Date].[All].[AggRange],[Measures].[Money Out])
    }
ON 0,
NON EMPTY [Region Manager].[Full Name].[Full Name] ON 1
FROM [Cube];



回答2:


Unless you're using a dialect of MDX I've never seen, there's no need to use double braces ({{....}}).

Braces mean that the contents are a set. Inside braces, the comma (,) is a UNION operator:

{Tom,Dick,Harry}

gives a set of three people.

Because you're using braces here:

non empty{{
    filter([Effective Date].[Date].&[2015-12-01T00:00:00], [Measures].[Plan Price]),
    filter([Effective Date].[Date].&[2015-12-31T00:00:00], [Measures].[Plan Price]),
    nonempty([Measures].[Money In], [Range]),
    nonempty([Measures].[Money Out], [Range])
}}

the parser is trying to form a set out of two members of the Effective Date dimension, and two members of the Measure dimension. This is impossible, because they have different dimensionality.

It would have been nice if the parser had told you that the

function

it refers to is the UNION (,) function within a set definition. That's the MDX parser for you...

If you want to show the two measures and two dates on the same axis, define the two dates as calculated measures (WITH MEMBER Measures.Bla AS Head([Range],1), for example).



来源:https://stackoverflow.com/questions/37047150/how-to-query-date-field-in-mdx-with-and-without-aggregate-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!