on 08-11-2016 5:20 PM
I am trying to write a formula that uses a date parameter selected by the user to calculate a Last Month's MTD total. I am having trouble setting up the formula to handle when the dates are not in the same day range. For example, the parameter is 07/31/2016, but there is only 30 days in June (last month).
Parameter Date: 07/31/2016 ({?EndDate})
If DATE({date field}) >= DATE(YEAR({?EndDate}), (MONTH({?EndDate})-1,1) and
DATE ({date field}) <= DATE(YEAR({?EndDate}), MONTH({?EndDate}),1)-1) then
Result I want:
Data from range 06/01/2016 to 06/30/2016
The above formula will not work when the Parameter date is today's date. How can I change the formula to make it work for all instances? Thank you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This should hopefully work:
If Date({date fied}) IN Date(DateAdd('m', - 1, {?EndDate}- Day({?EndDate})) + 1) TO
date(DateAdd('m',-1,{?End Date})) then
If it throws a 'data time required here' error and highlights the last line of the code, just remove the 'date' function:
If Date({date fied}) IN Date(DateAdd('m', - 1, {?EndDate}- Day({?EndDate})) + 1) TO
(DateAdd('m',-1,{?End Date})) then
-Abhilash
Abhilash, this is working except when my parameter date is 07/31/2016, it is pulling last month's data between 05/31/2016 and 06/30/2016. I think I fixed this by changing the 1st part of the line to:
If Date({date fied}) IN Date(YEAR({?End Date}), MONTH({?End Date})-1,1) TO (DateAdd('m',-1,{?End Date})) then...
Thank you EVERYONE for all your help.
You will have an issue when the user selects a date in January with that code.
I actually looked at my formula again, and I see that I messed up a bracket.
Here's the correct one that should work fine:
If Date({date fied}) IN Date(DateAdd('m',-1,{?End Date}- Day({?End Date}) + 1)) TO
Date(DateAdd('m',-1,{?End Date})) then
-Abhilash
Your formula also doesn't account for different years if, for example, the value of {?EndDate} is 1/31/2016. So, I would try something like this:
Datevar endDt := {?EndDate}) - Day({?EndDate}); <--Gives the end day of the previous month
DateVar startDt := Date(Year(endDt), Month(endDt), 1); <--Gives the first day of the previous month
If Date({Date Field}) >= startDt and Date({Date Field}) <= endDt then...
-Dell
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Which version of Crystal are you using? (Go to Help>>About and get the full version number.) Can you save the report with data without using the formula we're discussing (leave the "bad" version of the formula in the formula list...) and attach it here? To attach it, you'll have to go to the "Advanced" editor and you'll have to add ".txt" on the end of the file name (it won't allow you to upload a file with a .rpt extension)
-Dell
Yes. Please get my email address from my website: http://www.dellstinnett.com
-Dell
Hi,
Try:
If Date({date fied}) IN Date(DateAdd('m', - 1, {?EndDate}- Day({?EndDate})) + 1) TO
Date({?EndDate}- Day({?EndDate}))) then
-Abhilash
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.