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

Posted by ShaneD on Stack Overflow See other posts from Stack Overflow or by ShaneD
Published on 2010-04-16T17:35:24Z Indexed on 2010/04/16 17:53 UTC
Read the original article Hit count: 322

Filed under:
|
|
|

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.

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 

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.

sum({([Ledger].[Type].&[Credit]), ([Ledger].[Type].&[Paid]), ([Ledger].[Type].&[Held Money: Copay])},  [Measures].[Amount - 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?

© Stack Overflow or respective owner

Related posts about mdx

Related posts about olap

  • MDX lekérdezések az Oracle OLAP-hoz

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    Az Oracle OpenWord-ön, 2009. október 12-én jelentette be az Oracle, hogy elkészült a Simba Technologies MDX eszköze az Oracle OLAP eléréséhez: Oracle and Simba Technologies Introduce MDX Provider for Oracle® OLAP. Az MDX Provider for Oracle® OLAP eszközzel közvetlenül az Excel felületrol lehet elérni… >>> More

  • An OLAP client!

    as seen on SQL Blog - Search for 'SQL Blog'
    While surfing CodePlex I’ve come across a very interesting tool for all BI Developers who misses a decent OLAP client where to write, run & test MDX queries http://ranetuilibraryolap.codeplex.com/ I’ve not tested it yet, but I’ll surely do this week and I’ll post my impressions ASAP. The first… >>> More

  • An OLAP client!

    as seen on SQL Blog - Search for 'SQL Blog'
    While surfing CodePlex I’ve come across a very interesting tool for all BI Developers who misses a decent OLAP client where to write, run & test MDX queries http://ranetuilibraryolap.codeplex.com/ I’ve not tested it yet, but I’ll surely do this week and I’ll post my impressions ASAP. The first… >>> More

  • OWB 11gR2 – OLAP and Simba

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    Oracle Warehouse Builder was the first ETL product to provide a single integrated and complete environment for managing enterprise data warehouse solutions that also incorporate multi-dimensional schemas. The OWB 11gR2 release provides Oracle OLAP 11g deployment for multi-dimensional models (in addition… >>> More

  • Building dynamic OLAP data marts on-the-fly

    as seen on SQL Blogcasts - Search for 'SQL Blogcasts'
    At the forthcoming SQLBits conference, I will be presenting a session on how to dynamically build an OLAP data mart on-the-fly. This blog entry is intended to clarify exactly what I mean by an OLAP data mart, why you may need to build them on-the-fly and finally outline the steps needed to build… >>> More