cancel
Showing results for 
Search instead for 
Did you mean: 

YTD, MTD from current date

Former Member
0 Kudos

Hello,

I need to calculate the YTD and MTD from System date.

Suppose, when the user execute the report today i.e 21/11/2008, The report should display the data from 01/01/2008 to 21/11/2008 for YTD.

For MTD, it should display from 01/11/2008 to 21/11/2008.

There is no user Input from selection screen for time period.

Pls advice.

Regards,

Srikanth

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member181964
Active Contributor
0 Kudos

Hi,

Use the following Code to get the MTD, based on Input date.

Where: ZFDIM: is Customer Exit Variable on 0CALDAY and (Don't check input ready but Mandotory)

ZCDAY is User Entry Variable on 0CALDAY (Input ready tick mark)

DATA : l_s_range TYPE rsr_s_rangesid,

loc_var_range LIKE rrrangeexit.

DATA : w_year1(7) TYPE c,

w_year2 TYPE sy-datum.

DATA : w_mon(2) TYPE n.

DATA : w_year(4) TYPE c.

**************First day of Input Month************12.09.2007

WHEN 'ZFDIM'.

LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZCDAY'.

CLEAR l_s_range.

w_mon = loc_var_range-low+4(2).

w_year = loc_var_range-low+0(4).

w_year2+0(4) = w_year.

w_year2+4(2) = w_mon.

w_year2+6(2) = '01'.

l_s_range-low = w_year2.

l_s_range-sign = 'I'.

l_s_range-opt = 'EQ'.

APPEND l_s_range TO e_t_range.

ENDLOOP.

For YTD:

the following code : FMYEAR and LNYEAR are the Customer Exit Variables on 0CALMONTH, I have the same requirement, so I did like this, after that you give offset for FMYEAR and LMYEAR. Create New Selections in Columns and then restrict the above key figures with these two variables by using 0CALMONTH. Or You can create Restricted Keyfigures also, but in the both the case you need to give off set values.

Just try this FMs in SE37 then give some input and see what is out put, with this you get some Idea.

DATA : l_s_range TYPE rsr_s_rangesid,

loc_var_range LIKE rrrangeexit,

zbdatj LIKE t009b-bdatj,

zbuper LIKE t009b-poper,

zper LIKE t009b-poper.

First Month Of Current Fiscal year comment*

WHEN 'FMYEAR'.

CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'

EXPORTING

i_date = sy-datum

I_MONMIT = 00

i_periv = 'V3'

IMPORTING

e_buper = zbuper

e_gjahr = zbdatj.

CLEAR: l_s_range.

l_s_range-low+4(2) = '04'.

l_s_range-low+0(4) = zbdatj.

l_s_range-sign = 'I'.

l_s_range-opt = 'EQ'.

APPEND l_s_range TO e_t_range.

Last Month of Current Fiscal year*

WHEN 'LMYEAR'.

CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'

EXPORTING

i_date = sy-datum

I_MONMIT = 00

i_periv = 'V3'

IMPORTING

e_buper = zbuper

e_gjahr = zbdatj.

CLEAR: l_s_range.

l_s_range-low+4(2) = '03'.

l_s_range-low+0(4) = zbdatj + 1.

l_s_range-sign = 'I'.

l_s_range-opt = 'EQ'.

APPEND l_s_range TO e_t_range.

Thanks

Reddy

Former Member
0 Kudos

Hi,

Thanks for your response.

However in my case, there is no user input. I just need to calculate YTD and MTD based on sy-date.

How many variables i need to declare ?

ZFDIM is a custom exit variable . I have created a RKF with 0calday = ZFDIM.

So in below code, what is ZCDAY ?

****************

WHEN 'ZFDIM'.

LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZCDAY'.

CLEAR l_s_range.

w_mon = loc_var_range-low+4(2).

w_year = loc_var_range-low+0(4).

w_year2+0(4) = w_year.

w_year2+4(2) = w_mon.

w_year2+6(2) = '01'.

l_s_range-low = w_year2.

l_s_range-sign = 'I'.

l_s_range-opt = 'EQ'.

******************

Pls clarify.

Srikanth

former_member181964
Active Contributor
0 Kudos

Dear Srikanth, Use the following code as it is for MTD and YTD based on Sy-Datum.

To get the First day of the Month based on Sy-Datum

ZFDIM is a Customer Exit variable on 0CALDAY

DATA: zbuper LIKE t009b-poper,

zbdatj LIKE t009b-bdatj,

zzdate LIKE sy-datum.

DATA: zcweek LIKE scal-week.

data: w_day(2),

w_mon(2),

w_year(4),

w_year2 type dats.

WHEN 'ZFDIM1'.

CLEAR l_s_range.

zzdate = sy-datum.

w_mon = zzdate+4(2).

w_year = zzdate+0(4).

w_year2+0(4) = w_year.

w_year2+4(2) = w_mon.

w_year2+6(2) = '01'.

l_s_range-low = w_year2.

l_s_range-sign = 'I'.

l_s_range-opt = 'EQ'.

APPEND l_s_range TO e_t_range.

First Day and Month Of Current Fiscal year

ZFMYEAR is a Customer Exit variable on 0CALDAY

WHEN 'ZFMYEAR'.

CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'

EXPORTING

i_date = sy-datum

I_MONMIT = 00

i_periv = 'V3'

IMPORTING

e_buper = zbuper

e_gjahr = zbdatj.

CLEAR: l_s_range.

l_s_range-low+6(2) = '01'.

l_s_range-low+4(2) = '04'.

l_s_range-low+0(4) = zbdatj.

l_s_range-sign = 'I'.

l_s_range-opt = 'EQ'.

APPEND l_s_range TO e_t_range.

Restricted Key Figure or New Selction for MTD

Restrict the 0CALDAY with ranges [] ZFDIM1;0DAT

Restricted Key Figure or New Selction for YTD

Restrict the 0CALDAY with ranges [] ZFMYEAR;0DAT

Thanks

Reddy

Former Member
0 Kudos

Hi,

Check the howto guide below for YTD and MTD calculation.

https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/25d98cf6-0d01-0010-0e9b-edcd4597...

Regards.