Calculated measure/dimension

你。 提交于 2019-12-06 12:02:40

I should have mentioned that I am using Mondrian/MySQL. I came to similar conclusion but found a way to create a new degenerate dimension using an SQL key expression (so I don't actually need to add column to table):

<Dimension name="PNLCategory">
    <Hierarchy hasAll="true">
        <Level name="PNLCategory" column="pnlCategory" uniqueMembers="true">
            <KeyExpression>
                <SQL dialect="generic"> <![CDATA[IF(pnl >= 0,'Winner','Loser')]]></SQL>
            </KeyExpression>
        </Level>
    </Hierarchy>
</Dimension>

Now it becomes easy to do the calculated member:

<CalculatedMember name="WinnersCountByPNL" aggregator="count" dimension="Measures">
  <Formula>([PNLCategory].[Winner], Measures.PNL)</Formula>
  <CalculatedMemberProperty name="FORMAT_STRING" value="$#,###"/>
  <CalculatedMemberProperty name="DATATYPE" value="Numeric"/>
</CalculatedMember>     

so here I restrict the summation to only "winners" and the benefit is that Mondrian will not retrieve count(fact) entries from the row table.

If you want to do something in MDX on the row detail level you will need a dimension containing the ID of the fact table (so each member in the dimension represents a row in the fact table). Then you can write a calculation:

WITH MEMBER Measures.PositivePNL as
'
Sum([DimFactId].[DimFactId].Members, IIF(Measures.PNL > 0, Measures.PNL, 0))
'

But this can be slow if you have a lot of rows in your fact table. Alternative is to add a column in your fact table containing only the positive values of PNL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!