Solved QUERY - Data only from the last working week (1 Viewer)

georg0307

Registered User.
Local time
Today, 15:00
Joined
Sep 11, 2014
Messages
91
Dear All,

in a query how do I set data only from the last working week? i.e. Monday to Friday without putting dates.

I have this SQL but it doesn't work:
SELECT [2021].Date, [2021].ECO_LABEL, DatePart(weekday, [Date]) Between 2 And 6 AS Weekdays
FROM 2021;

Thanks in advance

best regards
Georg
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:00
Joined
May 7, 2009
Messages
19,245
you will need a function to return the last day of work done.
then move back to get the monday:
Code:
'arnelgp
Public Function fnStart() As Date
Dim d As Date
d = DMax("Date", "2021")
fnStart = FirstDayInWeek(d, vbMonday)
End Function

' https://stackoverflow.com/questions/30026572/how-to-find-the-start-date-of-week-from-a-given-date
Public Function FirstDayInWeek(ByVal dtmDate As Date, Optional FirstDay As VbDayOfWeek = 1)
FirstDayInWeek = dtmDate - Weekday(dtmDate, FirstDay) + 1
End Function


SELECT [2021].Date, [2021].ECO_LABEL
FROM 2021
WHERE [Date] Between fnStart() And fnStart()+5
 

georg0307

Registered User.
Local time
Today, 15:00
Joined
Sep 11, 2014
Messages
91
Ciao,
Fast & Furious (INCREDIBLE!), I will try.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:00
Joined
Feb 19, 2013
Messages
16,614
Add this where clause to your sql

WHERE [Date]=> dateadd("ww",-1,date())-weekday(date(),2)+1 and [date]<=dateadd("ww",-1,date()-weekday(date(),2)+7

or

WHERE [Date] between dateadd("ww",-1,date())-weekday(date(),2)+1 and dateadd("ww",-1,date()-weekday(date(),2)+7

note that date is a reserved word (it means today) and should not be used as a field name - similarly it is not a good idea to name tables with a number
 
Last edited:

ebs17

Well-known member
Local time
Today, 15:00
Joined
Feb 7, 2020
Messages
1,946
@CJ_London: Jet cannot resolve the name of the VBA constant vbMonday, you would have to enter the value 2 directly.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:00
Joined
Feb 19, 2013
Messages
16,614
oops :) tested in in the immediate window - code updated
 

georg0307

Registered User.
Local time
Today, 15:00
Joined
Sep 11, 2014
Messages
91
I receive back a message in Italian that the function is not defined what am i doing wrong?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:00
Joined
Feb 19, 2013
Messages
16,614
you have a couple of solutions - which one are you referring to?
 

ebs17

Well-known member
Local time
Today, 15:00
Joined
Feb 7, 2020
Messages
1,946
that the function is not defined what am i doing wrong?
You'll probably also need to store the function in a standard module in your application to be able to use it. Just looking at the forum is not enough.
 

georg0307

Registered User.
Local time
Today, 15:00
Joined
Sep 11, 2014
Messages
91
Add this where clause to your sql

WHERE [Date]=> dateadd("ww",-1,date())-weekday(date(),2)+1 and [date]<=dateadd("ww",-1,date()-weekday(date(),2)+7

or

WHERE [Date] between dateadd("ww",-1,date())-weekday(date(),2)+1 and dateadd("ww",-1,date()-weekday(date(),2)+7

note that date is a reserved word (it means today) and should not be used as a field name - similarly it is not a good idea to name tables with a number
1674220964177.png
I receive this error
 

georg0307

Registered User.
Local time
Today, 15:00
Joined
Sep 11, 2014
Messages
91
you will need a function to return the last day of work done.
then move back to get the monday:
Code:
'arnelgp
Public Function fnStart() As Date
Dim d As Date
d = DMax("Date", "2021")
fnStart = FirstDayInWeek(d, vbMonday)
End Function

' https://stackoverflow.com/questions/30026572/how-to-find-the-start-date-of-week-from-a-given-date
Public Function FirstDayInWeek(ByVal dtmDate As Date, Optional FirstDay As VbDayOfWeek = 1)
FirstDayInWeek = dtmDate - Weekday(dtmDate, FirstDay) + 1
End Function


SELECT [2021].Date, [2021].ECO_LABEL
FROM 2021
WHERE [Date] Between fnStart() And fnStart()+5
I receive back this error
1674221050805.png
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:00
Joined
Feb 19, 2013
Messages
16,614
looks like I missed a bracket in the second part - this is the revised code
Between DateAdd("ww",-1,date())-Weekday(Date(),2)+1 And DateAdd("ww",-1,Date())-Weekday(Date(),2)+7
 

georg0307

Registered User.
Local time
Today, 15:00
Joined
Sep 11, 2014
Messages
91
Hi,
I used this: Between DateAdd("ww",-1,Date())-Weekday(Date(),2)+1 And DateAdd("ww",-1,Date())-Weekday(Date(),2)+7))

Thanks to All, it works!!!
 

Users who are viewing this thread

Top Bottom