Less than two years ago, the man who will become Spain's new prime minister was staring at the premature end of an unremarkable political career.
The heavyweights of Spain's Socialists had forced Pedro Sanchez' removal as their leader. Back-to-back losses by the party in general elections had left Mr. Sanchez without credit and he was ostracized after a rebellion from within his own ranks.
Fast forward to Friday: Sanchez is set to become the leader of the eurozone's fourth leading economy after completing an audacious bid to oust conservative Prime Minister Mariano Rajoy from power in a no-confidence vote.
Sanchez completed his spectacular turnaround after seizing the opportunity provided by last week's court verdict in a massive corruption case involving Mr. Rajoy's conservative Popular Party. His appeal for a government clean of scandal, coupled with a promise to hold new elections soon, brought him just enough votes in parliament to end Rajoy's 6 1/2 years in charge.
In October 2016, Sanchez looked finished. He had lost a bid to form a government and been cast out by the party's regional chiefs. Sanchez then gave up his seat in parliament when the Socialists' caretaker leadership opted to allow Rajoy to stay in power, avoiding new elections they feared would result in even bigger losses.
Sanchez, however, refused to go quietly. Even so, his vow to "get in my car and visit every corner of Spain to win back" the party sounded quixotic given his scarce support among its higher echelons.
But Sanchez embraced the role of underdog and tapped into the anger of rank-and-file members who felt that he had been unfairly dumped by the party elite.
He earned a stunning victory to return as leader in May 2017 when he won an internal party election against Susana Diaz, the candidate anointed by the party's powerbrokers, including former prime ministers Felipe Gonzalez and Jose Luis Rodriguez Zapatero.
Still, the 46-year-old Sanchez, a basketball player in his youth, had never shown the political savvy to challenge Rajoy.
A political survivor and adroit parliamentarian, Rajoy had apparently cleared the biggest hurdle facing his minority government when he passed a national budget recently and said he was confident he would to see out the remaining two years of his term.
That was until last week's ruling by the National Court that delivered hefty prison sentences to 29 business people and ex-members of Rajoy's Popular Party, including some elected officials, for fraud, money laundering and tax evasion, among other crimes.
Sanchez, often criticized as lacking a feel for the moment, pounced and left lawmakers with the choice of keeping a party thoroughly tarnished by corruption in charge or making a fresh start.
In parliament Thursday, Sanchez presented his case that Rajoy must go because Spain "is sick and tired of serial corruption."
"Today I have returned to this chamber for three reasons, to act in accordance with my beliefs, out of a sense of responsibility and for the sake of our democracy," Sanchez told lawmakers.
The move to topple Rajoy comes with Sanchez's Socialists trailing both the Popular Party and the upstart center-right Ciudadanos (Citizens) in polls.
By moving into La Moncloa palace, the seat of government in Madrid, Sanchez will recover the spotlight that he had been sorely missing since leaving parliament.
He can try to leverage his position to recharge the Socialists' support, which has been eroded by the rise of both Ciudadanos and the far-left Podemos (We Can), while he picks the most opportune moment to carry through on his pledge to call new elections.
In the meantime, he will face the pitfalls of being in charge of a minority government that will have a very tough time getting anything done. Rajoy has already warned of the "political instability" a Sanchez-led government would suffer.
Sanchez may also pay a heavy price for taking down Rajoy. In order to secure votes against Rajoy from Catalonia's separatists, Sanchez had to promise to open talks with the secessionists about the future of the northeastern region.
Sanchez had been Rajoy's most loyal backer in his takeover of Catalonia's regional government following its failed secession attempt last year. Now, he runs the risk that both the Popular Party and Ciudadanos will label him a traitor.
An economist by education, Sanchez was chief of cabinet to the United Nations envoy to Bosnia, Carlos Westendorp, in the late 1990s. In 2003 he took a position as a city council member in Madrid and then climbed up the Socialist party ladder, first becoming a lawmaker and then party leader in 2014.
This story was reported by The Associated Press.