Hey @DanielJansson !
The best way I can see you creating this calculated field is by wrapping it in an ifelse() that determines the date and then runs your existing calculation. For example, the logic would be "if the year date part of ${M1} is equal to the year date part of Now() then run the existing calculation, else (margin by month/ avg margin of previous year). Then filter your visual using relative date filters set to current and previous year or previous N years with a 1 year look back window.
However the reason I suggested two visuals formatted together is because you are using sumif() functions which would most likely cause nesting errors. It would probably be easier to break up the visual than totally adapt your current calculation.
You can also share this as an arena and we can work through the calculation there.