Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: date range and weekends Post Reply Post New Topic
Author Message
stevbren
Newbie
Newbie
Avatar

Joined: 20 Nov 2007
Online Status: Offline
Posts: 33
Quote stevbren Replybullet Topic: date range and weekends
     Posted: 03 Dec 2007 at 12:03pm
This is probably simple, but I have a report that I am looking to flag all late products. A product is late if delivered more than 48 hours after receipt. Weekends do not count, so if a product is ordered friday and ships monday now it is flagged as late.
Can anyone help me please.
Thanks
IP IP Logged
Lugh
Senior Member
Senior Member
Avatar

Joined: 14 Nov 2007
Online Status: Offline
Posts: 377
Quote Lugh Replybullet Posted: 04 Dec 2007 at 10:34am
Is the number of hours important, or can you say "2 weekdays"?

The DateAdd function allows you to add (or subtract) weekdays, which ignore weekends.  Unfortunately, the DateDiff function does not do the same.  But, a little manipulation of the formula should give you what you need.  Something like:

If DateAdd("w",-2,{MyShipDate}) < {MyOrderDate} Then
flag it late
Else
don't flag it late




Edited by Lugh - 04 Dec 2007 at 10:35am
IP IP Logged
stevbren
Newbie
Newbie
Avatar

Joined: 20 Nov 2007
Online Status: Offline
Posts: 33
Quote stevbren Replybullet Posted: 08 Dec 2007 at 11:25am
Lugh,
Thank you for leading me in the right direction. Here is what I wrote and can't make it work better...
 
if dateadd("w", -2,{@Proof Out})<{Job.Date-Entered} then "LATE"
Else ""
 
It works on only anything just -2 days. I don't need hours, just days. So anything -1 day shows late, stuff that is 2 months past job entered date does not show late.
 
Can you or anyone help me get over this "hump".
 
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 08 Dec 2007 at 2:06pm
This is interesting. The "w" option of DateAdd() doesn't work on my computer. It includes weekends as well. I wonder if this has always been a bug or is new? Anyway, I re-worked the problem using the DayOfWeek() function to determine if a weekend is within the range. I'm assuming that no products get ordered on a Sat or Sun? If so, this should work for you.
if {@Proof Out} > iif (DayOfWeek({Job.Date-Entered},2) >3, DateAdd("d", 4, {Job.Date-Entered}), DateAdd("d", 2, {Job.Date-Entered})) Then "Late"

If you need a great reference guide for all the date formulas in Crystal Reports, check out Chapter 6 of my book Crystal Reports Encyclopedia

Edited by BrianBischof - 08 Dec 2007 at 2:07pm
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
stevbren
Newbie
Newbie
Avatar

Joined: 20 Nov 2007
Online Status: Offline
Posts: 33
Quote stevbren Replybullet Posted: 09 Dec 2007 at 7:24am
Brian,
Thanks for checking this out. I do not understand how the formula should look using your dayofweek () function. No products get ordered on sat. or sun. I am just not getting how to take those days out of the formula. Can you either show me how, or tell me what page(s) to look at in your book?
 
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 09 Dec 2007 at 11:57am
Yeah, it's a bunch of code all in one big chunk.
I personally like to write very consise code, but this does make it tough to see all the details.
I'll rewrite it here so that you can see the individual steps.
NumberVar DayEntered;
DateTimevar LastValidDate;

//Get which day of the week the job was entered.
//This formula returns 1 for Monday and 7 for Sunday
DayEntered := DayOfWeek({Job.Date-Entered},2);

//If this is Mon-Wed, then just add two days
if DayEntered <= 3 Then
   LastValidDate := DateAdd("d", 2, {Job.Date-Entered});

//If this is a Thursday or Friday, then we need
// to add two extra days to bump it over the weekend.
if DayEntered >= 4 Then
   LastValidDate := DateAdd("d", 4, {Job.Date-Entered});

//Now that we know what the last valid date is,
//see if the proof date comes after it.
if {@Proof Out} > LastValidDate Then "Late"

Once you understand this code, you can look at the original formula and see if it makes more sense now.
I cover all the Crystal Report date related functions on pages 255-261 in my book Crystal Reports Encyclopedia
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
stevbren
Newbie
Newbie
Avatar

Joined: 20 Nov 2007
Online Status: Offline
Posts: 33
Quote stevbren Replybullet Posted: 12 Dec 2007 at 3:42am
You are "The Man".
Now that I see the formula in writing it makes sense...and it works.
Thank you so much.
Steve
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum