Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 805
  • Last Modified:

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

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
Cole100
Asked:
Cole100
2 Solutions
 
KorbusCommented:
>> 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
 
Dale FyeCommented:
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
 
Gustav BrockCIOCommented:
Don't mess with the days:

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

/gustav
0
 
Cole100Author Commented:
Gustav, yours worked exactly how I was asking for it. Thank you

Fyed, that does what I needed and more. Thank you
0
 
Gustav BrockCIOCommented:
You are welcome!

/gustav
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now