如何设置多个并行期?

How to get multiple parallel periods set?

我想创建一个 MDX 查询,其中 returns 两个日期之间每个月的第 15 天的度量值。

例如,2010-01-01 和 2016-12-15 的结果应如下所示:

2016-12-15: 123
2016-11-15: 789
2016-10-15: 556
(...) 
2010-01-15: 456

我知道我可以使用 DateDiff() 函数计算两个日期之间的月数。另外,我可以使用 ParallelPeriod() 函数来获取上个月的值。

但是我不知道如何将这些值一起使用,"iterate" 从 1 到 DateDiff() 结果在 "Days" 集合中创建多个 ParallelPeriod() 调用。

WITH 
MEMBER NumberOfMonths AS
DateDiff("m", 
    [Calendar].[Day].&[20100101].MemberValue,
    [Calendar].[Day].&[20160315].MemberValue
) 

SET Days AS {
     PARALLELPERIOD([Calendar].[Month], 1, [Calendar].[Day].&[20160315]),
     PARALLELPERIOD([Calendar].[Month], 2, [Calendar].[Day].&[20160315]),
     PARALLELPERIOD([Calendar].[Month], 3, [Calendar].[Day].&[20160315])
     -- (...) How to generate this set automatically, using NumberOfMonths?
}

SELECT 
    { Days } ON 0,
    { Quantity } ON 1
FROM [MyCube]

如有任何帮助,我们将不胜感激。

MDX 方法 №1(具有总和聚合的计算成员):

WITH 
MEMBER [Measures].[Quantity15] AS
SUM(
    [Calendar].[Day].[Day].Members,
    IIF( 
        Right([Calendar].[Day].CurrentMember.Properties('Key'),2) = "15",
        [Measures].[Quantity],
        NULL
    )
)    

SELECT 
    NON EMPTY { [Calendar].[Day].&[20100101]:[Calendar].[Day].&[20160315] } ON 0,
    { [Measures].[Quantity15] } ON 1
FROM [MyCube]

MDX 方法 №2(没有新成员,过滤集合):

SELECT 
    NONEMPTY(
        {[Calendar].[Day].&[20100101]:[Calendar].[Day].&[20160315]},
        IIF(
            Right([Calendar].[Day].CurrentMember.Properties('Key'),2) = "15",
            1,
            NULL
        )
    ) ON 0,
    { [Measures].[Quantity] } ON 1
FROM [MyCube]

这是一个有趣的问题,虽然有解决方案,但阅读 MDX 参考资料让我走入了死胡同。

以下是获取每月十五日一组的方法:

WITH SET Months
AS
    [Calendar].[Month].Members

SET FifteenthDays AS
GENERATE(
    Months,
    StrToSet('HEAD(DESCENDANTS(Months.Current,[Calendar].[Day]),1).Item(0).Lead(14)')
    )

SELECT {} ON 0,
FifteenthDays ON 1
FROM TheCube

您可以通过使用日期参数过滤初始命名集 "Months" 来调整它以满足您的要求。

这是正在发生的事情:

  1. GENERATE/StrToSet 组合将引号内的 MDX 应用于第一组的每个成员 ("Months")
  2. Current 函数类似于 CurrentMember,但应用于 Generate() 括号内的集合。
  3. DESCENDANTS 函数在日级别获取所有月份的 "children"(我不得不使用它而不是 .Children,因为在我的多维数据集中有额外的一代 - 周 - 在月和日之间)
  4. HEAD 函数获取月份的第一天。 (如果您的时间维度叶级别未按日期顺序排序,您可能需要将集合包装在 ORDER 函数中)。
  5. MDX 不会自动确定单例集(HEAD({},1) 总是 return 单例或空集)是一个成员,您可以在上应用成员函数它。所以在应用 Lead 之前,我必须使用 Item() 函数。我不知道为什么这个功能会起作用,因为根据文档,它有不同的用途。
  6. Lead(14) 给出该月的第 15 天,因为参数是基于 0 的。