Quantcast
Channel: SCN : Blog List - All Communities
Viewing all articles
Browse latest Browse all 2548

How to implement a Running Count Distinct

$
0
0

Hi,

There was a particularly tough question (at least for me) posted by Michal Radomski in WEBI Running Count Distinct with tooke me some time to figure out how to solve it.

 

The question was how to implement a RunningCount of distinct elements The OP had a list of IDs and dates and he needs to count the distinc elements over time (a date), his data is somethong like this :

 

dateID
01/01/20091
01/01/20092
01/01/20093
01/01/20094
01/01/20091
01/01/20095
01/01/20092
01/01/20096
02/01/20093
02/01/20097
02/01/20098
02/01/20099
02/01/20092

 

 

resulting in this :

 

 

date
Running Count Distinct
01/01/2009
6
02/01/2009
9

 

 

 

In order to do so,I proposed a solution using the following formula , which I´ll try to explain

 

[Running Count Distinct]  =RunningSum(Sum(If ( ([Id] ForEach([Id];[date])  In ([Id];[date]) = Previous([Id] ForEach ([Id];[date]) )) In ([Id];[date]);0;1) ) )

 

The idea behind this is compare , row by row, The [id] element with its previous [id] object  AS IF IT WAS ORDERED BY ID INSTEAD OF DATE , in

case it´s equal  i sum 0 otherwise 1.

 

As the data is, in fact ordered by [date], I force the comparison to be evaluated as if was ordered by [id] and redefine the context to include [id].

This is accomplished by setting the correct order in the redefinition context ([ID];[date]).

 

In fact, when redefining context, the order in which the objects are set matters in terms of the final result.

 

Have ypu all a nice WEEK END.

 

Regards,

Rogerio


Viewing all articles
Browse latest Browse all 2548

Trending Articles