Solved

Crystal reports last 7 days exclude weekends

Posted on 2014-11-05
13
1,002 Views
Last Modified: 2014-11-06
Hello,

I need to write a formula for getting dynamic last 5 days excluding weekends. For example

Day 1 = currentdate (11/5/2014)
Day 2 = currentdate -1 (11/4/2014)
Day 3 = Currentdate - 2 (11/3/2014)
Day 4 = currentdate -3 (10/31/2014) Since this is a weekend I need the last Friday's date
Day 5 = currentdate -4 (10/30/2014)

How can I do that? please assist.
Thank you
0
Comment
Question by:angel7170
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 35

Expert Comment

by:johnsone
ID: 40424286
This should do it:

SELECT wdate 
FROM   (SELECT wdate 
        FROM   (SELECT Trunc(SYSDATE) - LEVEL + 1 wdate 
                FROM   dual 
                CONNECT BY LEVEL < 14) 
        WHERE  Trim(To_char(wdate, 'day')) NOT IN ( 'saturday', 'sunday' ) 
        ORDER  BY wdate DESC) 
WHERE  ROWNUM <= 5; 

Open in new window


I'm sure there is a fancier way to do it, but this works.  I used 14 as an arbitrary number.  Ideally the most number of days you should have to go back is 9 or 10, but the extra 4 doesn't hurt the query.
0
 
LVL 18

Expert Comment

by:vasto
ID: 40424311
You can get the last 7 days and exclude days 1 and 7 (Sunday and Saturday)
The formula might be something like


<<your date field>> IN currentdate-7 to currentdate and not (DayOfWeek<<your date field>>)  in [1, 7])

If you need to get the last full week you can use LastFullWeek function
0
 
LVL 35

Expert Comment

by:johnsone
ID: 40424424
I would discourage you from using day of week 1 and 7.  The day of week is determined by NLS settings and may not necessarily be 1 and 7.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:angel7170
ID: 40424447
Thank you!

sorry, I was looking for a formula to get last 7 days. These formulas will be used as a header in Crystal reports.

For example

Formula 1 : This should be currentdate
Formula 2 : This should be currentdate - 1 but if it is a weekend subtract days based Saturday or Sunday
Formula 3 : This should be currentdate - 2 but if it is a weekend subtract days based Saturday or Sunday
.........etc

I apologize for not explaining clearly.
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 40424539
What do you want the formula to display?

mlmcc
0
 

Author Comment

by:angel7170
ID: 40424547
display the date
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 40424561
I reread your previous comment.  You have 5 formulas and want to display 5 dates.

Will the report ever be run on a Saturday or Sunday?

mlmcc
0
 
LVL 32

Expert Comment

by:awking00
ID: 40424574
Will you ever be running the query on a Saturday or Sunday?
0
 

Author Comment

by:angel7170
ID: 40424692
The report will not be run on weekends only during the weekdays.

Report format is attached in the spreadsheet
0
 

Author Comment

by:angel7170
ID: 40424696
Sample format
Book1.xlsx
0
 
LVL 35

Expert Comment

by:johnsone
ID: 40424813
I'm confused as well.  The query that I posted will give you the dates of the last 5 non-weekend days.  That seems to be what you are asking for.
0
 
LVL 101

Accepted Solution

by:
mlmcc earned 500 total points
ID: 40425115
He wants Crystal formulas to get them.

Formula 1 - Today
CurrentDate

Open in new window


Formula 2 - 1DayAgo
If DayOfWeek (CurrentDate) = crMonday then
    CurrentDate - 3
Else
    CurrentDate -1

Open in new window


Formula 3 - 2DaysAgo
If DayOfWeek (CurrentDate) <= crTuesday then
    CurrentDate - 4
Else
    CurrentDate - 2

Open in new window


Formula 4 - 3DaysAgo
If DayOfWeek (CurrentDate) <= crWednesday then
    CurrentDate - 5
Else
    CurrentDate - 3

Open in new window


Formula 5 - 4DaysAgo
If DayOfWeek (CurrentDate) <= crThursday then
    CurrentDate - 6
Else
    CurrentDate - 4

Open in new window


mlmcc
0
 

Author Closing Comment

by:angel7170
ID: 40427084
Thank you. This is exactly what I needed.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

627 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