在表格多维数据集模型的 MDX 查询中使用计算成员中的日期范围

Using a date range in calculated member in MDX query for Tabular cube model

我有一个要求,我必须获得不同度量的值,每个度量都有自己的日期范围,以下查询有效并为我获取了一年的数据,

WITH  MEMBER [Measures].[Ex1] AS ([Measures].[Average Spending], [Date].[Year].&[2020])

MEMBER [Measures].[Ex2] AS ([Measures].[Average Time Spent], [Date].[Year].&[2019])

SELECT {[Customer].[Name].[Name]} ON 1, {[Measures].[Ex1],[Measures].[Ex2]} ON 0 FROM [Model];

但是当我尝试传递日期范围而不是年份时,出现错误,

WITH  MEMBER [Measures].[Ex1] AS ([Measures].[Average Spending], [Date].[Year].&[2020])

MEMBER [Measures].[Ex2] AS ([Measures].[Average Time Spent], [Date].[Date].[01-May-2020]:[Date].[Date].[31-May-2020])

SELECT {[Customer].[Name].[Name]} ON 1, {[Measures].[Ex1],[Measures].[Ex2]} ON 0 FROM [Model];

我收到以下错误,

Executing the query ... The function expects a string or numeric expression for the argument. A tuple set expression was used. Run complete

如何获取每个计算所得成员的日期范围度量值?

如果您不使用元组(即层次结构的单个成员的组合,如度量或年份),而是使用集合,则您需要告诉 MDX 如何处理集合元素以获得单个价值。我假设以下内容会满足您的需求:

WITH
MEMBER [Measures].[Ex2] AS 
       Aggregate([Date].[Date].[01-May-2020]:[Date].[Date].[31-May-2020],
                 [Measures].[Average Time Spent]
                )
...

而不是Aggregate,你也可以使用SumAvgVariance等。但通常情况下,我更喜欢使用Aggregate,因为这个使用为多维数据集 i 中的度量定义的任何聚合方法。 e.更通用,查询不需要适配cube计算脚本的变化

另见 Aggregate in the SSAS documentation