Search code examples
mdxolap

Simulate group by in mdx


I have a Fact Table That has the list of all courses that every employee in the company has been passed.

FactPassedCourse: (CourseKey, EmployeeKey, DateKey)

DimEmployee: (EmployeeKey)

DimCourse: (CourseKey)

DimEmployee

Emp1 , Emp2

DimCourse

Course1, Course2, Course3, Course4, Course5

FactPassedCourse
-----------------------------------------
(Emp1, Course1, DateKey1)
(Emp1, Course2, DateKey2)
(Emp1, Course2, DateKey3)
(Emp1, Course3, DateKey3)
(Emp1, Course4, DateKey3)
(Emp2, Course4, DateKey1)
(Emp2, Course5, DateKey5)

DimEmployee & DimCourse Has Regular Relationship With FactPassedCourse in my Cube.

I want to get the count of Employees based on every courses but if an employee passed a special course(like Course2) more than once, the employee must be counted just one time I mean this result I want:

MDX result:

(Coursename: Course1    Employee_Count: 1)
(Coursename: Course2    Employee_Count: 1:because ONE employee passed this course two times)
(Coursename: Course3    Employee_Count: 1)
(Coursename: Course4    Employee_Count: 2: because two different employees passed this course)
(Coursename: Course5    Employee_Count: 1)

In order to get this result I wrote this mdx query and this return the correct result but the performance of this query is very slow.

With 
  Member [measures].[Employee_Count] as 
     Count(
        Filter(
          [DimEmployee].EmployeeKey].members-[DimEmployee].[EmployeeKey].[All]),
          [measures].[FactPassedCourseCount]>0)
        )
  Set [Course_Set] as 
     Filter(
       ([DimCourse].[CourseKey].members–[DimCourse].CourseKey].[All]),
        [measures].[FactPassedCourseCount] > 0
     )
Select 
   [measures].[Employee_Count] on 0
 , [Course_Set] on 1 
From MyCube

This Query Result is:

(Coursename: Course1    Employee_Count: 1)
(Coursename: Course2    Employee_Count)
(Coursename: Course3    Employee_Count: 1)
(Coursename: Course4    Employee_Count)
(Coursename: Course5    Employee_Count: 1)

is there a better way to write this mdx query that have fast performance?


Solution

  • Is this quicker?

    With 
      Member [measures].[Employee_Count] as 
         Count(
            EXISTING 
               NONEMPTY(
                 [DimEmployee].[EmployeeKey].[EmployeeKey]
                ,[measures].[FactPassedCourseCount]
               )
            )
    Select 
       [measures].[Employee_Count] on 0
     , NONEMPTY(
          [DimCourse].[CourseKey].[CourseKey]
         ,[measures].[FactPassedCourseCount]
       ) on 1 
    From [MyCube];