Quantcast
Channel: SCN : Blog List - SAP Business Explorer (SAP BEx)
Viewing all 103 articles
Browse latest View live

Calculating First and Last date of a Calendar Year(Monday - Sunday)

$
0
0

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 right?

 

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

 

Bex query layout.JPG

Variable

 

calyear variable.JPG

Selection


current year selection screen.JPG


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


RSRT result


RSRT output.JPG

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, I figured out this way and thought to share, hope it helps.


Check queries assigned to a workbook / Workbooks that uses specific query.

$
0
0

I can see many requesting how to check the queries inside a workbook during the last year. The steps are quite simple, and I will sumarize these steps here.

 

 

If you want to check which workbooks have an specific query as dataprovider, you need to know the query technical name.
As example, I will use my query with technical name ZEDD_TEST3.

 

 

Step 1 - Access table (SE16) -  RSRPREPDIR 

1.png

OBJVERS = A

COMPID =  ZEDD_TEST3

 

Execute(F8)

 

2.png

 

Step 2 - Access table (SE16) RSRWORKBOOK

 

Basically, RSRWORKBOOK.GENUNIID = RSRPREPDIR.GENUNIID

 

 

 

So, for my case:

 

3.png

 

GENUNIID = 4UBUGC2QE23K7REMTHZQ5W3E9

OBJVERS  = A

 

 

Execute(F8)

 

WB.png

 

We can see that I have only one workbook with my query.


Workbook ID is 3EGGEJQPR1MX5CKP6AFH0Q8DB

 

 

If you need to know the workbook title, access table RSRWBINDEXT and use the workbook id on the proper field.

 

4.png

 

 

All steps mentioned are also valid if you have to check which queries an specific workbook is used.

For this case, you have to do the reverse path:

 

  1. Table RSRWORKBOOK - Search for your workbook, on version A.
  2. Copy all GENUNIID
  3. Go to table RSRPREPDIR and paste the GENUNIID there.

 

 


Future works: It is possible to consider a creation of a REPORT to check automatically.

BEx vs. Excel Average

$
0
0

Hi

 

here is a problem, which I had recently at a client and I want to share this with you. I also posted this post on my blog (www.reyemsaibot.com).

 

When I was recently creating a new query at a customer, I have found a strange phenomenon. The query should show the average ticket number of a department. First you see the explanation and then I provide the solution of the problem. The query should show the individual, cumulative and average monthly values, as you can see in the screenshot.

 

bex analyzer wrong avg.png

 

As you can see, there are no values for the months of May, June, August, November and December, so the cumulative values remain the same as the previous month. Unfortunately the calculation of the average has a problem, when a column has no value (for example May). In comparison the average value with the Excel formula AVERAGE.

 

BEx Query Average vs. Excel Average.png

 

As you can see in the screenshot above, the key figures differ between the Excel formula and the result from the Bex Analyzer. According to the Excel formula the month of May has an average of 43, according to the Bex Analyzer it has an average of 16. This is a large deviation. As you can see this error runs through all months after one column has no value.

The interesting thing about the query is, if you have a department that has values from January to December, the average of the Bex Analyzer works without problems.

 

bex vs excel average over a whole year.png

 

The line Avg. no. tickets is identical to the line Excel formula AVG. That means the query definition is correct and working. But if there are months where no values have been posted, the average calculation of the Bex Analyzer failed.

 

The following settings have been made to the key figures:

 

• No of tickets cumulated: the key figure is only cumulative, otherwise no further calculations.

 

selection cumulated.png

 

• Avg. no tickets: Aggregation on average with reference characteristic to Calendar Year/Month (0CALMONTH) and calculating the individual values and results as average or moving average.

 

formula aggregation.png

bex formula calculations.png

 

The question arises to me now is, why it works correctly when the entire year have posted values, but not when there are months without value. The cumulative value is also correctly calculated and doesn't show an error. Only the average value is incorrect. Unfortunately, that was a problem for my client, since the report was intended of the controlling department and this department, you surely know, love Excel ;)

 

So why does the Bex Analyzer interpret a blank line at cumulative values as 0, but failed if you want to calculate an average.

 

So that the controlling department get the same result in the Bex Analyzer like with the Excel formula, we needed a solution. After a little research I found this SCN article, which says insert a new formula and add to this formula a 0. So nothing complicated, just a simple solution.

 

formula general.png

 

As you can see on the screenshot below, the row Avg no of tickets + 0 has now another value than the row avg no of tickets. The cumulative value is identical.

 

BEx AVG with 0 fixed.png

 

Now the comparison with the Excel formula AVERAGE:

 

bex analyzer with fixed 0 and excel avg.png

 

And as you can see now, the row avg. no of tickets and the row Excel formula AVG are identical. So I only have to hide the unnecessary rows and the query can be used. I hope this may help someone when you walk into the same issue.

Viewing all 103 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>