Solved

Need date code to pull "last month" month to date

Posted on 2014-04-23
5
688 Views
Last Modified: 2014-04-25
to query month to date data I  am using  

Between DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1

Today being 4/23 it gives me everything between 4/1 and 4/22. Now I need a piece of code that gives me last month month to date if that makes sense, It would need to pull 3/1 to 3/22. As the month progresses it would then of course add the previous day to the queried results like the

Between DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1

does for the current month. I am using this for creating compairable time frame sales data. Thanks
0
Comment
Question by:Cole100
5 Comments
 
LVL 10

Expert Comment

by:Korbus
ID: 40018573
>> DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1

You are so close! (I'm putting parts of your original statement in bold)


DateAdd("m",-1,DateAdd("d",-DatePart("d",Date())+1,Date())) And DateAdd("m",-1,Date()-1)

(P.S. sorry I cannot test this for syntax errors right now)
0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 25 total points
ID: 40018623
If you need this as two columns within a single query, you might use something like:

SELECT
SUM(iif([DateField] >= Dateserial(Year(date()), Month(Date()), 1) AND
              [DateField] < Date(), [Amount], 0)) as ThisMonthToDate,
SUM(iif([DateField] >= Dateserial(Year(date()), Month(Date())-1, 1) AND
              [DateField] < DateSerial(Year(Date()), Month(Date()) - 1, Day(Date()), [Amount], 0)) as LastMonthToDate
From yourTable
WHERE yourTable.[DateField] >= DateSerial(Year(Date()), Month(Date()) - 1, 1)

This would give you the sum of the [Amount] field in YourTable for the two time periods.
You could add columns and group by those columns if you needed, for example the sales by product over the same period in successive periods.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 25 total points
ID: 40018627
Don't mess with the days:

Between DateSerial(year(Date()), Month(Date()) - 1, 1) And DateAdd("m", -1, Date())

/gustav
0
 

Author Closing Comment

by:Cole100
ID: 40021415
Gustav, yours worked exactly how I was asking for it. Thank you

Fyed, that does what I needed and more. Thank you
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40021987
You are welcome!

/gustav
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now