SSRS Commonly Required Date Expressions

Reporting Services has many expressions that can manipulate date fields.  These expressions can be used just about anywhere in SSRS including object properties, displayed data, and parameters.

Recently I’ve worked on some projects that required date parameters to do things that you would typically see in an Analysis Services cube.  My client needed to see data brought back Week to Date, Month to date,  Year to date, and on with a rolling week.  My first suggestion of course was to create a cube to not only to do these calculations but get many other benefits.  Unfortunately, they did not take my advice so I had to use the SSRS expression language to return the results they were looking for.

I thought this made for a great blog topic so I could refer back to these whenever I need them.  All the reports had at least two parameters for start date and end date.  I needed to change the default values so they would default to show the above date range (YTD, MTD…etc).

End date would always have the current date or Today() in the expression language.  If you want time included then use Now().  The default value of the start date parameter will use the DateAdd function to return back a date that is a set number of units back.  The DateAdd function is constructed like this:

DateAdd(interval, number, date)

Argument Description
Interval This is the interval you want to add (Ex. Days, Months, Years…)
Number Numeric value that represents the number of intervals you want to add
Date The date to perform the addition to.

Expression I use to change my parameter default values.  These could be written many different ways and actually use VB in the customer code are but I wrote them this way if you’re not a VB expert.

Month over month

=DateAdd(“m”,-1,Today)

MTD

=DateAdd(“d”,-(Day(today)-1), Today)

Week over week

=DateAdd(“ww”,-1, Today)

or

=DateAdd(“d”,-7, Today)

WTD

=DateAdd(“d”,-DatePart(DateInterval.WeekDay,Today,0,0)+1,Today)

Year over Year

=DateAdd(“yyyy”,-1, Today)

YTD

=DateAdd(“d”,-DatePart(DateInterval.DayOfYear,Today,0,0)+1,Today)

Notice in the YTD and WTD expression I’m calling a DateInterval function that is actually a Visual Basic function but does not require you do add anything to the custom code section of your report.

One comment

  1. Hi Devin, great site to bookmark.
    Im trying to work out how long an event took via an expression.
    eg,
    I have two date filters – startdate & endDate,
    Events could start or end outside these parameters , so in that case i would need to calculate the difference between the 4 possible stop start times.
    hope that makes sense

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s