简体   繁体   English

如何根据维度属性在MDX中定义计算度量?

[英]How do I define a Calculated Measure in MDX based on a Dimension Attribute?

I would like to create a calculated measure that sums up only a specific subset of records in my fact table based on a dimension attribute. 我想创建一个计算度量,它根据维度属性仅汇总事实表中的特定记录子集。

Given: 鉴于:

Dimension 尺寸

  • Date 日期
  • LedgerLineItem {Charge, Payment, Write-Off, Copay, Credit} LedgerLineItem {收费,付款,注销,复印,信用}

Measures 措施

  • LedgerAmount LedgerAmount

Relationships 关系
* LedgerLineItem is a degenerate dimension of FactLedger * LedgerLineItem是FactLedger的简并维度

If I break down LedgerAmount by LedgerLineItem.Type I can easily see how much is charged, paid, credit, etc, but when I do not break it down by LedgerLineItem.Type I cannot easily add the credit, paid, credit, etc into a pivot table. 如果我通过LedgerLineItem.Type分解LedgerAmount我可以很容易地看到收费,支付,信用等多少,但是当我没有通过LedgerLineItem.Type分解时,我不能轻易地将信用,付费,信用等添加到数据透视表。 I would like to create separate calculated measures that sum only specific type (or multiple types) of ledger facts. 我想创建单独的计算度量,仅对特定类型(或多种类型)的分类帐事实求和。

An example of the desired output would be: 所需输出的一个例子是:

| Year  | Charged | Total Paid | Amount - Ledger |
| 2008  | $1000   | $600       | -$400           |
| 2009  | $2000   | $1500      | -$500           |
| Total | $3000   | $2100      | -$900           |

I have tried to create the calculated measure a couple of ways and each one works in some circumstances but not in others. 我曾试图通过几种方式创建计算出的度量,每种方法都可以在某些情况下运行,但在其他情况下则不然。 Now before anyone says do this in ETL, I have already done it in ETL and it works just fine. 现在在有人说在ETL中这样做之前,我已经在ETL中完成了它并且它工作得很好。 What I am trying to do as part of learning to understand MDX better is to figure out how to duplicate what I have done in the ETL in MDX as so far I am unable to do that. 作为学习更好地理解MDX的一部分,我想要做的是弄清楚如何复制我在MDX中的ETL中所做的事情到目前为止我无法做到这一点。

Here are two attempts I have made and the problems with them. 这是我做过的两次尝试以及它们的问题。 This works only when ledger type is in the pivot table. 仅当分类帐类型位于数据透视表中时,此方法才有效。 It returns the correct amount of the ledger entries (although in this case it is identical to [amount - ledger] but when I try to remove type and just get the sum of all ledger entries it returns unknown. 它返回正确数量的分类帐条目(虽然在这种情况下它与[金额 - 分类帐]相同但是当我尝试删除类型并且只获得所有分类帐条目的总和时它返回未知。

CREATE MEMBER CURRENTCUBE.[Measures].[Received Payment]
AS CASE WHEN ([Ledger].[Type].currentMember = [Ledger].[Type].&[Credit]) 
OR ([Ledger].[Type].currentMember = [Ledger].[Type].&[Paid])
OR ([Ledger].[Type].currentMember = [Ledger].[Type].&[Held Money: Copay])
THEN [Measures].[Amount - ledger] 
ELSE 0
END 
, FORMAT_STRING = "Currency"
, VISIBLE = 1 
, ASSOCIATED_MEASURE_GROUP = 'Ledger'  ; 

This works only when ledger type is not in the pivot table. 仅当分类帐类型不在数据透视表中时,此方法才有效。 It always returns the total payment amount, which is incorrect when I am slicing by type as I would only expect to see the credit portion under credit, the paid portion, under paid, $0 under charge, etc. 它总是返回总付款金额,这在我按类型切片时是不正确的,因为我只希望看到信用额度,信用部分,付费部分,付费,收费0美元等。

CREATE MEMBER CURRENTCUBE.[Measures].[Received Payment]
AS sum({([Ledger].[Type].&[Credit]), ([Ledger].[Type].&[Paid])
, ([Ledger].[Type].&[Held Money: Copay])}
,  [Measures].[Amount - Ledger])
, FORMAT_STRING = "Currency"
, VISIBLE = 1 
, ASSOCIATED_MEASURE_GROUP = 'Ledger'  ;  

Is there any way to make this return the correct numbers regardless of whether Ledger.Type is included in my pivot table or not? 有没有办法让这个返回正确的数字,无论Ledger.Type是否包含在我的数据透视表中?

Try EXISTING: 尝试现有:

CREATE MEMBER CURRENTCUBE.[Measures].[Received Payment]
AS sum(Existing({([Ledger].[Type].&[Credit]), ([Ledger].[Type].&[Paid])
, ([Ledger].[Type].&[Held Money: Copay])})
,  [Measures].[Amount - Ledger])
, FORMAT_STRING = "Currency"
, VISIBLE = 1 
, ASSOCIATED_MEASURE_GROUP = 'Ledger'  ;  

Should make it pay attention to the members in play. 应该让它关注游戏中的成员。

Can't comment on Meff's answer, so I'll post my own. 无法对Meff的答案发表评论,所以我会发表自己的答案。

You should consider using Aggregate instead of Sum, as results may not always be the ones you expect using Sum: 您应该考虑使用Aggregate而不是Sum,因为结果可能并不总是您期望使用Sum的结果:

CREATE MEMBER CURRENTCUBE.[Measures].[Received Payment]
AS Aggregate(Existing({([Ledger].[Type].&[Credit]), ([Ledger].[Type].&[Paid])
, ([Ledger].[Type].&[Held Money: Copay])})
,  [Measures].[Amount - Ledger])
, FORMAT_STRING = "Currency"
, VISIBLE = 1 
, ASSOCIATED_MEASURE_GROUP = 'Ledger'  ; 

I am assuming the Ledger dimention has a key within the FactLedger but the issue that it doesn't rollup right with the first MDX calculated member leads me to believe that you may want to rethink the hierachies for this. 我假设Ledger维度在FactLedger中有一个密钥但是它没有与第一个MDX计算成员汇总的问题让我相信你可能想重新考虑这个层次结构。

Then a simple SUM based on your ledger type would work, does that make sense? 然后,基于您的分类帐类型的简单SUM可以工作,这有意义吗?

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

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