We always recommend separating the trade from the new lease to get an accurate sense of where the outgoing lease is. If it’s $5k upside down the dealer isn’t eating that. Not enough detail here to judge whether this is feasible.
You’re correct a dealer buying your car doesn’t pay sales tax, but IIRC you already paid the full sales tax at inception, so there shouldn’t be tax in your 1st party lease buyout quote. Did you get a payoff from bmwfs that includes/breaks-out sales tax?
I’ve seen 26 X5s in the Marketplace at that discount and slightly more. I doubt new/non-loaner 27s will 12%+ off in 4 months but the BMW people will know better.