Calculate "LastFriday" date

imfarhan

Board Regular
Joined
Jan 29, 2010
Messages
125
Office Version
  1. 2016
Platform
  1. Windows
Hi
The following function calculate the last "Monday" value in MS Access (SQL) but I would like to calculate the "last Friday" of the week and could some one go through how this fucntion work (break down) please. I need to get the understanding so in future I can do my self instead of copy/paste
Many thanks

#Format(DateAdd("d", -(Weekday(Now()) - 2 +7), Now()), "dd/mm/yyyy"),#

If I break the above function to get more understaning
Weekday(Now()) = 2
Don't understand -->= so the answer is =
(Weekday(Now())-2+7) = 2-2+7 ? = 7 and further
-(Weekday(Now())-2+7) = 2-2+7 ? = 7 = -7
DateAdd("d", -7, ??) = ?? what value will come on that?

Don't understand , please could some one help me on that.

Many thanks

Regards
Farhan
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
The following query pulls the "Last Friday", even if the current day is firday as will which I wanted in my query.

SELECT <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
#7/9/2010# AS TestDate, <o:p></o:p>
Format(DateAdd("d",IIf(Weekday([TestDate])<7,-(Weekday([TestDate])+1),6-Weekday([TestDate])),[TestDate]),"dd/mm/yyyy") AS LastFriday<o:p></o:p>
FROM data;<o:p></o:p>
<o:p> </o:p>
<o:p>If you don't want to see Last Friday if the Testday=Friday change 7 to 6</o:p>
<o:p> </o:p>
<o:p>Regards,</o:p>
<o:p>Farhan</o:p>
 
Upvote 0
Hi imfarhan,

if you are working with SQL bypass query you will be able to use this statement, this will return last friday date (change the 4 to different number to return different day). ensure you declare the variable before the select cause

declare @DATE datetime
set @DATE = DATEADD(wk, DATEDIFF(wk,0,GETDATE()-7), 4)
where [your time field] = @DATE
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top