This need is a little strange, and I can do it in C#, but can’t seem to get the expression syntax right for Report Designer.
I want to set default Start and End parameters for a report. To do this, I need “date only” values for the first Saturday of a month, and the Friday date of the last week of that month. This means the latter value can actually be in the next month. So for 2012, my values would look like this:
StartDate: 10/06/2012
EndDate: 11/02/2012
StartDate: 11/03/2012
EndDate: 11/30/2012
StartDate: 12/01/2012
EndDate: 01/04/2013
I’ve been working with conditional statements using Month(), Weekday(), WeekdayName, etc. but not having good luck. Has anyone done something like this?
You probably don’t need any hefty conditional statements. If you’re doing it to simulate loops and other imperative programming features, it may be easier to create a function in Custom Code to do it.
An alterantive is to look for an algorithm for the particular calculations you’re doing. For example this answer can be a great basis for getting your needed dates, be it with a little adjustment. Here’s how it would work for “The first Saturday of this month”:
Day of the week for the day 1 of this month:
How many days should we add to that first day of the month:
So the date for the first Saturday of the month is:
There are probably even smarter ways to do this, though VBScript expressions are a wee bit limited. The example above can be tweaked to get the “Friday of the last week of the month” as well.
Bottom line: the (indirect, but generic) answer to your question is to think about the algorithm to get to your wanted date (as declarative as possible, because you can’t do loops and stuff in Expressions easily).