3 weeks ago - last edited 3 weeks ago
I have a WEBI report that needs to generate a date range starting from 4/28/2024, calculating 20 weeks. When it reaches the end of the month, it needs to create a new interval. I've been thinking about it for weeks but can't come up with a solution, so I'm seeking help from everyone.
Prompt day1 = 04/28/2024 for example.
Prompt Weeks = 20.
Week 1: 2024/4/28-2024/4/30 (<--Original week 1 is from 4/28 to 5/4 but it during month end)
Week 1: 2024/5/1-2024/5/4
Week 2: 2024/5/5-2024/5/11
Week 3: 2024/5/12-2024/5/18
Week 4: 2024/5/19-2024/5/25
Week 5: 2024/5/26-2024/5/31
Week 5: 2024/6/1-2024/6/1
Week 6: 2024/6/2-2024/6/8
Week 7: 2024/6/9-2024/6/15
Week 8: 2024/6/16-2024/6/22
Week 9: 2024/6/23-2024/6/29
Week 10: 2024/6/30-2024/6/30
Week 10: 2024/7/1-2024/7/6
Week 11: 2024/7/7-2024/7/13
Week 12: 2024/7/14-2024/7/20
Week 13: 2024/7/21-2024/7/27
Week 14: 2024/7/28-2024/7/31
Week 14: 2024/8/1-2024/8/3
Week 15: 2024/8/4-2024/8/10
Week 16: 2024/8/11-2024/8/17
Week 17: 2024/8/18-2024/8/24
Week 18: 2024/8/25-2024/8/31
Week 19: 2024/9/1-2024/9/7
Week 20: 2024/9/8-2024/9/14
I do not know how to do this natively in WebI although it likely is. Perhaps my solution using free-hand SQL employing MIN() and MAX() functions with the OVER clause will work for you or spark another idea.
The functional SQL Server SQL code can be seen here.
I have repeated the code below with the exception of uncommenting the line prompting for a date and commenting the line with the hard-coded date.
DECLARE @StartDate DATE;
DECLARE @EndDate DATE;
DECLARE @WeekOffset INT;
DECLARE @WeeksInYear INT;
SET @StartDate = @Prompt('Enter Starting Date:','D',,Mono,Free,Not_Persistent,,User:0)
--SET @StartDate = '2024-04-28';
SET @EndDate = DATEADD (DAY, -1, DATEADD (WEEK, 20, @StartDate));
SET @WeekOffset = DATEPART (WEEK, @StartDate) - 1;
-- @WeeksInYear is to account for some years having more weeks than most (e.g., 2028)
SET @WeeksInYear = DATEPART (WEEK, DATEFROMPARTS (YEAR (@StartDate), 12, 31));
--SELECT
-- @StartDate AS [StartDate]
-- , @EndDate AS [EndDate]
-- , @WeekOffset AS [WeekOffset]
-- , @WeeksInYear AS [WeeksInYear];
WITH dates_cte (CalendarDate) AS
(
SELECT @StartDate AS [CalendarDate] -- Put the start date here
UNION ALL
SELECT DATEADD (DAY, 1, CalendarDate)
FROM dates_cte
WHERE CalendarDate < @EndDate -- Put the end date here
)
SELECT DISTINCT
IIF(DATEPART (WEEK, d.CalendarDate) - @WeekOffset < 0
, (@WeeksInYear - 1) + (DATEPART (WEEK, d.CalendarDate) - @WeekOffset)
, DATEPART (WEEK, d.CalendarDate) - @WeekOffset) AS [WeekNumber] -- necessary to span the end of the year
, MIN (d.CalendarDate) OVER (PARTITION BY
DATEPART (MONTH, d.CalendarDate)
, DATEPART (WEEK, d.CalendarDate)
) AS [FirstDayOfWeek]
, MAX (d.CalendarDate) OVER (PARTITION BY
DATEPART (MONTH, d.CalendarDate)
, DATEPART (WEEK, d.CalendarDate)
) AS [LastDayOfWeek]
FROM dates_cte d
ORDER BY WeekNumber
, FirstDayOfWeek
OPTION (MAXRECURSION 32767);
The dates_cte CTE generates all of the dates between your Start Date and End Date (Start Date + 20 weeks - 1 day). If you have a Calendar table you could use that with the same date range instead of the CTE. The MIN() and MAX() functions find the first and last day of each unique combination of Month and Week yielding this...
If you use a database other than SQL Server my code will likely need to be adjusted. If you are not able to do that you will need to wait for the native WebI solution.
In any case, please do your own thorough testing with a variety of dates...
Hope this works for you or gets you going down the right path. I may try giving this a try natively in WebI with some calculation contexts when I have more time.
Noel
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
72 | |
8 | |
8 | |
7 | |
6 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.