How to use the material substitution function to reduce inventory?

I am work for a equipment manufacturing factory, there is now a scenario that D365’s standard functionality cannot solve.

A product have a parts name A, because of the design upgrade, parts B will replace parts A.

Now parts A have some inventory, we want to use parts A in produce until parts A out of stock, then we will use parts B and no longer purchase or produce parts A any more. We want this to be done automatically during master planning.

In another words, when parts A in stock, master planning use parts A, when parts A out of stock, master planning use parts B.

D365 have a very simple formula to control material substitution, there is no way to meet our requirement, so I wonder if anyone has a good idea to solve this problem.