Solved

ssrs return first monday of month

Posted on 2014-07-28
8
917 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
 
LVL 14

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 69

Accepted Solution

by:
ScottPletcher 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:ScottPletcher
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
TSQL - Rollup data set 7 29
using & in TSQL 18 21
Contained Database Collations 6 20
Troubleshooting Methodology - steps 3 22
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now