Roger Federer completed a 1,200th career victory in dramatic fashion on Thursday, as he booked a place in the Madrid Open quarter-final.