An Idiom to describe the action of botching a job when it is almost complete [duplicate]

Solution 1:

You could be snatching defeat from the jaws of victory:

(idiomatic) To suddenly lose a contest one seemed very likely to win, especially through mistakes or bad judgment.
from yourdictionary.com

While the definition refers particularly to a contest, it is applicable broadly to any situation where a positive outcome seems assured.

Solution 2:

Would Choking do?

In sports, choking is the failure of a person, or persons, to act or behave as anticipated or expected.1 This can occur in a game or tournament that they are strongly favoured to win, or in an instance where they have a large lead that they squander in the late stages of the event. It can also refer to repeated failures in the same event, or simply infer an unexpected failure when the event is more important than usual.

Wikipedia