Looking for a correct word / idiom

Solution 1:

It’s impossible to say if things would have turned out differently for Y if he’d aligned himself with Z against X from the start but if so (and even regardless) it seems that Y

bet on/backed the wrong horse.

Solution 2:

Consider:

no good deed goes unpunished proverb Beneficial actions often go unappreciated or are met with outright hostility. If they are appreciated, they often lead to additional requests. - wiktionary

Presumably, Y thought it a good deed to help X, but that turned out to be less than beneficial for Y.

Note that like many proverbs, this proverb is a generalisation that doesn't always hold true.