如何在 MDX 中基于维度属性定义计算度量?

发布于 2024-08-28 19:31:54 字数 1847 浏览 5 评论 0原文

我想创建一个计算度量,根据维度属性仅对事实表中的特定记录子集进行求和。

给定:

维度

  • 日期
  • LedgerLineItem {Charge, Payment, Can-Off, Copay, Credit}

度量

  • LedgerAmount

关系
* LedgerLineItem 是 FactLedger 的退化维度

如果我按 LedgerLineItem.Type 细分 LedgerAmount,我可以轻松查看收费、支付、贷项等金额,但是当我不按 LedgerLineItem.Type 细分时,我无法轻松添加贷项、付费、信用等放入数据透视表中。我想创建单独的计算度量,仅对特定类型(或多种类型)的分类账事实求和。

所需输出的一个示例是:

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

我尝试通过多种方式创建计算度量,每种方式在某些情况下有效,但在其他情况下无效。现在,在有人说在 ETL 中执行此操作之前,我已经在 ETL 中完成了它,而且效果很好。作为学习更好地理解 MDX 的一部分,我试图做的就是弄清楚如何在 MDX 中复制我在 ETL 中所做的事情,因为到目前为止我还无法做到这一点。

这是我所做的两次尝试以及其中存在的问题。 仅当分类帐类型位于数据透视表中时,此功能才有效。它返回正确的分类帐条目数量(尽管在这种情况下它与 [金额 - 分类帐] 相同,但是当我尝试删除类型并仅获取所有分类帐条目的总和时,它返回未知。

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'  ; 

仅当分类帐类型为不在数据透视表中。它总是返回总付款金额,当我按类型切片时,这是不正确的,因为我只希望看到贷项下的贷方部分、已付款部分、未付款、收费 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'  ;  

。有什么方法可以使其返回正确的数字,无论 Ledger.Type 是否包含在我的数据透视表中?

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}

Measures

  • LedgerAmount

Relationships
* LedgerLineItem is a degenerate dimension of 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. 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. 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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

篱下浅笙歌 2024-09-04 19:31:54

尝试现有的:

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'  ;  

应该让它关注游戏中的成员。

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.

墨洒年华 2024-09-04 19:31:54

无法评论 Meff 的答案,所以我将发布我自己的答案。

您应该考虑使用 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'  ; 

Can't comment on Meff's answer, so I'll post my own.

You should consider using Aggregate instead of Sum, as results may not always be the ones you expect using 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'  ; 
新一帅帅 2024-09-04 19:31:54

我假设 Ledger 维度在 FactLedger 中有一个键,但它无法与第一个 MDX 计算成员正确汇总的问题使我相信您可能需要重新考虑此层次结构。

然后,根据您的账本类型进行简单的 SUM 就可以了,这有意义吗?

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.

Then a simple SUM based on your ledger type would work, does that make sense?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文