Crystal Date Parameter for last year - date

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)

Related

How do I Determine The Number Of Days In A Month from a particular date in angularjs 2?

I am using angularjs 2 and type script.
I want the number of days in a month from a particular date given by user.
How do I Determine that?
Please help. Thanks in Advance.
As pointed out by Evgeny Shmanev, this answer is incorrect. Please see the correct answer by Zvi Tarem
You don't need angular, you can call this function:
function daysRemainingInMonth(date) {
var year = date.getYear();
var month = date.getMonth();
return new Date(year, month, 0).getDate()
}
Edit: Updated now that I know you have the date
It may be a bit late, but the right answer is a follows:
In order to find the number of days in a month, you need to get the 'date' of the 0th day of the next month:
let daysInMonth = new Date(year, month + 1, 0).getDate();

convert year-month string into daily dates

recently I asked how to convert calendar weeks into a list of dates and received a great and most helpful answer:
convert calendar weeks into daily dates
I tried to apply the above method to create a list of dates based on a column with "year - month". Alas i cannot make out how to account for the different number of days in different months.
And I wonder whether the package lubridate 'automatically' takes leap years into account?
Sample data:
df <- data.frame(YearMonth = c("2016 - M02", "2016 - M06"), values = c(28,60))
M02 = February, M06 = June (M11 would mean November, etc.)
Desired result:
DateList Values
2016-02-01 1
2016-02-02 1
ect
2016-02-28 1
2016-06-01 2
etc
2016-06-30 2
Values would something like
df$values / days_in_month()
Thanks a million in advance - it is honestly very much appreciated!
I'll leave the parsing of the line to you.
To find the last day of a month, assuming you have GNU date, you can do this:
year=2016
month=02
last_day=$(date -d "$year-$month-01 + 1 month - 1 day" +%d)
echo $last_day # => 29 -- oho, a leap year!
Then you can use a for loop to print out each day.
thanks to answer 6 at Add a month to a Date and answer for (how to extract number with leading 0) i got an idea to solve my own question using lubridate. It might not be the most elegant way, but it works.
sample data
data <- data_frame(mon=c("M11","M02"), year=c("2013","2014"), costs=c(200,300))
step 1: create column with number of month
temp2 <- gregexpr("[0-9]+", data$mon)
data$monN <- as.numeric(unlist(regmatches(data$mon, temp2)))
step 2: from year and number of month create a column with the start date
data$StartDate <- as.Date(paste(as.numeric(data$year), formatC(data$monN, width=2, flag="0") ,"01", sep = "-"))
step 3: create a column EndDate as last day of the month based on startdate
data$EndDate <- data$StartDate
day(data$EndDate) <- days_in_month(data$EndDate)
step 4: apply answer from Apply seq.Date using two dataframe columns to create daily list for respective month
data$id <- c(1:nrow(data))
dataL <- setDT(data)[,list(datelist=seq(StartDate, EndDate, by='1 day'), costs= costs/days_in_month(EndDate)) , by = id]

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

Show Date Ending on the group and Chart Crystal Reports

I have a Crystal Reports that is pulling data by date range and grouping it by WEEK. Also the data is displayed in Chart. Everything works fine. However, I run in into one problem.
When I group by WEEK, it displays the start date in both grouping and chart. I would like to display the END Date. I wrote a formula to take care of this, however, for some reason the dates are off:
totext(cdate(dateadd("d",6,date({CallDate}))),"MM/dd/yyyy")
For example, I do a date range between 11/10/2013 - 11/23/2013. If I run this without a formula it groups like this:
11/10/2013
11/17/2013
I want to display
11/16/2013
11/23/2013
If I run this with the formula above, I get the following output:
11/20/2013
11/27/2013
I'm not even sure how they formula calculates 20 and 26. Any suggestions on how to fix this problem?
This is a little tricky. CR assumes Sunday to be the first day of the week, unless specified otherwise. So, for example to get the first day of the week is Monday (number 2 in the default CR week), this formula will give you the first day of the week:
If DayOfWeek({#Today}) = 2 Then
{#Today}
Else If DayOfWeek({#Today}) = 3 Then
dateadd ("d",-1,{#Today})
Else If DayOfWeek({#Today}) = 4 Then
dateadd ("d",-2,{#Today})
Else If DayOfWeek({#Today}) = 5 Then
dateadd ("d",-3,{#Today})
Else If DayOfWeek({#Today}) = 6 Then
dateadd ("d",-4,{#Today})
Else If DayOfWeek({#Today}) = 7 Then
dateadd ("d",-5,{#Today})
Else If DayOfWeek({#Today}) = 1 Then
dateadd ("d",-6,{#Today})
To get you the last day of the week, which should be Sunday in a regular work week, this formula would give it to you:
If DayOfWeek({#Today}) = 1 Then
dateadd("d", +6, {#Today})
Else If DayOfWeek({#Today}) = 2 Then
dateadd("d", +5, {#Today})
Else If DayOfWeek({#Today}) = 3 Then
dateadd("d", +4, {#Today})
Else If DayOfWeek({#Today}) = 4 Then
dateadd ("d",+3,{#Today})
Else If DayOfWeek({#Today}) = 5 Then
dateadd ("d",+2,{#Today})
Else If DayOfWeek({#Today}) = 6 Then
dateadd ("d",+1,{#Today})
Else If DayOfWeek({#Today}) = 7 Then
{#Today}
So your days are:
Sunday (1)
Monday (2)
Tuesday (3)
Wednesday (4)
Thursday (5)
Friday (6)
Saturday(7)
So in your case, the formula to get the end date should be:
totext(cdate(dateadd("d",+6,date({CallDate}))),"MM/dd/yyyy")
Seems easier to convert the date to a week number value, then add the number of days you need to get to end of the week date.
Modified from Source:
dateadd("ww",datepart("ww",{Command.REPORTDATE})-1,date(year({Command.REPORTDATE}),01,01)-dayofweek(date(year({Command.REPORTDATE}),01,01)))+7
Adjust the +7 at the end of the formula if you want the week to end on a different day.
Proof sample report

Previous month in parameter

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: