Solved

ssrs return first monday of month

Posted on 2014-07-28
8
1,017 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 66

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 66

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

705 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