I believe that the Use track relationships option is what you will need to get the Work information.
I assume you looked at https://picard.musicbrainz.org/docs/scripting/ and the links included on that page?
Looks like you have the assignment going the wrong way. Try:
$set(title,$if2(%work%,%title%))
This should set the title variable to %work% if it exists, otherwise it is left as %title%. Does that do what you’re after?