Previous month in parameter - date

I need to set a Hidden Default Parameter in SSRS.
In my query I need to not show last months total but the month before that, ie in Feb I need to show Dec totals.
I usually use this for last month but cannot tweak it for the month prior.
Set first date of last month:
=DateAdd("m", -1, DateSerial(Year(Now()), Month(Now()), 1))
Set last date of last month:
=DateAdd("d", -1, DateSerial(Year(Now()), Month(Now()), 1))

If i understand you right you want to:
Get the first day of two months from now
=DateAdd("m", -2, DateSerial(Year(Now()), Month(Now()), 1))
And you want to get the last day on month two months from now
=DateAdd("d" , -1 , DateAdd("m", -1, DateSerial(Year(Now()), Month(Now()), 1)))

Depending on exactly what you need, use something like:
DateAdd
(
DateInterval.Month
, -1
, DateAdd(DateInterval.Day, -1, DateSerial(Year(Parameters!Date.Value), Month(Parameters!Date.Value), 1))
)
Used in a report:

Related

Subtracting 1 ISO 8601 year from a date in BigQuery

I'm trying to manipulate a date value to go back in time exactly 1 ISO-8601 year.
The following does not work, but best describes what I want to accomplish:
date_add(date '2018-01-03', interval -1 isoyear)
I tried string conversion as an intermediate step, but that doesn't work either:
select parse_date('%G%V%u',safe_cast(safe_cast(format_date('%G%V%u',date '2018-01-03') as int64)-1000 as string))
The error provided for the last one is "Failed to parse input string "2017013"". I don't understand why, this should always resolve to a unique date value.
Is there another way in which I can subtract an ISO year from a date?
This gives the corresponding day of the previous ISO year by subtracting the appropriate number of weeks from the date. I based the calculation on the description of weeks per year from the Wikipedia page:
CREATE TEMP FUNCTION IsLongYear(d DATE) AS (
-- Year starting on Thursday
EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 5 OR
-- Leap year starting on Wednesday
(EXTRACT(DAY FROM DATE_ADD(DATE(EXTRACT(YEAR FROM d), 2, 28), INTERVAL 1 DAY)) = 29
AND EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 4)
);
CREATE TEMP FUNCTION PreviousIsoYear(d DATE) AS (
DATE_SUB(d, INTERVAL IF(IsLongYear(d), 53, 52) WEEK)
);
SELECT PreviousIsoYear('2018-01-03');
This returns 2017-01-04, which is the third day of the 2017 ISO year. 2018-01-03 is the third day of the 2018 ISO year.

SSRS expression, iif statement with date conditions

So my default values for startDate and endDate in SSRS were set up with the following ssrs expressions.
first day of previous month ssrs expression
=DateAdd(DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))
last day of previous month ssrs expression
=DateAdd(DateInterval.Day, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))
But that won't work alone in my case unless I want to go in on the 16th of every month and generate this report for the people requesting it for the first 15 days of the current month.
So in my default value expression for start date i am trying this iif statement...
= iif(
DatePart(DateInterval.Day, Today() <> "20",
DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1),
DateInterval.Month, 1, DateSerial(Year(Date.Now), Month(Date.Now), 1)
)
Not working out so well. So what i'm trying to do is.....
Change the default start and end date based on what day of the current month it is, So if current day of the current month equals 16, make start date 1 of current month and end date 15 of current month, if current day of the month isn’t 16 make start date first of previous month and end date last day of previous month. So then the only thing needed is to get subscription emails and what day to send them out on.
Untested, but what if you try this? (for your start date parameter):
= iif(
DatePart(DateInterval.Day, Today()) <> "16",
DateAdd(DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1)),
DateSerial(Year(Date.Now), Month(Date.Now), 1)
)

Get records from this week

I'm a bit puzzled on how to get all records from my table where CheckInDate (datetime) occurred THIS WEEK. As in, since Sunday morning at midnight.
To get THIS MONTH was easy:
and year(eci.CheckInDate) = year(getdate())
and month(eci.CheckInDate) = month(getdate())
...but there is no "week" function similar to MONTH() and YEAR(). Can someone give me a code example on how to do this?
Got it:
AND eci.CheckInDate > cast(dateadd(day,1-datepart(dw, getdate()), getdate()) as date)
Here is a solution that can be used to do what you want:
declare #WeekStart datetime = dateadd(week, datediff(week, 0, GetDate()), 0)
select #WeekStart
It's simpler than it looks. Here's what it is doing:
Add to week 0 the number of weeks from 0 to now.
Simple yet effective.
By the way, you can use the same method for truncating to second, minute, hour, day, week, month, quarter, etc.

Check if the difference between dates is exactly 'n' months in expression SSRS

In my quarterly report Im trying to validate the two parameters StartDate and EndDate.
I first check if the difference between the dates is 2 months:
Switch(DateDiff(
DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) <> 2,
"Error message")
Then I try to add whether the StartDate is the first day of month AND EndDate is last day of month:
And (Day(Parameters!StartDate.Value) <> 1
And Day(DATEADD(DateInterval.Day,1,Parameters!EndDate.Value)))
So the whole expression looks like this:
Switch(DateDiff(DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) <> 2
And
Parameters!IsQuarterly.Value = true
And
Day(Parameters!StartDate.Value) <> 1
And
Day(DATEADD(DateInterval.Day,1,Parameters!EndDate.Value))<>1),
"Error: Quarterly report must include 3 months")
But It works wrong when the difference between dates is still 2 months, but StartDate and EndDate are not first and last day of the whole period.
I'd appreciate any help :)
I would say just change the implementation Add another two Parameter With Quarter and Year
Quarter like Q1,Q2,Q3 & Q4 with Value 1,2,3 & 4 respectively and year 2012,2013,2014 & so on
Now based on the parameter selected Qtr & Year set Default value of start & End Date
=DateSerial(Parameters!Year.Value), (3*Parameters!Qtr.Value)-2, 1) --First day of Quarter
=DateAdd("d",-1,DateAdd("q",1,Parameters!Year.Value, (3*Parameters!Qtr.Value)-2, 1))) --Last day of quarter
Doing this no need to do any validation bcz its always get the correct Date Difference.
Other Reference
First day of current quarter
=DateSerial(Year(Now()), (3*DatePart("q",Now()))-2, 1)
Last day of current quarter
=DateAdd("d",-1,DateAdd("q",1,DateSerial(Year(Now()), (3*DatePart("q",Now()))-2, 1)))

Crystal Date Parameter for last year

I have tried using this formula with a date parameter. It works for the other 11 months but not the 12th month. Could some one please let me know what additional code I need to add to the formula.
If {?End Date} = 12/14/13
I want the formula to = 12/31/12
This is what I have been using to make it work for the other 11 months.
Date(year({?End Date})-1, month({?End Date})+1,1) - 1
I receive an error that the month needs to be 1 - 12.
Any suggestions will be greatly appreciated.
Thanks
This will give you the last day of the month of the previous year:
DateAdd("m", 1, DateTime( Year({immaster.timestmp})-1, Month({immaster.timestmp}), 1, 0,0,0 )) - 1
Try this.
CDate(Year(DateAdd ('yyyy',-1,CurrentDate)),Month(DateAdd ('M',0,CurrentDate)),31)