BAQ Calculated Fields

Hello everyone,
I’m hoping someone will be able to help me with this little one!

I have created a BAQ (attached)
TEAGLE-PartStoresAllocation.baq (146.9 KB)

I’m trying to create a calculated field called “Restock Qty” which is already appearing but giving the wrong information. I am currently using

ISNULL(SUM(D.PartPlant_MinimumQty - K.Calculated_ATP_Calculated), D.PartPlant_MinimumQty - C.Calculated_PARTSQOH)

But I need it to be something like

DemandQty - OnHandQty max(MinStockLevel or Min SupplyQty or Min Mfg Lot Size)

I cannot seem to work out the calculation of this one, also is it possible to show items if

ATP (calculated field) is less than Min Stock Level (calculated field)?

Here is a screenshot of the BAQ.

I’m totally in the dark with this one soo any help is awesome!

Kind regards,

You’ll use a case statement to do that.

case when a<=b and a<=c then a
when b<=a and b<=c then b
else c

Can you give me an example using my BAQ?