SSRS Expression to count the distinct number of items visible

whytheq picture whytheq · Oct 11, 2012 · Viewed 9.3k times · Source

Currently I'm using this expression in a cell above the column headers of a table:

= "Num Days " & CountDistinct(Fields!Date.Value)

In the report designer this is the context of the expression:

enter image description here

Here is what the result looks like:

enter image description here

The expression is working ok i.e in the data set there are 60 distinct dates which is what the expression returns. However there is a filter on the group (Date2) so that it only has 10 visible days in the tablix.

How do I amend the expression to return just the dates that are visible?

Answer

Jamie F picture Jamie F · Oct 11, 2012

The easiest solution is to move the filter upstream: can you put the filter on the Dataset instead of the Tablix Group? Then these rows won't be included in your total.

Other options include custom code to keep a running tally of values, or putting a conditional in your aggregate expression, maybe something like:

   = "Num Days " &
     (COUNTDISTINCT( IIF(DateDiff("d", Fields!Date.Value, Now) <= 10,  Fields!Date.Value, 1)) -1 )