错误在MDX中使用案例时的总计

时间:2016-05-23 09:58:49

标签: mdx

如果我使用下面的负总数而不是前一个,我得到相同的输出,但仍然不是我预期的输出。有人帮帮我吗?

MEMBER [Measures].[Negative Total] AS
CASE
    WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) < 0
    THEN [Measures].[Total]*-1
    WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) > 0
    THEN 0
END

让我解释一下我想要实现的目标。我有[Dim Account]的维度,其具有[帐户类型],[帐户活动],[帐户营销]等属性。[帐户类型]是收入,费用,.. [帐户活动]就像广告,折扣,赞助商的销售等[客户营销]是这些东西的组合,我的意思是广告是费用,而折扣则存储为负收入,因此最终折扣将被视为营销费用这就是为什么我的会员[负面]总计]是将收入乘以-1并且我想排除那些收入是积极的,因为它们是收入。我想只显示那些转换后的收入,最后将它们加起来。在我的MDX中,数据显示完美,但加起来原始值-43380.4 +18 - 9181.32 = -52542.81,应该是43380.4 +0 + 9181.32 = 52561.72。为了比较原始总数和预期总数,我同时显示[测量]。[总计]和[测量]。[负总计]。如果你知道诀窍enter image description here

,请帮帮我
WITH
MEMBER [Measures].[Negative Total] AS
CASE
    WHEN
        [Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
        [Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
        [Measures].[Total]<1
    THEN [Measures].[Total]*-1

    WHEN 
        [Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
        [Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
        [Measures].[Total]>1 
    THEN 0
END

SELECT
 {
 [Measures].[Total],[Measures].[Negative Total]} on COLUMNS
 , 
NON EMPTY 
 (
    {
        (
        [Dim Account].[HierarchyMarketing].[Account Marketing].&[Income]&[Marketing] 
        *
        {
        [Dim Account].[Account Activity].[Account Activity].MEMBERS,
        VISUALTOTALS([Dim Account].[Account Activity].[All])
        }
        )
     }
    ,
    [Dim Branch].[Trading As].&[BAR]&[Barlens Event Hire] ,
    [Dim Scenario].[Scenario Name].&[Actual]  ,
    [Dim Fiscal Year].[HierarchyFiscal].[E Month].&[2016]&[December]
 )
 on ROWS
FROM [CubeProfitLoss]

3 个答案:

答案 0 :(得分:2)

它正在按预期工作,因为[All]也是成员。

要实现您想要的效果,请添加获得负总数的基本度量,然后在最终计算中使用它。

MEMBER [Measures].[Negative Total Test] AS
CASE
    WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) < 0
    THEN [Measures].[Total]*-1
    WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) > 0
    THEN 0
END

MEMBER [Measures].[Negative Total] AS
CASE
    WHEN [Dim Account].[Account Activity].CURRENTMEMBER IS  [Dim Account].[Account Activity].[All]
    SUM(
        [Dim Account].[Account Activity].[Account Activity].MEMBERS,
        [Measures].[Negative Total Test]
       )
   ELSE [Measures].[Negative Total Test]
END

答案 1 :(得分:1)

(只是为了增加Sourav的优秀解决方案)

| Platform | PyVer | __dict__ | _asdict | | -------------------------- | --------- | -------- | ------- | | Ubuntu 14.04 LTS | Python2.7 | yes | yes | | Ubuntu 14.04 LTS | Python3.4 | yes | yes | | CentOS Linux release 7.4.1708 | Python2.7 | no | yes | | CentOS Linux release 7.4.1708 | Python3.4 | no | yes | | CentOS Linux release 7.4.1708 | Python3.6 | no | yes | CASE都很慢 - IIF在某些情况下会更快,如果其中一个分支可以是IIF,通常会更快选择:

NULL

根据屏幕截图,没有必要在第一个计算中包含营销,因此可以进一步简化:

MEMBER [Measures].[Negative Total Test] AS
  IIF(
    (
       [Measures].[Total]
      ,[Dim Account].[Account Marketing].&[Income]&[Marketing]
    ) >= 0
      ,NULL
      ,[Measures].[Total] * -1
  )
MEMBER [Measures].[Negative Total] AS
  IIF( 
    [Dim Account].[Account Activity].CURRENTMEMBER 
       IS [Dim Account].[Account Activity].[All]
    ,SUM(
        [Dim Account].[Account Activity].[Account Activity].MEMBERS,
        [Measures].[Negative Total Test]
       )
   ,[Measures].[Negative Total Test]
  )

答案 2 :(得分:0)

哇!哇!我得到了答案,这就是我要找的东西。它只是改变是[负面总测试]的显示。我再次感谢Saurav,因为他给了我生命的MDX代码 enter image description here

WITH
MEMBER [Measures].[Negative Total Test] AS
CASE
    WHEN
        [Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
        [Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
        [Measures].[Total]<1
    THEN [Measures].[Total]*-1

    WHEN 
        [Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
        [Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
        [Measures].[Total]>1 
    THEN NULL
	ELSE 
		[Measures].[Total]
END

MEMBER [Measures].[Negative Total] AS
CASE
	WHEN  [Dim Account].[Account Activity].CURRENTMEMBER IS  [Dim Account].[Account Activity].[All]
	THEN 
	SUM(
        [Dim Account].[Account Activity].[Account Activity].MEMBERS,
        [Measures].[Negative Total Test]
       )
   ELSE [Measures].[Negative Total Test]
END

MEMBER [Dim Account].[HierarchyMarketing].[Income] AS
	([Dim Account].[HierarchyMarketing].[Account Type].&[Income], [Measures].[Negative Total])

MEMBER [Dim Account].[HierarchyMarketing].[Total Marketing Expense] AS
 AGGREGATE
 (
 {
	[Dim Account].[Account Marketing].&[Income]&[Marketing]
	,
	[Dim Account].[Account Marketing].&[Expenses]&[Marketing] 
 }
 ,
 [Measures].[Negative Total]
 )

SELECT
 {
	//[Measures].[Total], [Measures].[Negative Total], 
	[Measures].[Negative Total Test]
 } on COLUMNS
 , 
 NON EMPTY 
 (
    {
		(
		[Dim Account].[HierarchyMarketing].[Account Type].&[Income]
		* 
		{
		[Dim Account].[Account Activity].[All]
		}	
		)
		,
        (
        {
          [Dim Account].[HierarchyMarketing].[Account Marketing].&[Expenses]&[Marketing]
            ,
			[Dim Account].[HierarchyMarketing].[Account Marketing].&[Income]&[Marketing] 
        }
        *
        [Dim Account].[Account Activity].[Account Activity].MEMBERS 
        )
        ,
        (
        [Dim Account].[HierarchyMarketing].[Total Marketing Expense]	
	    ,
		[Dim Account].[Account Activity].[All]  
        )
     }	
	,
//	Traders,
	[Dim Branch].[Trading As].&[BAR]&[Barlens Event Hire] ,
	[Dim Scenario].[Scenario Name].&[Actual]  ,
	[Dim Fiscal Year].[HierarchyFiscal].[E Month].&[2016]&[December]
 )
 on ROWS
FROM [CubeProfitLoss]
;

If I execute [Measures].[Negative Total Test] commenting MEMBER [Measures].[Negative Total Test] AS -- (1) the output is like this

If I execute [Measures].[Negative Total Test] commenting MEMBER [Measures].[Negative Total Test] AS -- (2) the output is like this

我应该问另一个问题,但是我已经达到了我的问题限制,对不起。 上面的问题是我问题的一部分。最终输出应该像我编辑的图片一样。根据Sourav的解决方案,他检查了[Dim Account]的条件。[账户活动] .CURRENTMEMBER是[Dim Account]。[账户活动]。[全部]完美地解决了这个小问题,但当我想积累原始收入时(650,920.88)然后它出现了没有预期的大数字。我需要的另一个解决方案我想加上营销费用(24010.44)和已经转换的负营销收入(43380.4 + 9181.32),所以最终结果应该是76572.16。请修改我的MDX。非常感谢

WITH

//MEMBER [Measures].[Negative Total Test] AS -- (1)
//CASE
//    WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) < 0
//    THEN [Measures].[Total]*-1
//    WHEN ([Measures].[Total],[Dim Account].[Account Marketing].&[Income]&[Marketing]) > 0
//    THEN NULL
//	ELSE
//	[Measures].[Total]
//END

MEMBER [Measures].[Negative Total Test] AS --(2)
CASE
    WHEN
        [Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
        [Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
        [Measures].[Total]<1
    THEN [Measures].[Total]*-1

    WHEN 
        [Dim Account].[Account Marketing].CurrentMember.MEMBER_CAPTION="Marketing" AND
        [Dim Account].[Account Marketing].Properties("Account Type") ="Income" AND
        [Measures].[Total]>1 
    THEN NULL
	ELSE 
		[Measures].[Total]
END

MEMBER [Measures].[Negative Total] AS
CASE
    WHEN [Dim Account].[Account Activity].CURRENTMEMBER IS  [Dim Account].[Account Activity].[All]
    THEN
	SUM(
        [Dim Account].[Account Activity].[Account Activity].MEMBERS,
        [Measures].[Negative Total Test]
       )

   ELSE [Measures].[Negative Total Test]
END
//MEMBER [Dim Account].[Account Activity].[TOTAL] AS
//	[Dim Account].[Account Activity].[All]

MEMBER [Dim Account].[HierarchyMarketing].[Total Marketing Expense] AS
 AGGREGATE
 (
 {
	[Dim Account].[Account Marketing].&[Income]&[Marketing]
	,
	[Dim Account].[Account Marketing].&[Expenses]&[Marketing] 
 }
 ,
 [Measures].[Negative Total Test]
 )


SELECT
 {
	[Measures].[Total], [Measures].[Negative Total]
 } on COLUMNS
 , 
 NON EMPTY 
 (
    {
		(
		[Dim Account].[HierarchyMarketing].[Account Type].&[Income]
		* 
		{
		VisualTotals([Dim Account].[Account Activity].[All])
		}	
		)
		,

        (
        {
            [Dim Account].[HierarchyMarketing].[Account Marketing].&[Expenses]&[Marketing]
            ,
			[Dim Account].[HierarchyMarketing].[Account Marketing].&[Income]&[Marketing] 
        }
        *
        [Dim Account].[Account Activity].[Account Activity].MEMBERS 
        )
        ,
        (
        [Dim Account].[HierarchyMarketing].[Total Marketing Expense]	
	    ,
		VISUALTOTALS([Dim Account].[Account Activity].[All])  
        )
     }	
	,
	[Dim Branch].[Trading As].&[BAR]&[Barlens Event Hire] ,
	[Dim Scenario].[Scenario Name].&[Actual]  ,
	[Dim Fiscal Year].[HierarchyFiscal].[E Month].&[2016]&[December]
 )
 on ROWS
FROM [CubeProfitLoss]