SirRobin
SirRobin

Reputation: 23

SSRS group by ranges

I have a field in a dataset that contains a formula (using IFF and datediff) that determines how long a record was open (there is a CreatedDate and a ClosedDate). I am then grouping by that field.

However, I want to be able to group that data into ranges. So right now, the header is Days Open and the three rows say 7, 5, and 1 (days). But the rows will grow as more data comes in, as Days Open could be any number between 1 and 50. That means I could end up with 50 rows.

I would like there to be a set number of rows, each within a range. So one row will have "1-5 days open" and then count how many rows are >=5, another row with "6-10 days open", and count those. Etc.

How can I group a calculated field by different ranges? The only way I can think of is to write a whole lot of SQL.

Upvotes: 1

Views: 2487

Answers (1)

Nathan Griffiths
Nathan Griffiths

Reputation: 12776

So if I've understood correctly, you have added a field to a Dataset that uses an expression to calculate the "Days Open" value based on two other fields (CreatedDate and ClosedDate).

You already know that the value of Days Open can be 1 - 50 and you know what ranges you want, so you could add another calculated field to the Dataset that is based on the same calculation as Days Open, e.g.

Assuming your existing Days Open calculation is

=DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value)

then you could add

=Switch(
   DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) > 0 And 
   DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) <=5, 
   "1-5 Days",
   DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) > 5 And 
   DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) <=10, 
   "6-10 Days",
   DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) > 11 And 
   DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) <=15, 
   "11-15 Days",
...
   DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) > 45 And 
   DateDiff(DateInterval.Day, Fields!CreatedDate.Value, Fields!ClosedDate.Value) <=50, 
   "45-50 Days"
)

Although this could be vastly simplified if you can modify the actual SQL query for the Dataset to perform the Datediff function, e.g. in T-SQL for SQL Server you could add

SELECT 
...
DATEDIFF(dd, CreatedDate, ClosedDate) AS DaysOpen
...
FROM
...

which would give you a single Dataset Query field that you could use in the above expression e.g.

=Switch(
   Fields!DaysOpen.Value) > 0 And 
   Fields!DaysOpen.Value) <=5, 
   "1-5 Days",

Whichever way you calculate it, you can then use this Dataset field containing the range values in a simple Grouping expression in your tablix.

Upvotes: 3

Related Questions