Hello,
Its not going to be straight solution. Your calculation will work at Sales order / line item level not at any other level
My approach would be to go from Sales > Delivery > Billing. So no issues whether a sales order is still to be delivered or billed the model will support.
1) Sales Order, SOItem , Delivery & Delivery item as key field in the DSO.
There is always one to one relation between delivery and billing. So billing document can be as data field.
2) Map 2LIS_11_VAITM , 2LIS_11_VASCL kpis to the data field in the DSO. For these fields there will not be any mapping from 2LIS_12_VCITM , 2LIS_13_VDITM.
3) Map 2LIS_12_VCITM kpis to the data field in the DSO.
4) In 2LIS_13_VDITM you will get the delivery document / item in the field preceding document / item. So this makes things easy as in 13_vditm you have sales order, delivery & billing document.
Hope this gives you some idea
Regards
Gajesh