简体   繁体   English

MDX计算得出的度量,得出不同值的总和

[英]MDX Calculated measure which makes sum of distinct values

I have weird problem which I'm trying to resolve. 我有一个奇怪的问题要解决。 My fact table looks like follow: 我的事实表如下所示:

CaseID | ClientID | PaymentDate|ToPay   | Paid
1      |     1    | 2015-01-01 |1000    |100
1      |     1    | 2015-02-01 |1000    |200
1      |     1    | 2015-03-01 |1000    |300
2      |     1    | 2015-01-01 |2000    |100
2      |     1    | 2015-02-01 |2000    |400
2      |     1    | 2015-03-01 |2000    |150

What I'm trying to do is create 2 measures: 我想做的是创建2个措施:
Sum(ToPay)
Sum(Paid)

What is the problem? 问题是什么?

In the result set I should get values like this: Client: 1 To Paid: 3000, Paid: 1250 在结果集中,我应该获得如下所示的值:客户:1支付:3000,支付:1250

Which means that sum(ToPay) will be calculated as distinct values for specific client and case. 这意味着sum(ToPay)将被计算为特定客户和案例的不同值。

It is possible to create such query in MDX ? 是否可以在MDX创建这样的查询? If yes then how? 如果是,那怎么办?

Ok - I tried to model your situation with the MS cube AdvWrks 好的-我尝试使用MS多维数据集AdvWrks对您的情况进行AdvWrks

Here is the situation where all are summed: 这是所有情况相加的情况:

WITH 
  MEMBER [Date].[Calendar].[All].[x] AS 100 
  MEMBER [Date].[Calendar].[All].[y] AS 200 
  MEMBER [Date].[Calendar].[All].[z] AS 200 
  SET [blah] AS 
    {
      [x]
     ,[y]
     ,[z]
    } 
  MEMBER [Measures].[xx] AS 
    Sum
    (
      [blah]
     ,[Measures].[Internet Sales Amount]
    ) 
SELECT 
  {[Measures].[xx]} ON 0
FROM [Adventure Works];

Result is 500. 结果是500。

To pull out the sum of just distinct values I've used Filter , it depends on the set being in order so also needed to use the Order function: 为了提取我使用过Filter不同值的总和,这取决于设置的顺序,因此还需要使用Order函数:

WITH 
  MEMBER [Date].[Calendar].[All].[a] AS 100 
  MEMBER [Date].[Calendar].[All].[x] AS 50 
  MEMBER [Date].[Calendar].[All].[y] AS 200 
  MEMBER [Date].[Calendar].[All].[z] AS 200 
  SET [blah] AS 
    {
      [a]
     ,[x]
     ,[y]
     ,[z]
    } 
  MEMBER [Measures].[xx] AS 
    Sum
    (
      Filter
      (
        Order
        (
          [blah]
         ,[Measures].[Internet Sales Amount]
        ) AS x
       ,
          (
            x.CurrentMember
           ,[Measures].[Internet Sales Amount]
          )
        <> 
          (
            x.Item(
            x.CurrentOrdinal)
           ,[Measures].[Internet Sales Amount]
          )
      )
     ,[Measures].[Internet Sales Amount]
    ) 
SELECT 
  {[Measures].[xx]} ON 0
FROM [Adventure Works];

Result is 350. 结果是350。

If you do take Greg Galloway's advice, great for you. 如果您接受Greg Galloway的建议,那对您非常有用。 Otherwise, another good MDX challenge! 否则,另一个很好的MDX挑战!

Here's what you can do. 这是您可以做的。

First sort the set in its natural order. 首先按照自然顺序对集合进行排序。

Next, count the measure [To Pay] only for those cases where either the current member in the set is not the same as the previous member OR if it is the very first member(in which case the current will always not be the same as previous member) 接下来,算上措施[To Pay]只对那些情况下,无论是在设定的当前成员是不一样的前一个成员,或者如果它是第一个成员(在这种情况下,电流将永远不会是相同的前成员)

with set [Client&Case] as
order
     (
        [Fact].[ClientID].[All].children * [Fact].[CaseID].[All].children,
        [Fact].[ClientID].currentmember.name + "" + [Fact].[CaseID].currentmember.name
     )


member measures.[Actual - To Pay]  as 
sum
    (
     [Client&Case],
      iif
      (    
        not([Client&Case].item([Client&Case].currentordinal-1) is [Client&Case].item([Client&Case].currentordinal-2)) 
           --current not same not as previous
        or [Client&Case].currentordinal = 1 --the first member!
       ,[Measures].[To Pay]
       ,0
      )
    )

select 
{measures.[Paid], measures.[Actual - To Pay]} on 0,
[Client&Case] on 1
from [Your Cube]

From Adventure Works: 从冒险作品:

    with set [Category&Country] as
    {
    ([Product].[Category].&[3], [Customer].[Customer Geography].[Country].&[Australia]),
    ([Product].[Category].&[3], [Customer].[Customer Geography].[Country].&[Australia]),
    ([Product].[Category].&[4], [Customer].[Customer Geography].[Country].&[Australia]),
    ([Product].[Category].&[4], [Customer].[Customer Geography].[Country].&[Australia]),
    ([Product].[Category].&[4], [Customer].[Customer Geography].[Country].&[Australia])
    }

    member measures.TotalInternetSales as
    sum([Category&Country],[Measures].[Internet Sales Amount])

    member measures.[ActualInternetSales]  as 
    sum
        (
         [Category&Country],
          iif
          (         
            not([Category&Country].item([Category&Country].currentordinal-1) is [Category&Country].item([Category&Country].currentordinal-2)) 
            or [Category&Country].currentordinal = 1
           ,([Category&Country].currentmember, [Measures].[Internet Sales Amount])
           ,0
          )
        )

    select 
    {measures.TotalInternetSales, measures.[ActualInternetSales]} on 0
    from [Adventure Works]

Hope it works. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM