SSRS Subscription for Previous Month - reporting-services

I have SSRS report that has a month parameter. When the report runs the month parameter should default to current month (the user wants to see current month data when running from report manager)
I want to schedule this report to be delivered via email so it runs at the beginning of every month but should select previous month (meaning ignoring the default month parameter of current month).
How can I approach this problem?

Create a data driven subscription. Use the below query to get the start date and end date. (Add other parameter columns that you might need to the query).
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) as [Start Date], DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) as [End Date]
Hope it helps. Please mark it as an answer, if it is.

Related

SSRS - Set param to be the time right now but on a different date

I was asked to create an SSRS Report, but taking the live sales of today and compare it to sales the exact same time as a date to be named later. For example, "How are we doing compared to this time on last Saturday?" The user would simply pick the date, and the query would calculate sales for both Today() and #BusinessDate, from the start of the business until Today()'s time.
I am sure I can do the query, but I don't know how to set a query parameter for this.
Please note, Today() and Now() throw errors, but GETDATE() works fine.
Got it:
SELECT CONVERT(DATETIME, CONVERT(CHAR(8), #BusinessDate, 112) + ' ' + CONVERT(CHAR(8), GETDATE(), 108))

SSRS Report subscription date parameters

The thing that I want to do is as follows
I have a SQL reporting services report that takes date parameter (I have two parameters as Run_Report_From_Date_PARM and Run_Report_To_Date_PARM. These dates are picked from the Datepicker). I want to create a subscription to the report that will run on 1st of every month and show the data for the prior month.
While making subscription if i choose 2016-08-01 for Run_Report_From_Date_PARM and 2016-09-01 for Run_Report_To_Date_PARM then the report is generating always with that date range only. But i need the date ranges modify automatically after every month(like for the next month From date should be 2016-09-01 and to date should be 2016-10-01 ).
What is simplest is to just use expressions to set the default values for these parameters.
For a start date of the first of last month:
=DateAdd("m", -1, DateAdd("d", 1 + -1 * DatePart("d", Today()), Today()))
For an end date of the last of last month:
=DateAdd("d", -1 * DatePart("d", Today()), Today())
However, if you don't want these to be the default values when executing the report, you can set up a hidden boolean parameter that you only set to true in the subscription settings. That can be useful if you need to include formatting or default value tweaks in the subscription version of a report.

Get previous month and year using a user-defined function in SQL

I need to write a function which will return me the previous month and year.
Like if the input is : function_name('February', 2015) the output will be 'January', 2014.
In SQL Server 2008 there is the date data type, which has no time attached. You can thus remove the time portion quite easily simply by converting, then performing the DateAdd.
SELECT DateAdd(month, -1, Convert(date, GetDate()));
This will return a date data type. To force it to be datetime again, you can simply add one more Convert:
SELECT Convert(datetime, DateAdd(month, -1, Convert(date, GetDate())));
You may not need the explicit conversion to datetime, though.

SSRS daily subscription for monthly report

I have a SSRS report which is executed and delivered daily to emails. The report calculates some numbers for the 1st date of the month to the (d-1)th date (d being the day when it is executed). So the default parameter values in this report are :
Start_date :=DateSerial(Year(Date.Now), Month(Date.Now), 1)
End_date :=DateAdd("d",-1,Today())
The problem is on 1st of every new month , the start date evaluates to 1st of new month and the end date becomes the last date of previous month. This makes the report non-sensical on 1st of every month.
What should we do to avoid this and force the expression to evaluate 1st of the new month as start and end date ?
For the End Date, perhaps check if it's the 1st of the month and then default to the current date (i.e. the 1st) if it is:
=Iif(
Day(DateTime.Today) = 1,
DateTime.Today,
DateAdd("d",-1,DateTime.Today)
)
But I'm assuming there is a reason your are using today-1 as the end date, so this could mean your report shows nothing for the 1st day of the month...

SSRS Expression - Find First Day of Week From Week value

I've got an SSRS report that returns unique login count to our VDI Pools broken down by week.
For example for January it would read:
Week # of Logins
2015_JANUARY_WEEK_NO_1 3
2015_JANUARY_WEEK_NO_2 49
I'm using the Week column to link to another report that'll display the selected week's logins so I have to get the first day of the selected week and the last day of the select week and pass them to the other report as parameters.
I know how to get pull the year and week # from the Week column.
=Mid(Fields!Week.Value,1,4)
gives me the year and
=Trim(Mid(Fields!Week.Value,Len(Fields!Week.Value),Len(Fields!Week.Value)))
gives me the week.
I found this T-SQL that works:
DECLARE #WeekNum INT
, #YearNum char(4);
SELECT #WeekNum = 2
, #YearNum = 2015
-- once you have the #WeekNum and #YearNum set, the following calculates the date range.
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + #YearNum) + (#WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + #YearNum) + (#WeekNum-1), 5) AS EndOfWeek;
but I cannot figure out how to turn that into an expression that doesn't throw an error.
This is what I've got so far:
=DateAdd("w", DateDiff("w", 6, '1/1/' + (Mid(Fields!Week.Value,1,4))) + (Trim(Mid(Fields!Week.Value,Len(Fields!Week.Value),Len(Fields!Week.Value)))
- 1), 6)
and when I try to run the report in design view it returns an Expression expected error.
Edit
Sorry, I guess I should've posted my original query that's populating the report. Here it is below:
SELECT Convert(varchar(20),UPPER(DATENAME(YEAR, Time)))
+'_'+CONVERT(varchar(20),UPPER(DATENAME(MONTH, Time)))
+'_WEEK_NO_'+CONVERT(varchar(10),(DAY(Time)
+ (DATEPART(DW, DATEADD (MONTH, DATEDIFF (MONTH, 0, Time), 0))-1) -1)/7 + 1) as 'Week'
, Count(DISTINCT SUBSTRING(ModuleAndEventText,LEN('User ') + 2
, CHARINDEX(' requested', ModuleAndEventText) - LEN('User ') - 2)) as WeekCount
FROM VE1_UserLogins
WHERE DesktopId = #Pool
AND ([Time] BETWEEN (#StartDate) and (DATEADD(ms, -1, #EndDate +1)))
GROUP BY Convert(varchar(20),UPPER(DATENAME(YEAR, Time)))
+'_'+CONVERT(varchar(20),UPPER(DATENAME(MONTH, Time)))
+'_WEEK_NO_'+CONVERT(varchar(10),(DAY(Time)
+ (DATEPART(DW, DATEADD (MONTH, DATEDIFF (MONTH, 0, Time), 0))-1) -1)/7 + 1),YEAR(Time),MONTH(Time)
ORDER BY YEAR(Time), MONTH(Time), Week
I am assuming your Fields!Week.Value is string in date format like MM/dd/yyyy or something similar so if you want to get the start of that week Sunday as first day and Saturday as last day and you want to get there date then you should use below expression,
For start of week,
=DateAdd("d",1- DatePart("w", CDate(Fields!WeekDate.Value)), CDate(Fields!WeekDate.Value))
For end of week,
=DateAdd("d", 7 - DatePart("w", CDate(Fields!WeekDate.Value)), CDate(Fields!WeekDate.Value))
----------
UPDATE
Now there are two ways to achieve what you want
1)Calculate start of week and end of week at the sql and get the result to directly display on repott
2) Get time field from the sql and set expression in the SSRS
For the first way to achieve you need to add these two lines in your select sql statement
DATEADD(dd, -(DATEPART(dw, MIN(Time))-1),MIN(Time)) AS 'StartOfWeek'
,DATEADD(dd, 7-(DATEPART(dw, MIN(Time))), MIN(Time)) AS 'EndOfWeek'
Second way to achieve is
Update your Sql statement to get the date part and then include that date into the above given expression.
So for you your query you need to add,
,MIN(Time) AS WeekDate
Then you can use the above expression with using the incoming field WeekDate as input(I have updated the expression).
But, If you have no actual need for the date other than calculating the start and end of week then use the first method to get the data from the sql server as formatted.