Solved

ssrs return first monday of month

Posted on 2014-07-28
8
987 Views
Last Modified: 2014-07-29
Hi I am wanting to set a parameter in SSRS, Report Builder 3.0  that returns the first Monday of April in the current year.  Could anyone help me with this expression please, I cannot seem to get my head around it.
0
Comment
Question by:deborahhowson00
[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
  • 2
  • 2
  • 2
  • +2
8 Comments
 

Author Comment

by:deborahhowson00
ID: 40223845
I want to return the date of that first Monday of April in the current year, sorry, just to be clear.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40223953
I have an article out there called How to Build Your Own SQL Calendar Table that handles situations like this.

Otherwise, you'll probably need to build some kind of loop to start at CAST(CAST(YEAR(GETDATE) as char(4)) + '-04-01' as date), then incriment one day at a time until SELECT DATEPART(dw, {the date} ) = 1 for Monday.

So ... Create a SP that contains the below code, attach it as a data set, and in your parameter set all values to this:
Declare @dt datetime
SET @dt = CAST(CAST(YEAR(GETDATE()) as char(4)) + '-04-01' as date) 

WHILE (DATEPART(dw, @dt) <> 2)
	begin
	SET @dt = @dt + 1
	end

-- Return set
SELECT @dt

Open in new window

0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 40224091
Try this:
=DateAdd("d", 1 - WeekDay (Cstr(Year(Today)) + "/04/07", 2), Cstr(Year(Today)) + "/04/07")

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40224123
Hi,

You can try this one

=DATEADD("ww",DATEDIFF("ww", DATEADD("d",0,"1900-01-01"), DATEADD("d", 6 - DATEPART("d", Parameters!DTS.Value), Parameters!DTS.Value)),"1900-01-01")

Here you can use the Parameter DTS value as per the date for which month you want first monday
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40224770
You can use a simple date calculation to get the first Monday (or given day) of a month -- no looping and definitely no I/O needed.

I think you can use a SELECT to provide a value for SSRS.

Change the "MONTH, 3" to get a different month.  For example, to get first Monday of Jan, use "MONTH, 0", for Jun, "MONTH, 5", etc..


SELECT DATEADD(DAY, -DATEDIFF(DAY, 0, month_day_7) % 7, month_day_7)
FROM (
    SELECT DATEADD(DAY, 6, DATEADD(MONTH, 3, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))) AS month_day_7
) AS month_day_7
0
 

Author Closing Comment

by:deborahhowson00
ID: 40226077
Cheers, worked a treat!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40226459
deborahhowson00

Did you try the first solutions provided you?  If yes, please comment on whether they worked.  

Not to take anything away from Scott's answer, which I'm kind of liking btw, but it is not good EE etiquette to ignor multiple earlier comments.

Just curious.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40228028
Perhaps.  But I think the overall quality of the answer should matter more than going strictly by the order in which answers were posted.

FWIW, I went back and checked both the "=DATEADD" codes -- neither was fully accurate.

As to the looping code, I would never consider looping if a simple SELECT could do the task flexibly and efficiently.  And it's not accurate unless the DATEFIRST setting is a specific value.

The SELECT code works regardless of any date setting.  And it can easily be completely generalized to work for any year, month and weekday.  For example, for the first Friday in June of the prior year:


DECLARE @day_of_week int --0=Mon,1=Tue,...,6=Sun.
DECLARE @month int --1=Jan,2=Feb,...,12=Dec.
DECLARE @year_adjustment int --0=current yr; -1=prior yr; 1=next yr; etc.
SET @day_of_week = 4
SET @month = 6
SET @year_adjustment = -1

SELECT DATEADD(DAY, -DATEDIFF(DAY, @day_of_week, month_day_7) % 7, month_day_7)
 FROM (
     SELECT DATEADD(DAY, 6, DATEADD(MONTH, (@month - 1), DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + @year_adjustment, 0))) AS month_day_7
 ) AS month_day_7
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Present Absent from working date rage 11 48
PROPERCASE SCRIPT IN SQL 3 21
Parse this column 6 34
T-SQL: Need Group By to use "fuzzy logic"?? 3 14
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi, I have heard from my friends that it’s not possible to create Label Printing report using SSRS. I am amazed after hearing this words not possible in SSRS. I googled lot and found that it is possible to some of people know about the Report Bui…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

735 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