简体   繁体   中英

Can the value of a subscription parameter be set to an expression when creating an SSRS report subscription in SQL Server 2014?

I have a report with two Date parameters "StartDate" and "EndDate."

I want to create multiple subscriptions to the report covering different time periods like previous calendar month, previous week, previous weekend, etc.

The most straightforward way that I can think of doing this is would be if the StartDate and EndDate parameters could be set to different expressions for each subscription. However I can't seem to get expressions to work at all from the subscription page. Is this possible?

The way I have handled this type of situation was to add a new parameter to the report that would allow you choose a report period to run for. This value would in turn be used to determine what dates to use for start date and end date.

The following steps outline how to do this.

  1. Add a new parameter to the report named ReportPeriod .

    Add the following Label(Value) pairs to the Available Values (Specified values) list.

    • Daily (D)
    • Weekly (W)
    • Monthly (M)

Make one of these the default, which doesn't matter.

Move this new parameter up in the parameters list so that is above(before) the StartDate and EndDate parameters. They will derive their values based the chosen ReportPeriod , so it must come before them.

  1. Set the default value expression for the StartDate parameter to this:

     =Switch(Parameters!ReportPeriod.Value = "D", Today.AddDays(-1), Parameters!ReportPeriod.Value = "W", DateAdd(DateInterval.WeekOfYear, -1, DateAdd(DateInterval.Day, -(DatePart(DateInterval.Weekday, Today, 0, 0)-1), Today)), Parameters!ReportPeriod.Value = "M", DateAdd(DateInterval.Month, -1, DateAdd(DateInterval.Day, -(DatePart(DateInterval.Day, Today, 0, 0)-1), Today))) 
  2. Set the default value expression for the EndDate parameter to this:

     =Switch(Parameters!ReportPeriod.Value = "D", Today, Parameters!ReportPeriod.Value = "W", DateAdd(DateInterval.Day, -(DatePart(DateInterval.Weekday, Today, 0, 0)), Today), Parameters!ReportPeriod.Value = "M", DateAdd(DateInterval.Day, -(DatePart(DateInterval.Day, Today, 0, 0)), Today)) 

You need to know that changing the ReportPeriod when running the report by hand will not update the StartDate and EndDate parameters right in front of you eyes. However, changing the default ReportPeriod will affect the values. More importantly , what ReportPeriod you choose when creating a report subscription will also affect the date values. Therefore, creating report subscriptions for this report, one for each ReportPeriod , will give you the results you desire.

Feel free to add more ReportPeriod available values, like Quarterly, or Annually, and adjust the expressions as needed.

Hope this helps you.

You can write expressions in SSRS design environment to set a default value for the date. The same will reflect in its subscriptions as "Report Default Value". Custom -configure the date feature using "Override Report Default" is not available even in Data-driven subscriptions in SSRS up to 2012. Users can only select a fixed date.

Maybe this article helps:

https://www.sqlservercentral.com/Forums/1104266/Creating-a-dynamic-date-parameter-in-a-SQL-Reporting-Services-2005-subscription

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM