user5021612
user5021612

Reputation:

How to set SSRS expression to return previous month?

I want to set parameter's default value using SSRS expression to previous month in a year.

I've tried:

=CDate(
    DateSerial(
        IIf(DatePart("m", Today())=12, DatePart("yyyy",Today())-1, DatePart("yyyy",Today())),
        IIf(DatePart("m", Today())=12, 1, DatePart("m",Today())-1),
        1
    )
)

Upvotes: 0

Views: 2526

Answers (1)

C Black
C Black

Reputation: 1008

I think you're over-complicating this, you don't need to check yourself for a change of year - let the built in functions do that for you.

There could be better solutions, but this has always worked for me.

If you just want the first day of the previous month, then:

=DateSerial(
    Year(
        DateAdd("m", -1, Today())
    ),
    Month(
        DateAdd("m", -1, Today())
    ),
    1
)

For my purposes, I also typically need the last day of the previous month, for which I use the following:

=DateSerial(
    Year(
        DateAdd("m", -1, Today())
    ),
    Month(
        DateAdd("m", -1, Today())
    ),
    Day(
        DateAdd("d", -1, DateSerial(Year(Today()), Month(Today()), 1))
    )
)

Upvotes: 1

Related Questions