Skip to main content

Module 5: Period Calculation Utilities - Time Bucket Management

Back to Overview | ← Previous: ALV Display | Complete Reference →

Module Overview

Lines: 4885-5617
Importance: Tier 2 (High) - Time period management foundation
Purpose: Calculates and manages time periods (accounting periods, calendar periods, weeks) for data aggregation

Business Context - The "Time Organizer"

Think of these modules as a sophisticated calendar system that:

  • Understands different types of business time (fiscal periods, calendar months, weeks)
  • Calculates date ranges for each time bucket
  • Handles year rollovers and period boundaries
  • Creates the time framework that all other modules use for aggregation

Module Structure and Relationships

Detailed Form Analysis

Form 1: calulate_acct_periods (Lines 4885-5050)

Purpose: Calculates accounting period date ranges based on company fiscal calendar

FORM calulate_acct_periods.
REFRESH t_future_dates.

DATA: lv_poper1 TYPE poper.
lv_poper1 = p_prd1_l.

" Get first day of starting accounting period
CALL FUNCTION 'FIRST_DAY_IN_PERIOD_GET'
EXPORTING
i_gjahr = p_yr1_l
i_periv = 'N1' " Period variant (fiscal calendar)
i_poper = lv_poper1
IMPORTING
e_date = v_first_day1.

Key Processing Steps:

  1. Period Date Calculation:
  1. 18-Month Forward Calculation:
CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
EXPORTING
date = v_first_day1
days = '00'
months = '18' " Standard 18-month horizon
signum = '+'
years = '00'
IMPORTING
calc_date = lv_date1.
  1. Current Period Handling:
" If running for current period, start from today
IF lv_poper1+1(2) = sy-datum+4(2).
CALL FUNCTION 'YMTIR_R1117_FUTURE_DATES'
EXPORTING
current_date = sy-datum " Start from today
number_days = lv_date
TABLES
future_dates = t_future_dates.
ELSE.
" Start from period beginning
CALL FUNCTION 'YMTIR_R1117_FUTURE_DATES'
EXPORTING
current_date = v_first_day1 " Start from period start
number_days = lv_date
TABLES
future_dates = t_future_dates.
ENDIF.

Business Logic Explanation:

  • If user is running for the current accounting period, overdue requirements (before today) go into the first bucket
  • If running for future periods, all requirements from the period start are included
  • This ensures proper handling of "overdue" vs. "future" demand

Form 2: build_acct_columns (Lines 5051-5254)

Purpose: Builds accounting period columns and determines which periods to display

FORM build_acct_columns .
" Convert first day to period number for indexing
CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'
EXPORTING
i_date = v_first_day1
i_periv = 'N1'
IMPORTING
e_buper = v_poper " Current period
e_gjahr = v_bdatj. " Current year

Index Calculation Logic:

Column Count Calculation:

IF p_yr1_l EQ p_yr1_h.                    " Same year
IF p_prd1_h GT p_prd1_l.
w_columns = p_prd1_h - p_prd1_l + 1. " Simple difference
ENDIF.
ELSEIF p_yr1_l LT p_yr1_h. " Cross-year
lv_years1 = p_yr1_h - p_yr1_l.
IF lv_years1 EQ 1.
w_columns = 12 - p_prd1_l + p_prd1_h + 1. " Across one year
ELSEIF lv_years1 EQ 2.
w_columns = 12 - p_prd1_l + p_prd1_h + 1 + 12. " Across two years
ENDIF.
ENDIF.

Form 3: calulate_stnd_periods (Lines 5255-5419)

Purpose: Calculates standard calendar periods (Jan, Feb, Mar, etc.)

FORM calulate_stnd_periods .
DATA: lv_poper2 TYPE poper.
lv_poper2 = p_prd2_l.

" Build calendar month from period
CONCATENATE p_yr2_l lv_poper2 '01' INTO v_first_day2.

" Calculate 18 months forward
CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
EXPORTING
date = v_first_day2
days = '00'
months = '18'
signum = '+'
years = '00'
IMPORTING
calc_date = lv_date1.

Key Differences from Accounting Periods:

  • Uses calendar months (Jan=01, Feb=02) instead of fiscal periods
  • Builds dates using CONCATENATE instead of SAP period functions
  • Simpler calculation since calendar months are standardized

Form 4: build_stnd_columns (Lines 5420-5617)

Purpose: Similar to build_acct_columns but for calendar periods

Logic Flow:

Form 5: build_col_date_index (Lines 2109-2207)

Purpose: Builds weekly time buckets for the most granular reporting

FORM build_col_date_index.
DATA: lw_index TYPE sy-index,
lw_date TYPE sy-datum.

" Get Monday of current week
CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
EXPORTING
date = sy-datum
IMPORTING
monday = lw_date.

" Build weekly ranges
DO p_weeks TIMES.
r_dates-low = lw_date.
r_dates-high = lw_date + 6. " Sunday
r_dates-sign = 'I'.
r_dates-option = 'BT'. " Between
APPEND r_dates.

lw_date = lw_date + 7. " Next Monday
ENDDO.
ENDFORM.

Weekly Bucket Logic:

Data Structures Created

For Accounting/Calendar Periods:

TableStructurePurpose
t_future_periodsPeriod definitionsStart/end dates per period
t_future_datesDaily date listAll dates in horizon

For Weekly Periods:

TableStructurePurpose
r_datesRange tableWeek date ranges (Mon-Sun)

Key Fields in t_future_periods:

TYPES: BEGIN OF type_periods,
poper TYPE poper, " Period number (01-12)
bdatj TYPE bdatj, " Year
start_date TYPE sy-datum, " Period start date
end_date TYPE sy-datum, " Period end date
END OF type_periods.

Time Bucket Comparison

Bucket TypeGranularityBusiness UseSAP Function Used
Accounting PeriodsFiscal periods (usually monthly)Financial reportingFIRST_DAY_IN_PERIOD_GET
Calendar PeriodsCalendar monthsStandard business cyclesDate arithmetic
Weekly Periods7-day periods (Mon-Sun)Operational planningGET_WEEK_INFO_BASED_ON_DATE

Advanced Business Logic

Overdue Handling:

All period calculation forms handle the concept of "overdue" requirements:

Year Rollover Logic:

" Handle period 13 becoming period 1 of next year
IF lv_prd GT 12.
lv_prd = 1.
lv_bdatj = lv_bdatj + 1.
ENDIF.

18-Month Standard Horizon:

All forms use an 18-month forward-looking horizon, which is a business standard for:

  • Supply chain planning
  • Vendor capacity planning
  • Long-term demand forecasting

Performance and Memory Considerations

Optimization Techniques:

  1. Selective Date Generation: Only generates dates needed for the horizon
  2. Period-based Lookup: Uses period tables instead of daily loops
  3. Range Table Usage: Efficient date range checking with IN operator

Memory Usage:

  • t_future_dates: Approximately 550 entries (18 months × 30 days)
  • t_future_periods: Maximum 18 entries (18 months)
  • r_dates: User-controlled (typically 52 weeks = 52 entries)

Integration with Main Processing

Usage by Other Modules:

Data Flow Integration:

  1. Selection Screen → User chooses time bucket type and range
  2. Period Calculation → Creates time bucket definitions
  3. Data Processing → Uses buckets to aggregate requirements
  4. Output Building → Maps aggregated data to time columns
  5. ALV Display → Shows time-based column headers

Common Configuration Scenarios

Scenario 1: Current Month Production Planning

  • Selection: Accounting periods, current month + 3 months
  • Result: Overdue + next 3 fiscal periods
  • Business Use: Short-term production scheduling

Scenario 2: Annual Budget Cycle

  • Selection: Calendar periods, January - December next year
  • Result: 12 calendar months starting from January
  • Business Use: Annual procurement planning

Scenario 3: Weekly Operations

  • Selection: Weekly periods, 52 weeks
  • Result: Next 52 Monday-Sunday periods
  • Business Use: Detailed operational planning

Error Handling and Validation

Period Validation:

" Validate period exists in fiscal calendar
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Date Range Validation:

  • End period must be after start period
  • Maximum 18-month horizon enforced
  • Invalid period/year combinations handled gracefully

Business Impact

These utility forms are critical because they:

  1. Standardize Time Representation across all report variants
  2. Handle Complex Calendar Logic (fiscal vs. calendar vs. weekly)
  3. Support Multiple Planning Horizons (weekly operational, monthly tactical, annual strategic)
  4. Manage Overdue Requirements appropriately based on user context
  5. Enable Flexible Reporting by supporting different time bucket granularities

The time bucket framework created by these utilities becomes the foundation for all demand aggregation and reporting throughout the entire application.


Back to Overview | ← Previous: ALV Display | Complete Reference →