Try the above? The | character is a “separate” character that isolates what is inside of it to only appear when the field has content. You should also replace my * by inserting “link adjacent” characters from the field dropdown, which also helps to tell endnote to only include the punctuation/spaces when the field contains something, but isn’t always necessary, for reasons I never understand (ie. when are they necessary and when are they not necessary). They will appear in the template as tiny centered diamonds.
I also moved the | in relation to the year, as if it is ever empty you have retained the empty parentheses the way it was set up (obiously that would rarely be the case?).