cancel
Showing results for 
Search instead for 
Did you mean: 

Date minus (-) one month via formular

Former Member
0 Kudos

Hello,

I look for a BW formula to calculate a date - one month. At the moment I use the following formula but I'm not happy with this solution.


IF( DATE_MONTH( CALDAY ) = DATE_MONTH( ADD_TO_DATE( LOAD_DATE, NEGATIVE( 15 ) ) ), WS_COUNT, 0 )

Regards, Thomas

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Here's a go at it using a routine. Not sure if the c's need to be i's for the calculations, so you may need to play around with it. Also this code doesn't check for validity of the 29th, 30th, 31st dates (March 31 going back to Feb 31 is invalid).

DATA: l_m1(2) type c,

l_m2(2) type c,

l_y(4) type c,

l_d(2) type c,

l_lastmonth like <date_field>.

l_y = <date_field>+0(4). "first 4 characters = year

l_m1 = <date_field>+4(2). "5-6 = month

l_d = <date_field>+6(2). "7-8 = day

l_m2 = l_m1 - 1. " get last month

IF l_m1 = 0. "check for january

l_m1 = 12.

l_y = l_y - 1.

ENDIF.

CONCATENATE l_y l_m1 l_d into l_lastmonth. "rebuild date

RESULT = l_lastmonth.

It's a start and a direction, so take it as such and not as the correct solution because I don't really know your requirements or exactly what you are trying to do.

Brian

Former Member
0 Kudos

Hello,

thanks for help. I've modified the code a little bit and it runs perfect.


DATA: l_m1(2) type c,
      l_m2(2) type c,
      l_y(4) type c,
      l_d(2) type c,
      l_lastmonth like SOURCE_FIELDS-LOADDATE.

l_y = SOURCE_FIELDS-LOADDATE+0(4).   "Year eg. 2008
l_m1 = SOURCE_FIELDS-LOADDATE+4(2).  "Month eg. 05
l_d = '01'.                          "First of month eg 01

l_m2 = l_m1 - 1.

IF l_m2 = 0.
   l_m2 = 12.
   l_y = l_y - 1.
ENDIF.

CASE l_m2.
  WHEN '1'. l_m2 = '01'.
  WHEN '2'. l_m2 = '02'.
  WHEN '3'. l_m2 = '03'.
  WHEN '4'. l_m2 = '04'.
  WHEN '5'. l_m2 = '05'.
  WHEN '6'. l_m2 = '06'.
  WHEN '7'. l_m2 = '07'.
  WHEN '8'. l_m2 = '08'.
  WHEN '9'. l_m2 = '09'.
ENDCASE.

CONCATENATE l_y l_m2 l_d into l_lastmonth. "Create date

RESULT = l_lastmonth.

Regards, Thomas

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Thomas,

First convert date to Month and Use offset ( -1 ) to derive previous month.

Use Variables to display same.

Hope it Helps

Srini

Former Member
0 Kudos

Hi,

I'm not a ABAP king and I will try it with the build in formula editor via transformation but I can't find a good solution.

Regards, Thomas