How to get a COUNT of NON-NULL members in a set with some conditions?

*爱你&永不变心* 提交于 2019-12-12 04:53:33

问题


To give you a slight background to the problem, there are certain "users" who do certain "tasks" which are categorized into various types; one such type being "DSR". The requirement I am faced with is to find how many requests of type DSR the userid 033343 worked on for each month of year 2013.
What I would be needing is to get the count of the members in set of all the distinct requests a user (Userid = 033343) worked on in the year = 2013 and for the Request Type = DSR. Below is what I tried at first:

WITH SET requests AS
EXISTING [Request].[RequestID].MEMBERS

MEMBER Measures.[Count033343] AS
DISTINCTCOUNT(requests)

SELECT Measures.[Count033343] ON 1,
[Calendar].[CalendarMonthName].CHILDREN ON 0 /*Values like January, February, etc. Not tied to Calendar Years*/
FROM [Model]
WHERE(
                 [User].[UserID].&[033343],
                 [Request Type].[Request Type Name].&[DSR],
                 [Calendar].[CalendarYear].&[2013]

      )

But that didn't work. I got below result.

I figured, there is an issue with the set that I was building.

Then I moving the MDX to the Set definition. I thought it might make the code work faster as compared to having an extra set of tuple in WHERE clause.

WITH SET requests AS
EXISTS([Request].[RequestID].MEMBERS, (
                     [User].[UserID].&[033343],
                     [Request Type].[Request Type Name].&[DSR],
                     [Calendar].[CalendarYear].&[2013]
      ))

MEMBER Measures.[Count033343] AS
DISTINCTCOUNT(requests)

SELECT Measures.[Count033343] ON 1,
[Calendar].[CalendarMonthName].MEMBERS ON 0
FROM [Model]

But I was still getting the same result as above - 0 as the count for all the months!

Then, I thought of checking the set itself I was building(In the second example)

WITH SET requests AS
EXISTS([Request].[RequestId].MEMBERS, (
                     [User].[UserID].&[033343],
                     [Request Type].[Request Type Name].&[DSR],
                     [Calendar].[CalendarYear].&[2013]
      ))

SELECT [Measures].[Average of Late Tasks] ON 1,
requests ON 0
FROM [Model]

That proved two things.

1) The set was returning NULL values along with non null values.

2) There are indeed Request Ids returned from the set. So the count of Requests is definitely greater than 0 for the tuple specified.

I could use the NON EMPTY clause to remove the NON NULL values but, I don't want to use it as I read that it adds overhead.

As I have verified above that there are indeed requests for various users for all the months in 2013, why I am getting the count to be 0? Where am I going wrong? Why isn't the EXISTS and EXISTINGfunctionality working as expected? Isn't the set supposed to return only those Request IDs which exist for the condition? Is my understanding wrong?

EDIT

For @whytheq - With your final query, I got non-zero values

WITH MEMBER Measures.[x] AS
   Count(
     NONEMPTY(
       [Request].[RequestId].MEMBERS
       , {([Measures].[Average of Late Tasks])}
     )      
   )
SELECT 
   Measures.[x] ON 0,
   [Calendar].[CalendarYear].&[2013] ON 1 
FROM [Model]

OUTPUT

       x
2013   2

回答1:


(Not tested) Does this still return zeros?

WITH SET [requests] AS
  {([User].[UserID].&[033343],
  [Request Type].[Request Type Name].&[DSR])}
  * {[Measures].[Average of Late Tasks]}  //<<<<try running with and without this line
  * [Request].[RequestID].MEMBERS
MEMBER Measures.[Count033343] AS
  DISTINCTCOUNT(requests)
SELECT 
  [Calendar].[CalendarMonthName].CHILDREN ON 0,
  Measures.[Count033343] ON 1 
FROM [Model]
WHERE(
      [Calendar].[CalendarYear].&[2013]
      ) 

How about the following?

WITH 
  MEMBER Measures.[Count033343] AS
    DISTINCTCOUNT({[Request].[RequestID].[RequestID]})
SELECT 
  [Calendar].[CalendarMonthName].CHILDREN ON 0,
  Measures.[Count033343] ON 1 
FROM [Model]
WHERE(
      [Calendar].[CalendarYear].&[2013],
      [User].[UserID].&[033343],
      [Request Type].[Request Type Name].&[DSR]
      ) 

Maybe try this simpler query first and (if it works!) we can build up from this to try to locate the problem...

WITH MEMBER Measures.[x] AS
   Count(
     NONEMPTY(
       [Request].[RequestId].MEMBERS
       , {([Measures].[Average of Late Tasks])}
     )      
   )
SELECT 
   Measures.[x] ON 0,
   [Calendar].[CalendarYear].&[2013] ON 1 
FROM [Model]


来源:https://stackoverflow.com/questions/26506258/how-to-get-a-count-of-non-null-members-in-a-set-with-some-conditions

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