Minimum ( MonthToDate )
This will give you the first day of the current month in any report. Not everyone realizes this but you can take the Minimum or Maximum of any one of the 27 date range functions Crystal provides. However, if the date is a database field, a formula or a parameter you need a different syntax. I use:
{Table.Date} - Day ( {Table.Date} ) + 1
The {Table.Date} field can be replaced with any Date value. If your field is a DateTime value you can add the DATE() function around the whole calculation to strip off the time:
DATE ( {Table.Date} - Day ( {Table.Date} ) + 1 )
Finding the last day of a month can be done several ways but I think the one below is the simplest. It can be used with any date value including the CurrentDate function:
DateAdd ('m' , 1 , {Orders.Order Date} - day({Orders.Order Date}) + 1) - 1
You will notice that the first formula is used within this one. I find the first of the target month, then use DateAdd to add one month and find the first of the following month. Finally I subtract 1 day to get the last day of the month. Note that DateAdd always returns a DateTime value. As above, you can use the DATE() function around this formula to strip off the time.
These calculations are especially handy when you need rolling date columns that are all derived from a parameter date. By adding and subtracting more months you can calculate the first and last date of a whole range of months.
No comments:
Post a Comment