Question

Okay this is a fun one and part of what I did so far from reading thus far some people are claiming this cannot be done due to SSRS's default behavior of handling default values. However I have gotten pretty far and I am curious if I can get it all the way.

Issue: What I want to do is have a user get PRE DEFINED between statements such as 'This Month', 'LastMonth', etc... This then populates a variable with the DateTime for that string reference, but a user can change it later and it should refresh based on parent string or else let user choose a different one on SINGLE DATETIME PARAMETER.

To start let's work through what will work and won't:

I. I bind a dataset to this, an abbreviated form would be:

select 
'Today' as Description
,   DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) BegDt
union
select 
    'ThisMonth'
,   Dateadd(month, Datediff(month, 0, getdate()), 0) 

II. Then I set up a parameter called 'DatesPeriod' and then add the 'Description' as the field and label for the parameter when choosing 'get values from a query'. The user can now select either 'Today' or 'ThisMonth' from a dropdown.

III. I now set up another dataset and label it 'DatesSelected'. For my purposes of explanation I will keep it simple and just nest the dataset from part I but list a where clause. Essentially I am using the same dataset again but forcing a one row return at a time.

select *
from 
    (
    select 
        'Today' as Description
    ,   DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) BegDt
    union
    select 
        'ThisMonth'
    ,   Dateadd(month, Datediff(month, 0, getdate()), 0) 
    ) as x
where x.Description = @DatesPeriod

IV. I define another parameter called 'Start' which then is DateTime and gets its values from the dataset above as well as it's default values. Now when a user changes the first text variable the dependent DateTime variable will change as well. I have bound a text choice essentially to a datetime variable that now is limited in scope to only the available and default value for a description. This limits scope to one, forcing a default to be passed.

V. That is not enough though, I want a user to be able to select FREE DATE RANGES at will. The current setup will NOT ALLOW this date picker to be used as scope is limited by AVAILABLE VALUES. However this needs to be done to pass a text to a datetime variable that will refresh ON DEMAND (when it is changed while running the report, not just the first time). So I create another DateTime parameter and call it 'UserStart' and have the default be '[@Start]' to get it's default value as a dependency from 'Start'.

VI. I create a testing dataset called 'Choice' up now to visually see that my data is presenting as I want it too.

 Select isnull(@UserStart, @Start) as Choice

VII. I create a table object and put it on my report and put in the single cell I listed above to see that when a user overrides the default 'Start' value with their own choice (from a calendar display as offered by SSRS) that it will display.

So everything works with a caveot at this point. You need to display BOTH datetime parameters to allow an end user to select either a default set or choose their own. If I try to make the second parameter 'Start' hidden it will only set a default the FIRST TIME ONLY the report is ran, I want it everytime the first parameter is changed. If it is visible it works fine but for functionality I would not want a user to see two report parameters that as far as they are concerned are the same. I have tried custom code for returning the 'Start' parameter, creating yet another dataset, but as far as I know SSRS is made to not be flexible with a default refresh or an ANY DATE you want to pick. If you want to refresh a parameter and have it do so on the screen from another variable you need to tell it it's available values AND default values changed. However this gets rid of the calendar option then so you need to pass the parameter to another parameter. However if you hide the second parameter than it will not pass the value.

Any help would be much appreciated.

Was it helpful?

Solution

According to Microsoft Connect the ability to not refresh default parameters is "by design"

"As described, this is not a bug. We do not re-evaluate the default value for a subsequent parameter unless the selected value is no longer in the valid values list. We do not know whether the current value was specifically requested by the user or it is there because of the default. You could make a case to have control over this behavior through some sort of property but it is currently working as designed." - Microsoft

Suggested Workaround:

Been playing with this one for a while now, only solution i can see is to change how you intend the users to use the report.

Instead of having UserStart display the date calculated by the query you use it as override date and allow it to accept null values by and set null as the default value. Then in the query use the something like:

where Table.DateStart < isnull(@UserStart, @Start)

This allows users to select the DatePeriod run the report or update with there own custom date range.

Sample:

ssrs sample

OTHER TIPS

After much research, the above provided the final clue to a solution. There are a number of steps, but it does allow the user to select a predefined date range or enter there own dates.

  1. Add a text parameter "DateRange". Set AvailableValues like:

Choose Dates:Choose

This Year:ThisYear

Last Year:LastYear

All:All

Set DefaultValues to None

  1. Create a stored procedure "GetDateRangeFromDesc" as follows:

CREATE PROCEDURE [dbo].[GetDateRangeFromDesc]

@Range varchar(50)

AS

BEGIN

SET NOCOUNT ON;

SELECT
    CASE 
WHEN @Range = 'Choose' THEN NULL
WHEN @Range = 'ThisYear' THEN DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
WHEN @Range = 'LastYear' THEN DATEADD(yy, DATEDIFF(yy,0,getdate()) - 1, 0)
WHEN @Range = 'All' THEN Convert(DATE, '1/1/1900') END AS Date1,
CASE
WHEN @Range = 'Choose' THEN NULL
WHEN @Range = 'ThisYear' THEN DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, 0)
WHEN @Range = 'LastYear' THEN DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
WHEN @Range = 'All' THEN Convert(DATE, '12/31/2099') END AS Date2

END
GO
  1. Add a Dataset "DateRangeValues"

Select the stored procedure "GetDateRangeFRomDesc"

Fields should be Date1 and Date2

Parameters should be @Range:[@DateRange]

  1. Add a Date/Time parameter "StartDate". Set AvailableValues and DefaultValues as:

Get values from a query. Dataset: DateRangeValues; Value/Label field: Date1

  1. Add a Date/Time parameter "EndDate". Set AvailableValues and DefaultValues as:

Get values from a query. Dataset: DateRangeValues; Value/Label field: Date2

When you run the report, the Start and End dates will populate as determined in the stored procedure. Add as many date ranges as you need to sp. Just have a choice that returns NULL so the user can enter his own values.

** Sorry, but I posted before fully testing. When you select Choose, then enter a date, everything refreshes and the date goes away. :) Maybe someone can add the final piece that will allow this to work.

Another update: This works when DateRange parameter is after the last parameter that causes a refresh!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top