selecting max of distinct count when on multiple rows

74 Views Asked by At

I have a query that does a distinct count of accounts based on CB score. The problem I have is that an account can switch credit scores pretty often. So I would like to take the max credit score for a particular account. Here is what I have that is returning higher than I want because of the switching of CB scores.

So for example account 1234 initially has a cb score of 500, then later has a cb score of 550. The account 1234 is now in both cb score ranges, but I only want to count them once.

WITH 
  MEMBER mem1 AS 
    Count
    (
      NonEmpty
      (
        [AccountID].[Account ID].[Account ID].MEMBERS
       ,measures.[Transaction Amount]
      )
    ) 
SELECT 
  NON EMPTY 
    {mem1} ON 0
 ,[CB Score].[RANGE].[RANGE] ON 1
FROM [CUBE]
WHERE [PROMO].[PROMO].&24
1

There are 1 best solutions below

2
On BEST ANSWER

Try this:

WITH 
  MEMBER mem1 AS 
    Sum
    (
        [AccountID].[Account ID].[Account ID].MEMBERS
       ,IIF(
         Not IsEmpty([Measures].[Transaction Amount]) 
         And IsEmpty(Sum({[CB Score].[RANGE].CurrentMember.NextMember : null}, [Measures].[Transaction Amount])),
         1,
         Null
      )
    ) 
SELECT 
  NON EMPTY 
    {mem1} ON 0
 ,[CB Score].[RANGE].[RANGE] ON 1
FROM [CUBE]
WHERE [PROMO].[PROMO].&24

Basically the logic is to count the account if they had a transaction in that slice and if no greater CB Score range has a transaction.