如何使用MDX执行不同的总和?

时间:2012-10-24 21:01:55

标签: mdx mondrian

所以我有这样的数据:

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

我想总结人数,但我需要通过employee_id从总和中删除重复的条目。从数据中可以看出employee_id 1在表中多次出现,但我只想添加一次headcount列。例如,如果我在一年中累积起来,我可能会使用此查询获得报告:

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

它会产生这个输出:

YEAR    Distinct HeadCount
2011           3
2012           2

有关如何使用MDX执行此操作的任何想法?有没有办法控制每个员工在总和中使用哪一行?

1 个答案:

答案 0 :(得分:1)

你可以使用这样的表达式:

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])

如果你想要一个更通用的表达式,你可以使用它:

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))