Filtering for dates less than or or equal to 9 months in the future in an Access query - date

Once a week I need to run a report where I query an Access database for any product that will expire in 9 months or less. The way they want it calculated is to take the date 9 months into the future and return anything that expires at the end of that month or sooner. If it were simply 270 days or less, I'd have no problem. (I'd also have no problem if I could do it in Excel, but that's not an option for now).
I came up with a solution that works every month of the year, unless it happens to be March (more specifically between March 6th and April 5th).
< DateValue(Month(Date()+270)+1 & "/1/" & Year(Date()+270))
So basically I'm:
adding 270 days to today's date
extracting the resulting month
adding 1 to the month
putting it back together as a text string so I can use < the 1st of the following month
for the year, I'm using the year from the date +270 days so I don't end up using the current year by accident
The trouble is that for the date range above (which I unhappily discovered today), I land in December when I add 270 days, so the following month is in a different year. As a result, my report only produced items that already expired.
In other words, on March 5th, I would have needed a list of everything expiring prior to December 1, but on March 6th, I need everything before January 1 of the next year.
Is there a more effective way to do this that avoids this issue? I thought of using

You may have had DateDiff in mind, and it can be used:
Where DateDiff("m", Date(), [YourDateField]) Between 0 And 9
However, that will ignore an index you might have on [YourDateField].
This, however, will include products that expired previously in the current month.
The alternative is DateSerial as Hans showed but he forgot that in SQL Date() must be used and that only those products that will expire should be listed:
Where [YourDateField] Between Date() And DateSerial(Year(Date()), Month(Date()) + 10, 0)

Use the DateSerial Function to compute the future date you need.
Here is a demonstration in the Access Immediate window which computes the date 9 months from today:
? Date
3/6/2015
? DateSerial(Year(Date), Month(Date) + 9, Day(Date))
12/6/2015
However, as I understand your requirement, you actually want dates from that entire month. In that case you can compute the first of the month which is 10 months from today and ask for everything less than that date.
? DateSerial(Year(Date), Month(Date) + 10, 1)
1/1/2016
You can include that expression in your query like this ...
WHERE expire_date < DateSerial(Year(Date()), Month(Date()) + 10, 1)

Related

Set Tableau dates show show previous month going back 12 months

I need to modify some Tableau dashboards so that they show from the previous month going back 12 months.
For example, it is August 2017 now, so the company wants to see the 12 months ending at July 2017. In other words: Aug 2016, Sept 2016, Oct 2016...June 2017, July 2017.
Using Tableau's relative date filter, users can easily select "previous month" (July 2017) or "previous 12 months" (Sept 2016-Aug 2017) on the relative date filter. But they can't choose previous month going back 12 months (Aug 2016-July 2017).
I was thinking of writing a formula that sets current month as 0, previous month as 1, month before that as 2 etc and that way I can filter to only show months 1, 2, ...12. And not show month 0, the current month.
Is this the best approach? I thought Tableau would have an option for this - surely a lot of companies want to see a year worth of data ending at the previous month? (Since the current month is not yet complete so they don't want to see a partial month's data).
Thanks for your ideas!
You are right about this being a common requirement. I do 'Last 12 months' filter too which excludes current month since it is not a full month.
In my case I have defined a True/False type calculated field which I use to filter records which show in the dashboards.
Here is the formula that I use:
DATEDIFF('month',[My Timestamp Field],Today()) < 13 and DATEDIFF('month',[My Timestamp Field],Today()) > 0

Converting month to date format in access

I have an access column which has values like May, May-June, November, January-February etc.
Requirement is that
1) If only one month is there, I should create a transformation with a date column pointing to the last date of that month and the year would be the current year.
Eg May would be 5/30/2015, January would be 1/31/2015
2) If it is having two months, then second month must be taken and the same logic as in Point 1 should be implemented.
Eg May-June would be 6/31/2015, January-February would be 2/28/2015.
My first preference would be without using VBA code.
Please help.
You can use a one-liner in a function:
Public Function GetDateFromMonth(ByVal Months As String) As Date
Dim Ultimo As Date
Ultimo = DateAdd("d", -1, DateAdd("m", 1 + UBound(Split(Months, "-")), CDate(Split(Months, "-")(0) & "/1")))
GetDateFromMonth = Ultimo
End Function
Split the string on the "-", use the second month, add one to that month to get the next month, then take the first day of that month and subtract one from it. For example Jan-Feb : build 3/1/2015 and then subtract 1 day to get the last day of Feb.
Make a table that holds a list of Months:
MonthName MonthNumber
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
September 9
October 10
November 11
December 12
Create this query:
SELECT InputData.Months, DateSerial(Year(Date()),[MonthNumber]+1,0) AS ReqdDate
FROM InputData, Months
WHERE (((IIf(InStr([Months],'-')=0,[Months],Mid([Months],InStr([Months],'-')+1)))=[MonthName]));
That's it. Output looks like this:
Months ReqdDate
May 31/05/2015
May-June 30/06/2015
November 30/11/2015
January-February 28/02/2015
March 31/03/2015
April-September 30/09/2015
Sorry about formatting - noob. I'm in Eu so my dates look odd to you, but you get the idea; you can make them US dates in a moment I'm sure. You can use the bits of this you need in your code in two secs.
If you don't like the lookup table for month number, I'm sure you can use some format function to turn the month name into a month number.

What Date Time Format is this?

What date time format is this : 735715:37344280
<ExecDateTOD Friendly="Monday April 27, 2015 10:23:00am">735715:37344280</ExecDateTOD>
It's found in C:\Windows\Performance\WinSAT\DataStore\file_name.xml, and is the date time when the Windows Experience Index Assessment test was run.
Any idea how it's structured and can be edited? I need to change it to a previous years Date.
It seems that this format is called VariantTime, in MSDN the call to convert time is called VariantTimeToSystemTime. So it may be number of days, with decimal part after the :.
For the timestamp 735715:37344280
The first number (the one before the colon) is the number of days since the year 0:
735715 / 365 = 2015.66
The second number (the one after the colon) is the number of milliseconds that have passed within the current day.
37344280 / (1000*60*60) = 10.37 hours since start of day
So you can just subtract 365 days from the first number to obtain the previous year like this:
<ExecDateTOD Friendly="Monday April 27, 2014 10:23:00am">735350:37344280</ExecDateTOD>
Note that there were no leap years in either 2015 or 2014, so these year are exactly 365 days long.
Here is a link to a page with another <ExecDateTOD> tag where you can compare: http://www.scribd.com/doc/82935159/2012-01-30-16-00-49-986-Formal-assessment-Recent-WinSAT#scribd
I think that if you subtract 365 from that number, you'll be in previous year.
That number seems to be days since the year 0. The first part might be the number of days, taking into account leap years, etc). The second part the time coded in some way.

how would I query 4/1/ of the 72nd birth date year > an input date

I have tried the following:
add_months(to_Date('04/01/ind.birth_dte','MM/DD/YYYY'), 864) >= to_date('&StartDt','MM/DD/YYYY')
Is there a better way to pull April first of the participant's 72nd birth date?
You could use an interval calculation instead, but not sure how you're defining 'better'. Assuming you do want April 1st of the year in which their 72 birthday falls:
trunc(ind.birth_dte, 'YYYY') + interval '72-3' year to month
The trunc() function goes to the first day of their birth year, and the interval adds 72 years and 3 months to that, which will be April 1st.
SQL Fiddle with some sample dates, including a leap day to show that isn't a problem.
Or to compare that adjusted date with a fixed date as a filter:
where trunc(ind.birth_dte, 'YYYY') + interval '72-3' year to month
> to_date('&StartDt','MM/DD/YYYY');
SQL Fiddle.
You can use the trunc() method with your version as well to save building up a string and calling to_date, adding an additional three months to the add_months call (though I'd suggest you at least need a comment indicating where '867' comes from):
where add_months(trunc(ind.birth_dte, 'YYYY'), 867)
> to_date('&StartDt','MM/DD/YYYY');

Crystal report formula syntax

I have a report I am trying to modify in Crystal. It has a data field that has a formula in it, but I want to use another formula.
This is an example of what I am trying to do.
[((# Days in January) – 15) x (Market Rent/(# Days in January))]
+ [((# Days in February) – 0) x (Market Rent/(# Days in February))]
+ [14 x (Market Rent/(# Days in March))]
I have ADO commands built out for the market rent, and a start date and end date. The months in my example are just that an example. I am not sure how to take my ADO command dates that are entered in on a filter page, and put them in a formula like the one above. Any ideas?
Also, in the first and last para. the -15 and the 14 are for a date in the middle of the month. So if the start date was on the 15th of Jan, and the End date was the 15th of march. This formula would calculate my loss of rent during vacancy.
If I'm reading your question correctly, you want to take a date field and find out how to measure the number of days in that month, the month before it, and the month after it. Here's some Crystal formulas to help you out. Let's assume your date field is called {#DateFld}:
To find the number of days in a particular month relative to a particular date, try this:
local datevar X:=cdate(dateadd("m",0,{#DateFld}));
datediff(
"d",
date(year(X),month(X),1),
date(year(X),month(X)+1,1)
)
I recommend you copy & paste this in 3 different formulas:
- In the 1st formula, replace the "0" with a -1 to get the number of days in the previous month.
- In the 2nd formula, don't change anything. That'll get you the number of days in the current month (i.e. the month that {#DateFld} is in)
- In the 3rd formula, replace the "0" with a +1 to get the number of days in the next month.
For example, if {#DateFld} is March 10th, 2011, the 1st formula will give you 28, the 2nd will give you 31, and the 3rd will give you 30.