Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

We all know this FM FIRST_AND_LAST_DAY_IN_YEAR_GET to get the first and last day of a fiscal year, however if you want to know non-fiscal year’s (normal calendar year) first and last day of a logistic/factory calendar, there is no direct FM which gives the result.

Knowing that the calendar year starts from January and end on December, generally, when doing Yearly/YTD calculations, we define first day as: 0101 and last day as: 3112 and then concatenate with respective years accordingly in the user exit.

But, not all factory calendar years starts on 1st of January and ends on 31st December ?

If we take the SAP standard calendar (Germany – 01), its every Week starts from Monday and ends on Sunday (including weekend’s), so the Start and End date of every year changes accordingly.

This requirement rises particularly when your client/business is following with specific dates each year.

Path


Goto t.code : CMOD --> project--> EXIT_SAPLRRS0_001 --> INCLUDE ZXRSRU01


Here is the code


DATA: lv_from TYPE datum,
      lv_from1
TYPE datum,
      lv_to  
TYPE datum,

            lv_firstday TYPE  scdatum ,
      lv_lastday 
TYPE scdatum,

            lv_firstday1 TYPE  scdatum ,
      lv_lastday1 
TYPE scdatum,
      lv_year    
TYPE I,
      Cweek_last 
LIKE  SCAL-WEEK,
      Pweek_last 
LIKE  SCAL-WEEK,

      lr_range TYPE rsr_s_rangesid.


IF i_step = 1 AND
( i_vnam EQ 'ZCYEAR' OR   "Current Year
i_vnam
EQ 'ZPYEAR' ).   "Previous year

lr_range-sign = 'I'.
lr_range
-opt  = 'BT'.


*** First date of current year ***

CASE i_vnam.
WHEN 'ZCYEAR'.
lv_year 
= sy-datum(4) - 1 . " Last Year


"Since, there is no FM available to get first week of year, I choose last week of last year to get First day of current year

CALL FUNCTION 'TIME_GET_LAST_WEEK'
EXPORTING
IF_YEAR          
= lv_year
IMPORTING
EF_WEEK          
= Cweek_last. " Last week of Last year

CALL FUNCTION 'WEEK_GET_FIRST_DAY' "
EXPORTING
WEEK              
= Cweek_last
IMPORTING
DATE               = lv_firstday1. " First day of last week(Last Year)

lv_from
= lv_firstday1 + 7. " First date of current year

*** LAST date of current year***


lv_year
= sy-datum(4) . "Current year
CALL FUNCTION 'TIME_GET_LAST_WEEK'
EXPORTING
IF_YEAR          
= lv_year
IMPORTING
EF_WEEK          
= Cweek_last. " Last week of current year

CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
WEEK              
= Cweek_last
IMPORTING
DATE    = lv_firstday. "First day of last week (current year)


lv_to = lv_firstday + 6. "Last date of current year


*** First date of previous year ***

WHEN 'ZPYEAR'.


lv_year 
= sy-datum(4) - 2 . "Before last year

CALL FUNCTION 'TIME_GET_LAST_WEEK'
EXPORTING
IF_YEAR          
= lv_year
IMPORTING
EF_WEEK          
= Pweek_last. "Before last year last week (BLY)

CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
WEEK              
= Pweek_last
IMPORTING
DATE               = lv_firstday. "First day of last week (BLY)

lv_from
= lv_firstday + 7. "First date of last year


**** LAST day of previous year***


lv_to
= lv_firstday1 + 6. "Last date of last year

WHEN OTHERS.
ENDCASE.

lr_range-low  = lv_from.
lr_range
-high = lv_to.


APPEND lr_range TO e_t_range.


ENDIF.


Note: you can use the same code with slight modification for other weeks as well(ex: Sunday - Saturday ..etc)

Query Results

Variable

Selection



you can repeat both current year variable and selection definition for previous year as shown in the above images.


RSRT result


Current Year


First date 04.01.2016 - Monday

Last date 01.01.2017 - Sunday


Previous Year


First date 29.12.2014 - Monday

Last date 03.01.2016 - Sunday



if you want to calculate Year-To-Date, then replace last date in the code with current date(current year) / last year same date (for previous year).


(or) you can change it as per your requirement (ex: to current week)



PS Note: I am sure there might be various methods to achieve this, please do share your inputs too.

I figured out this way and thought to share, hope it helps. Thank you

Labels in this area