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.
Related
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))
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")
I have a query where I need to calculate the week number based on the fiscal start of the year occurring on 01.07.YEAR
The problem I have is that when it goes to 30 Days of a calendar year I don't get the correct result and so I need to amend my script. I want a query that returns the correct week number regardless of what month I am in.
I have a calendar table populated; I add 18 months to the current date which give me the period and so I tried to do the same to the week but I am going around in circles.
Below is the code I am using:
SET DATEFIRST 1;
SELECT
[date],
DATEPART(wk, DATEADD(MONTH, 18, DATEADD(dd, -2, [date]))) 'FWeek',
LEFT(CONVERT(varchar, DATEADD(MONTH, 18, [date]), 112), 6) AS 'Period',
DATENAME(dw, [date]) AS 'Day'
FROM [dbo].[DP_PERIOD_DATES]
WHERE [date] >= '01/11/2018'
AND [date] < '04-01-2019'
ORDER BY [date] DESC
Try datediff to get the number of weeks from 1/7/x.
The extra datediff is to get the first of the year and add 6 to get the 7th of the year.
SELECT [date],
DateDiff(wk,
--JANUARY 7th of Current Year
DATEADD(yy, DATEDIFF(yy, 0, DATEADD(m,18,[date])), 0)+6
, DATEADD(m,18,[date]))
FROM [dbo].[DP_PERIOD_DATES]
WHERE [date] >= '01/11/2018'
AND [date] < '04-01-2019'
ORDER BY [date] DESC
SET FWEEK = DATENAME(yy, (DATEADD(month, 18, [date]))) + RIGHT('00'+DATENAME(dy,(datepart(DAYOFYEAR,DATEDIFF(DY,0,[date])/7*7+547)+5)/7),2)
, period = LEFT(CONVERT(varchar, DATEADD(month, 18, [date]),112),6)
Thanks Paul for all your help; it really wasnt easy as trying to apply someone elses logic to a date. I got it working using the above code in case anyone is looking for the same type of answer in the future
This year, August contains week #'s 31, 32, 33 & 34.
How can I make a query that gets all returns from each week in the month(8)?
Example:
select sum(a) as MyTot
from MyTable
where week numbers are included in month
Clear as mud?
I can get the return for each week because there is a field with the week number in it. I need to sum all the weeks that are in any given month.
Thanks,
You could create a calendar table, containing all kinds of data about dates - weekday, week number, day of year, day of month, month name, month number etc'. Then you can join your current table with the calendar table, filtering by the relevant month.
In fact, this might be a very useful thing to have, as pointed out by Aaron Bertrand in his Creating a date dimension or calendar table in SQL Server article.
Another option is to compute the first and last date of the month, get the week number of these dates, and use that to select the data from your table:
DECLARE #StartDate date = DATEADD(Month, DATEDIFF(Month, 0, GetDate()), 0)
DECLARE #EndDate date = DATEADD(Day, -1, DATEADD(Month, 1, #StartDate))
SELECT SUM(a) as MyTot
FROM MyTable
WHERE weekNumber >= DATEPART(WEEK, #StartDate)
AND weekNumber <= DATEPART(WEEK, #EndDate)
Please note, however, that the DATEPART(WEEK, #Date) return value depends on the value set by SET DATEFIRST. You might also want to look at ISO_WEEK.
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)