My thought is that the maf sensor is working correctly.
did you reset the adjustments and parameters after replacing it?
I agree with @Greydog.
check the inlet pipe between the maf sensor and the throttle body for any cracks, especially on the elbow part. Throttle body could also be the issue.
my x5 has the 3.0i engine and was all over the place with idle and cutting out etc.
it was the throttle body which was faulty , but while I replaced it, I found the inlet pipe had little splits in it at the elbow folds and was leaking in air, confusing the mixture as the new maf sensor was saying one thing and the air ratio was another. The old maf sensor was reading a lower value and the engine had over compensated for it.
Check for splits, reset the values and see where it goes.