cancel
Showing results for 
Search instead for 
Did you mean: 

SAP WEBI how to generate 20 week date range during first day of month and last day of month

TyngHui
Newcomer
0 Kudos

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

 

ayman_salem
Active Contributor
0 Kudos
Did you already have the generated date and week in your webi and only had to create the new intervals by the end of the month?

Accepted Solutions (0)

Answers (1)

Answers (1)

nscheaffer
Active Contributor
0 Kudos

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...

nscheaffer_0-1715088802603.png

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...

  • All dates within one year
  • Dates that span more than one year
  • Dates that include a year with more weeks than most. Seems like that shouldn't happen, but it does in SQL Server (e.g., compare the week number returned by the DATEPART(WEEK, '2024-12-31') to that of DATEPART(WEEK, '2024-12-31'))

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

nscheaffer
Active Contributor
0 Kudos
Does anyone know why the "Preformatted Text" option looks decent (not great) within the editor, but then all on one line once I post the solution?
nscheaffer
Active Contributor
0 Kudos
I changed my "Preformatted Text" to indented text with a different font. The "Insert Code" option does nothing for me.