SSRS Returning incorrect WW - reporting-services

I have an SSRS report (2008R2 3.0) that uses parameter filters for workweek in the format YYYYWW. In 2018 there were 52 work weeks based on the ISO standard (source) .
For default parameter value I select the last WW using the following formula:
DATEPART(DateInterval.Year, DATEADD("d", -7, now())) & FORMAT(DATEPART(DateInterval.WeekOfYear, DATEADD("d", -7, now())), "00")
For some odd reason this returned 201853. From what I understand it should be 201852 given the date when ran was 20180106.
I know in T-SQL I can define return type using ISO_WEEK but is there an equivalent for DateInterval in SSRS?
To simplify:
DATEPART(DateInterval.WeekOfYear, DATEADD("d",-7,now()))
Returns 53 instead of 52 when ran from 201801-201807.
Any idea as to why this is returning the wrong WeekOfYear value?

I think this may be due to your Regional First Day of the Week setting on your server.
If your First Day of the Week is Saturday, there are 53 weeks in the 2018. You can set the first day of the week with the DATEFIRST setting in SQL Server.
Use SELECT ##DATEFIRST to determine your servers setting.
This example code will show the difference.
SET DATEFIRST 7
SELECT ##DATEFIRST
SELECT DATEPART(WEEK, '2018-12-30')
SET DATEFIRST 1
SELECT ##DATEFIRST
SELECT DATEPART(WEEK, '2018-12-30')
For your query, you may need to set the first day of the week to Sunday with SET DATEFIRST 1.
MS Docs: DATEFIRST

Related

Week Start date from a Date MS Access

I have a field with dates, I will like to run a query that updates another field with the Week Start Date (Sunday). How can I do that ?
I am using MS Access.
You should be able to do that with a combination of the DateAdd() and Weekday() functions, as in:
UPDATE theTable SET weekStartDate = DateAdd("d", -Weekday(theDate) + 1, theDate)

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.

Stuck on dates using interval

A little stuck, the end user is allowed to pick a year/month and then collect the data going back 5 months. I'm good with setting the initial date off a timestamp (yyyy-mm-dd hh:mm:ss) column but can I use last_day in conjunction with interval. So if the end user picks May 2013 they can collect all the data from May 31, 2013 through January 1, 2013.
Ex. set #d2:=concat('2013-05','-01');
set #d1:=#d2 - last_day(interval 5 month);
select #d1,#d2;
Any help/guidance is much appreciated.
The function LAST_DAY takes a date as an argument, not an interval.
It would appear that the code you want is:
set #d1 := LAST_DAY(#d2 - INTERVAL 5 MONTH)

Datediff function adds startdate day also to the output

I have a report in Report Builder 1.0 wherein I have written a formula that calculates the number of days between startdate and enddate. For that I have used a DATEDIFF function in such a manner:
DATEDIFF(DAY, TODAY(), column from table which has end date)
Suppose, if today's date is 1 July 2013 & the column is sending end date as 10 July 2013, then the DATEDIFF function is returning "10 days" whereas it should return output as value "9 days". Why is it returning 10 days rather than 9?
Execute this in Sql Server and you get 9 days:
select DATEDIFF(day, '2013-07-01', '2013-07-10')
Using this as a dataset:
select CAST('2013-07-01' AS DateTime) AS StartDate, CAST('2013-07-10 23:59:59' AS DateTime) AS EndDate
then using this expression in SSRS:
=DateDiff(DateInterval.Day, Fields!StartDate.Value, Fields!EndDate.Value)
still yields 9 days, even when using a time component. Try the following expression:
=DateDiff(DateInterval.Day, DateTime.Today, Fields!EndDate.Value)
Are you displaying the date fields to make sure what you think you should get is what you actually get? For instance, make sure you aren't being returned cached data.

ms access annual criteria

I need to create a query criteria to get any date from the last 1 may of last year till today, example if I run the query now it should get the data from 1 may 2012 till today, if I run the query the next year on Feb 2013 then get the data from 1 May 2012 till feb 2013.
update
I have used the below as the [JOINED DATE] query criteria but it returns nothing, what is wrong with this?
IIf(Month([Data]![JOINED DATE])>=5,Between DateSerial(Year(Now()),5,1) And Now(),Between DateSerial(Year(Now())-1,5,1) And Now())
Your syntax is incorrect, I do not advise including the "between" keyword in the IIF statement, you want your IIF to only return the date, something like:
SELECT *
FROM A
WHERE A.Date BETWEEN IIf(Month([Joined Date])>=5,DateSerial(Year(Date()),5,1),DateSerial(Year(Date())-1,5,1) AND Date()
Note: I have used Date() rather than Now() as Now() includes the timestamp which is not necessary in this case.