Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Last day of the previous month based on current date in ABAP CDS Views

9,223

Hi,

If the users execute the report on today's Date , I would like to fetch the sales orders created as on last day of last month IN ABAP CDS View.

Thanks,Naveen.
1 ACCEPTED SOLUTION

former_member527468
Participant
2,567

1. Create a CDS view Table function with just one attribute - let's say lastDayofPrevMonth

2. Implement this table function with logic like below:

select last_day( add_months(current_date, -1 ) ) as "lastDateofPrevmonth" from dummy

3. Inner Join this view with your original sales order CDS View on the created date column.

8 REPLIES 8

Kiran_Valluru
Active Contributor
0 Kudos
2,567

Hello Naveen,

Create CDS view with input parameters (Refer help - https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenselect_cds_para_abexa.htm) and pass the last day of previous month to the CDS view via parameters (search SCN as there are associated threads for the same).

Still as a pointers, FM - SN_LAST_DAY_OF_MONTH / RP_LAST_DAY_OF_MONTHS


Regards,

Kiran

0 Kudos
2,567

Hi Kiran,

Thank you for your suggestion , but in my requirement there is no input parameter.

I need to get the last day of the previous month in CDS view without input parameter.

Thanks,

Naveen.

0 Kudos
2,567

Hi Kiran,

Thank you for your suggestion , but in my requirement there is no input parameter.

I need to get the last day of the previous month in CDS view without input parameter.

Thanks,

Naveen.

former_member527468
Participant
2,568

1. Create a CDS view Table function with just one attribute - let's say lastDayofPrevMonth

2. Implement this table function with logic like below:

select last_day( add_months(current_date, -1 ) ) as "lastDateofPrevmonth" from dummy

3. Inner Join this view with your original sales order CDS View on the created date column.

0 Kudos
2,567

it works perfectly. but it displays date in format 21-02-28

Is there a option to convert it to 2021-02-28 ?

Regards,

Mehmet

2,567

Hello Naveen,

I also came across this problem today. A workaround would be: get the first day of the current month by replacing the last 2 digit to '01', then +1 month and -1 day to this date.

Test:

define view ZTest_DAT as select from usr02 {
    
    erdat, --Test Date
    
    DATS_ADD_DAYS( 
        DATS_ADD_MONTHS( 
            cast(  concat( left( erdat, 6 ), '01' ) as abap.dats ), --FirstDay
            1, 'FAIL'), 
        -1, 'FAIL') 
    as LastDay
}

Result:

Best regards,

Haolin

0 Kudos
2,567

Why posting here? The question is different: "Last day of the previous month". Moreover, the question has already been answered correctly.

abehaegel
Explorer
2,567

I disagree Sandra,

His answer is very helpfull because he is not going thrue a CDS View Table Function

Regards,

Alexandre