Recently I came across the requirement to get the number of working days between any two given dates along with the factory Calendar, which needs to be implemented using ABAP CDS view.
After lot of search, I found that one can use the WORKDAYS_BETWEEN function which fits my requirement.
Since this is SQL function and we can’t use the SQL function directly in ABAP CDS views, I built a Table function along with class and method where I can use the SQL functions.
Below is the sample code and output.
Sample Data using ACDOCA –
- Used BLDAT and BUDAT as Start and End date.
- Used Factory Calendar as CH (Switzerland).
- I have hard-coded the Factory Calendar, but you can keep it as a field as per the requirement.
Table Function –>
@EndUserText.label: ‘Table function for Number of Workdays’
define table function ZNumber_of_Workdays
//with parameters parameter_name : parameter_type
mandt : abap.clnt;
Start_date : abap.dats;
WORKDAYS : abap.int1;
implemented by method ZCL_WORKING_DAYS=>GET_WORKING_DAYS;
Class & Method –>
class ZCL_WORKING_DAYS definition
create public .
for TABLE FUNCTION ZNumber_of_Workdays.
CLASS ZCL_WORKING_DAYS IMPLEMENTATION.
METHOD GET_WORKING_DAYS BY DATABASE FUNCTION
RETURN SELECT top 100 DISTINCT
‘100’ AS mandt,
BUDAT as Start_date,
BLDAT as End_date,
WORKDAYS_BETWEEN(‘CH’, BLDAT, BUDAT) as “WORKDAYS” from ACDOCA
where BUDAT > BLDAT ;
Sample Output –
You can cross check the Workdays against the table TFACS which has counter for the working days of each month.
Now you can join the above table function with the required CDS with Start_Date, End_date and Factory Calendar ID (if required) to get the number of Workdays.
This is one way of achieving it in ABAP Using Table function, please share your views and comments based on your experience of achieving the above requirement.
Reference – SAP HANA SQL documentation of WORKDAYS_BETWEEN.