Solved

ssrs return first monday of month

Posted on 2014-07-28
8
944 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

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.

Question has a verified solution.

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

A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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