Solving blanks with a custom calendar and SAMEPERIODLASTYEAR

When using SAMEPERIODLASTYEAR and a custom calendar, you may get blank or incorrect results. Here is a solution to solve this problem. This also solves problems with misaligned periods lengths and multiple calendars on the same table.