My recently purchased 2002 X5 was running poorly, and occasional even stalling... But it ran fine cold, fuel consumption was avg 15.5 ugh..
I bit the bullet and had the dealer run the codes for me. Long story short, I paid them the $105 to diagnose and ordred the parts online. Thermostat was stuck open, idle control valve stuck, and intake camshaft position sensor was bad. $273 of online parts, and the install took me 6 hours but I am thrilled with the results. MPG went to 19.2

Just posting this in case you have had similiar symptoms and not sure where to start. I dont recommend replacing parts willy nilly, but maybe this will be helpful for someone.
The SES light went out after 5 minutes of driving.