Question

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?

Was it helpful?

Solution

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":

  1. Day of the week for the day 1 of this month:

    =Weekday(DateAdd("d", 1 - Now().Day, Now()), FirstDayOfWeek.Sunday)
    
  2. How many days should we add to that first day of the month:

    =7 - Weekday(DateAdd("d", 1 - Now().Day, Now()), FirstDayOfWeek.Sunday)
    
  3. So the date for the first Saturday of the month is:

    =DateAdd("d", 
         7 - Weekday(DateAdd("d", 1 - Now().Day, Now()), FirstDayOfWeek.Sunday),
         DateAdd("d", 1 - Now().Day, Now())
        )
    

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).

OTHER TIPS

Here is what I got for the first part. There is probably a much better way to accomplish this. What I did was get the current month and subract one from it. Then I concatenated the date and the current year to the month. Then I did a check on the name of the day to see if it was Saturday. If it was, then I used that date. Otherwise I did the next day with the same check, and so on. If you want to use this, you will need to add additional logic to it if you are in January. You would also substitute NOW() with the parameter you are using.

=IIF( WeekdayName(DatePart("w", (CDate((Month(NOW()) - 1) & "/1/" & YEAR(NOW()))))) = "Saturday", (CDate((Month(NOW()) - 1) & "/1/" & YEAR(NOW()))), 
(IIF( WeekdayName(DatePart("w", (CDate((Month(NOW()) - 1) & "/2/" & YEAR(NOW()))))) = "Saturday", (CDate((Month(NOW()) - 1) & "/2/" & YEAR(NOW()))), 
    (IIF( WeekdayName(DatePart("w", (CDate((Month(NOW()) - 1) & "/3/" & YEAR(NOW()))))) = "Saturday", (CDate((Month(NOW()) - 1) & "/3/" & YEAR(NOW()))),
        (IIF( WeekdayName(DatePart("w", (CDate((Month(NOW()) - 1) & "/4/" & YEAR(NOW()))))) = "Saturday", (CDate((Month(NOW()) - 1) & "/4/" & YEAR(NOW()))),
            (IIF( WeekdayName(DatePart("w", (CDate((Month(NOW()) - 1) & "/5/" & YEAR(NOW()))))) = "Saturday", (CDate((Month(NOW()) - 1) & "/5/" & YEAR(NOW()))),
                (IIF( WeekdayName(DatePart("w", (CDate((Month(NOW()) - 1) & "/6/" & YEAR(NOW()))))) = "Saturday", (CDate((Month(NOW()) - 1) & "/6/" & YEAR(NOW()))),
                    (IIF( WeekdayName(DatePart("w", (CDate((Month(NOW()) - 1) & "/7/" & YEAR(NOW()))))) = "Saturday", (CDate((Month(NOW()) - 1) & "/7/" & YEAR(NOW()))),
"1/1/2010")))))))))))))

On another note, there would be a very easy way to get this if you have a calendar table in SQL Server (especially for when you need the Friday). You could do a simple select statement such as the following:

SELECT
    MIN(date)
FROM
    calendar c
WHERE
    MONTH(DATEADD("m", -1, GETDATE())) = MONTH(date)
    AND YEAR(DATEADD("m", -1, GETDATE())) = YEAR(date)
    AND datename(dw, date) = 'Saturday'

Get the Last Friday of the last month.....

=DateAdd(DateInterval.Day,- Weekday(DateAdd("d", 1 - Now().Day, Now()), FirstDayOfWeek.Saturday), DateSerial(Year(Date.Now), Month(Date.Now), 1))

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