i have a 99 bmw 528i im working on. the code is p0340. i have replaced both cam sensors, found the maf sensor broken and replaced that to. the car will run great one minute and then has no power the next.
Fixing this one (or one very similar) burned me bad (not literally) back in 2001. Most likely cause is a defective Motor Driven throttle Valve BMW S.I 12 07 99.
I am responding based upon your sympton description and not the generic P0340 (Camshaft Position Sensor Malfunction). I believe the P0340 is secondary to the MDK failure. Confirmation can be had by scanning with a BMW scanner and looking for specific BMW codes.
But by sympton and pratical experience I think its the MDK. The S.I. for BMW is pretty much BMW jibberish but it does list the replacement part number 13 54 1 433 414. warranty labor time was a little over a hour. They are not fun but not the worst to replace. affected a large group of cars.