How to perform a Distinct Sum using MDX?

亡梦爱人 提交于 2019-12-10 22:15:42

问题


So I have data like this:

Date       EMPLOYEE_ID   HEADCOUNT   TERMINATIONS
1/31/2011        1            1             0 
2/28/2011        1            1             0
3/31/2011        1            1             0
4/30/2011        1            1             0
...
1/31/2012        1            1             0
2/28/2012        1            1             0
3/31/2012        1            1             0

1/31/2012        2            1             0
2/28/2011        2            1             0
3/31/2011        2            1             0
4/30/2011        2            0             1    

1/31/2012        3            1             0
2/28/2011        3            1             0
3/31/2011        3            1             0
4/30/2011        3            1             0
...
1/31/2012        3            1             0
2/28/2012        3            1             0
3/31/2012        3            1             0

And I want to sum up the headcount, but I need to remove the duplicate entries from the sum by the employee_id. From the data you can see employee_id 1 occurs many times in the table, but I only want to add its headcount column once. For example if I rolled up on year I might get a report using this query:

with member [Measures].[Distinct HeadCount] as
          ??? how do I define this???
select { [Date].[YEAR].children } on ROWS, 
       { [Measures].[Distinct HeadCount] } on COLUMNS 
       from [someCube]

It would product this output:

YEAR    Distinct HeadCount
2011           3
2012           2

Any ideas how to do this with MDX? Is there a way to control which row is used in the sum for each employee?


回答1:


You can use an expression like this:

WITH MEMBER [Measures].[Distinct HeadCount] AS
    Sum(NonEmpty('the set of the employee ids', 'all the dates of the current year (ie [Date].[YEAR].CurrentMember)'), [Measures].[HeadCount])

If you want a more generic expression you can use this:

WITH MEMBER [Measures].[Distinct HeadCount] AS
    Sum(NonEmpty('the set of the employee ids', 
                 Descendants(Axis(0).Item(0).Item(0).Hierarchy.CurrentMember, Axis(0).Item(0).Item(0).Hierarchy.CurrentMember.Level, LEAVES)),
        IIf(IsLeaf(Axis(0).Item(0).Item(0).Hierarchy.CurrentMember),
            [Measures].[HeadCount],
            NULL))


来源:https://stackoverflow.com/questions/13057676/how-to-perform-a-distinct-sum-using-mdx

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