SSRS Month over Month Date Expression beginning with Previous business day - reporting-services

The business requirement is to have a report with a month over month date starting with the previous business day. How do I achieve?
For month over month - my expression is:
=dateadd("m",-3,Today)
For previous business day - my expression is:
=DateAdd(DateInterval.Day
, Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
,DatePart(DateInterval.WeekDay, Today()) = 1, -2
,True, -1)
, Today())
How can I combine these into one expression so that the month over month starts with the previous business business day and not today's date?
Thanks,

You can use your previous business day expression in the start business day expression:
=dateadd("m",-3,
DateAdd(DateInterval.Day
, Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
,DatePart(DateInterval.WeekDay, Today()) = 1, -2
,True, -1)
, Today())
)
However the outer DATEADD function could produce a date which is not business day and your query will return rows from that date, if your datasource doesn't match dates in non-business days that is not a problem, the problem comes when you have non-business dates and you don't want to report it.
For simplicity I'd create a Hidden parameter (check the Hidden property radio control in Parameter properties) called ThreeMonthsPreviousDay, set it to Date/Time data type and use the the below expression in the Default Values property.
=DateAdd(DateInterval.Month,-3,
DateAdd(DateInterval.Day
, Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
,DatePart(DateInterval.WeekDay, Today()) = 1, -2
,True, -1)
, Today())
)
Now you can get the Business Date 3 months before the previous business day so use:
=DateAdd(DateInterval.Day,
Switch(
DatePart(DateInterval.WeekDay, Parameters!ThreeMonthsPreviousDay.Value) = 2, -3,
DatePart(DateInterval.WeekDay, Parameters!ThreeMonthsPreviousDay.Value) = 1, -2,
True, 0),
Parameters!ThreeMonthsPreviousDay.Value)
Let me know if this helps.

Related

Date parameter expression(s) - SSRS

I am struggling to work out how to get an SSRS report's date parameters to default to the:
Date 1: Last day of the previous month - in the previous year.
so for example today = 22/06/2021 - I'd need 31/05/2020 to appear
Date 2: Last day of the current month - in the previous year.
So for example today = 22/06/2021 - I'd need 30/06/2020 to appear.
any assistance gratefully received. Thanks.
I always find it is easier to first change the date to the first of the current month and go from there. I used to use DATEADD but have started using the VB.NET way - it's actually easier for complicated calculations like this.
For the first of the current month:
=DATEADD("d", 1 - DAY(TODAY))
OR
=TODAY.AddDays(1 - TODAY.Day)
Then subtract a year:
=DATEADD("y", -1, DATEADD("d", 1 - DAY(TODAY)))
OR
=TODAY.AddDays(1 - TODAY.Day).AddYears(-1)
Then subtract a day from that to get the end of the previous month:
=DATEADD("d", -1, DATEADD("y", -1, DATEADD("d", 1 - DAY(TODAY))))
OR
=TODAY.AddDays(1 - TODAY.Day).AddYears(-1).AddDays(-1)
For the last day of current month in previous year, a month should be added before subtracting the day:
=DATEADD("d", -1, DATEADD("M", 1, DATEADD("y", -1, DATEADD("d", 1 - DAY(TODAY)))))
OR
=TODAY.AddDays(1 - TODAY.Day).AddYears(-1).AddMonths(1).AddDays(-1)
The VB.NET is easier to read and can be calculated as you read it while reading the nested DATEADDs is more difficult.
For the fist expression use
=
DateAdd(
DateInterval.Day,
day(today()) * -1,
DateAdd(DateInterval.Year, -1, today())
)
for the 2nd use
=
DateAdd(
DateInterval.Day,
Day(today()) * -1,
DateAdd(
DateInterval.Month,
1,
DateAdd(DateInterval.Year, -1, today())
)
)
In the first expression starting in he middle and working outwards.
Use DateAdd to subtract 1 year from today's date, then subtract the current day number (22) from this.
The 2nd is..
Subtract 1 year from today's date, then add one month to it, then finally subtract the current day number.

SSRS query first day of last month based on current day

I have 1 column [Day ID] with format data "YYYYMMDD". Now i want create query EVALUATE SUMMARIZECOLUMNS with filter: 'Table'[Day ID] = FORMAT(PREVIOUSMONTH(NOW(),"YYYYMMDD").
Example: to day is 20210204 then my filter result is: 'Table'[Day ID] = 20210101.
Can you help me create filter?
To do this in the query, you would DATEADD to subtract the day number from today and then subtract another month:
WHERE [Day ID] = CONVERT(CHAR(8), DATEADD(MONTH, -1, DATEADD(DAY, 1 - DAY(GETDATE()), GETDATE())), 112)
If you wanted to do this in the report's dataset filter, you would do something similar but with the SSRS report functions like the FORMAT that you had:
Filter Value Expression
=FORMAT(DATEADD("M", -1, DATEADD("d", 1 - DAY(TODAY), TODAY)),"yyyyMMdd")

Report Builder 3 Creating a current, 30, 60, date range parameter to generate reports

I am new to Report Builder and I have been tasked with creating a date range search parameter. They are looking for current, 30 day, and 60 day values. The overall report is working and I have only made a few changes to it. So in parameters I made a dateRange parameter and set data type up as Date/Time and no boxes are check below. in the available values properties I have specified 3 values:
Current: =DateAdd("d", -1, Today())
30 day Range: =DateAdd("d", -30, Today())
60 day Range: =DateAdd("d", -60, Today())
Default values and advanced properties have remained untouched.
Why did I go with those expressions? I was looking at the StartDate and EndDate values and the enddate value has =DateAdd("d", -1, Today()) and so my logic said going back 30 and 60 days i would need add a neg in front. This I think is errant thinking. The customer is looking for the parameter to return the the values starting from the first day of the month so I need to make sure I use a month.minValue to have the report always start on the first day of the month. I hope all of this makes sense. Again I am very new to Report Builder so forgive me for my ignorance.
Jim
Based on your question and feedback comments, this is what I got.
Define DateRange parameter as follows:
Available values:
=DateAdd("d", -1, Today())
=DateAdd("d", -30, Today())
=DateAdd("d", -60, Today())
For use current =DateAdd("d", -1, Today()) as default you must to put it in the default values tab of DateRange parameter.
Now for handle the end date I've created other parameter named EndDate set it default value to current:
=DateAdd("d", -1, Today())
Now in the dataset query you have to use the parameter DateRange to get the first date of month. Check this example.
select *
from mytable a
where a.DocDate between DATEADD(month, DATEDIFF(month, 0, #DateRange), 0)
and #EndDate
DATEADD(month, DATEDIFF(month, 0, #DateRange), 0) will produce the first date of month based on the daterange selection example using
today date:
current: 01/10/2015 (dd/MM/yyy format)
30 days range: 01/09/2015
60 days range: 01/08/2015
EndDate parameter will produce 26/10/2015
Let me know if this was helpful

How to do YTD, MTD calculations based on the Today()

I am very new to SSRS, I have below scenario.
I have to calculate measures for YTD, MTD, Last 7 Days and Previous day based on the today(). I need to use these YTD, MTD, Last 7 days and Previous day variables in the calculations while writing the IIF syntax
For example:
sum(IIF(Year="this year is Year to Date", value, sales)
sum(IIF(CreatedDate="the last 30days values ",sales)
Can any one please tell me how to achieve this
Thanks
You can use the YEAR function to get the year of a date field. An IIF statement is used to evaluate an expression (in your case if the date is within the current year) then the desired result if the expression is true then the result if false. So your YTD formula could look like:
=SUM(IIF(YEAR(Fields!YourDateField.Value) = Year(TODAY()), Fields!Sales.Value, 0)
This reads: The sum of (if the year = current year then Sales else 0).
The last 30 days is similar but you would use the DATEADD function to figure what the date was 30 days ago:
=SUM(IIF(Fields!YourDateField.Value >= DATEADD("d", -30, TODAY()), Fields!Sales.Value, 0)
https://technet.microsoft.com/en-us/library/aa337194(v=sql.100).aspx

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.