【问题标题】:MDX Calculated member in WHERE clause causes different resultsWHERE 子句中的 MDX 计算成员导致不同的结果
【发布时间】:2015-04-08 17:21:34
【问题描述】:

以下查询具有不同的结果集,具体取决于我在 WHERE 子句中使用的 [Measures]。 激活 [Measures].[MetricFormula] 而不是 [Measures].[Actual],查询会提供不同的(和预期的)结果。

WITH
MEMBER [Measures].[MetricFormula] AS Measures.[Downloads]
MEMBER [Measures].[Actual] AS [Measures].[MetricFormula], LANGUAGE = 1031,FORMAT_STRING='#,##0'

SET [mySet] as 
topcount(
    nonempty(
        filter(
            distinct descendants({[AppPackages].[AppPackages].&[F_TAAM00000069]}), 
            [Measures].[MetricFormula] <> 0 AND ([AppPackages].[AppPackages].PROPERTIES('AppTypeID') = 'DETAIL')
            ),
        [Time].[Calendar].[Date].&[2015-03-22T00:00:00]:[Time].[Calendar].[Date].&[2015-03-31T00:00:00]
        )
    ,6
    ,aggregate([Time].[Calendar].[Date].&[2015-03-22T00:00:00]:[Time].[Calendar].[Date].&[2015-03-31T00:00:00], [Measures].[MetricFormula]))


select
([Time].[Calendar].[Date].&[2015-03-22T00:00:00]:[Time].[Calendar].[Date].&[2015-03-31T00:00:00]) ON COLUMNS,
([mySet]) ON ROWS   

FROM [Facts] 
WHERE (
--([Measures].[MetricFormula]),
([Measures].[Actual]),
{[DownloadTypes].[DownloadTypes].&[New]}
)

([Measures].[Downloads] 是多维数据集中定义的真正度量。)

问题:为什么它会产生不同的结果? 我使用这个结构(以各种形式)来简化基于代码的查询生成。 在开始重新设计查询生成器之前,我想了解这些差异。

背景: 该查询应该根据给定的度量计算给定时间范围内给定 AppPackage 的前 6 个后代。我首先找到时间范围的(非空)后代,在其上添加顶部计数,确保时间聚合度量值驱动顶部计算。最后,我将评估集放在行上,将时间放在列上,指定在哪里看什么。除了所描述的效果之外,它的效果很好。

【问题讨论】:

  • 有趣的问题 - 我将针对 AdvWks 进行测试。如果你简化这个集合,它的行为是否仍然相同?
  • 好点。我删除了 topcount 并保留了其他所有内容 (nonempty(filter(..)) => 集合没有改变。嗯......要考虑的事情......
  • Measures.[Downloads] 是直接来自 RDBMS 吗?如果是,该列的数据类型是什么?如果没有,您如何在多维数据集中创建此度量?
  • @Sourav_Agasti:是的,它来自 RDBMS,Cube 中的类型是货币。
  • @whytheq:我刚刚用一个简单的(r)topcount 进行了测试:问题仍然存在 => 我会看看 topcount 在做什么。

标签: ssas mdx


【解决方案1】:

我在AdvWrks 中复制很有趣,但这里是:

WITH 
  MEMBER [Measures].[x] AS 
    [Measures].[Internet Order Count] 
  MEMBER [Measures].[y] AS 
    [Measures].[x] 
   ,LANGUAGE = 1031 
   ,FORMAT_STRING = '#,##0' 
  SET [s] AS 
    TopCount
    (
      NonEmpty
      (
        Filter
        (
          [State-Province].[State-Province].MEMBERS
         ,
          [Measures].[x] <> 0
        )
       ,
          [Date].[Calendar].[Month].&[2005]&[10]
        : 
          [Date].[Calendar].[Month].&[2006]&[5]
      )
     ,10
     ,Aggregate
      (
          [Date].[Calendar].[Month].&[2005]&[10]
        : 
          [Date].[Calendar].[Month].&[2006]&[5]
       ,[Measures].[x]
      )
    ) 
SELECT 
  {
      [Date].[Calendar].[Month].&[2005]&[10]
    : 
      [Date].[Calendar].[Month].&[2006]&[5]
  } ON COLUMNS
 ,Order
  (
    [s]
   ,[State-Province].CurrentMember.Member_Caption
   ,basc
  ) ON ROWS
FROM [Adventure Works]
WHERE 
  (
    //([Measures].[x]),
    ([Measures].[y]),
   {[Product].[Product Categories].[Category].&[1]}
  );

它的作用与您的脚本相同 - 如果我们更改为 [x],那么南澳大利亚将从结果集中消失。


编辑
至于为什么 - 我现在知道了!

MDX 对你很好,即使脚本中有错误也不会出错:

Aggregate
      (
          [Date].[Calendar].[Month].&[2005]&[10]
        : 
          [Date].[Calendar].[Month].&[2006]&[5]
       ,[Measures].[x]  //<<this is not allowed so `mdx` is ignoring it!
      )

因此请尝试以下操作或尝试将Aggregate 交换为Sum

WITH 
  MEMBER [Measures].[MetricFormula] AS 
    Measures.[Downloads] 
  MEMBER [Measures].[Actual] AS 
    [Measures].[MetricFormula] 
   ,LANGUAGE = 1031 
   ,FORMAT_STRING = '#,##0' 
  SET [mySet] AS 
    TopCount
    (
      NonEmpty
      (
        Filter
        (
          (DISTINCT 
            Descendants({[AppPackages].[AppPackages].&[F_TAAM00000069]}))
         ,
            [Measures].[MetricFormula] <> 0
          AND 
            [AppPackages].[AppPackages].Properties('AppTypeID') = 'DETAIL'
        )
       ,
          [Time].[Calendar].[Date].&[2015-03-22T00:00:00]
        : 
          [Time].[Calendar].[Date].&[2015-03-31T00:00:00]
      )
     ,6
     ,Aggregate
      (
          [Time].[Calendar].[Date].&[2015-03-22T00:00:00]
        : 
          [Time].[Calendar].[Date].&[2015-03-31T00:00:00]
       ,Measures.[Downloads]   //<<<<<<<<<<<<moved away from using a calculated measure as they are not working as expected inside the function `AGGREGATE`
      )
    ) 
SELECT 
    [Time].[Calendar].[Date].&[2015-03-22T00:00:00]
  : 
    [Time].[Calendar].[Date].&[2015-03-31T00:00:00] ON COLUMNS
 ,[mySet] ON ROWS
FROM [Facts]
WHERE 
  (
    --([Measures].[MetricFormula]),
    [Measures].[Actual]
   ,{[DownloadTypes].[DownloadTypes].&[New]}
  );

为什么?

好吧,在 Aggregate 函数中使用自定义度量 [Measures].[MetricFormula] 似乎是非法的,但是当您将其作为 TopCount 中第二个参数的一部分执行此操作时,mdx 只会忽略错误并使用多维数据集中的默认度量.... 很好的引擎忽略了那个错误?!


将计算的成员输入聚合函数时抛出的错误示例:

WITH 
  MEMBER [Measures].[j] AS 
    [Measures].[Internet Order Count] 
  MEMBER [Measures].[x] AS 
    Aggregate
    (
        [Date].[Calendar].[Month].&[2005]&[10]
      : 
        [Date].[Calendar].[Month].&[2006]&[5]
     ,[Measures].[j]   //<<FEEDING "ILLEGAL" CALCULATED MEMBER INTO AGGREGATE FUNCTION
    ) 
SELECT 
  {[Measures].[x]} ON COLUMNS
 ,[State-Province].[State-Province].MEMBERS ON ROWS
FROM [Adventure Works];

【讨论】:

  • 为什么这是非法的,亲爱的朋友?
  • @Sourav_Agasti ??自己试试吧……我现在为你添加一个脚本——你需要问微软为什么。
  • 哦,是的。聚合函数中不允许计算成员。抱歉,我没有阅读您答案的最后一部分。我会说你进行了很好的调查。
  • @Sourav_Agasti 危险的一点是,如果您使用 Aggregate(, computed_Measure) 作为 TopCount 的第三个参数,那么它不会抛出错误,而只是忽略了 computed_Measure ... 看起来像对我来说是一个错误
猜你喜欢
  • 2013-10-25
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多