Hi community,
I need to calculate last twelve month value of a measure given some conditions. But this calculation should be added as a row with a new dimension member of LTM. Below picture simply illustrates the calculation logic. For Quarter case LTM equals 'latest quarter value' + 'latest year value' - 'previous year quarter value'. In other words 3m + 12m - 3m. For half-year case LTM equals 'latest half-year value' + 'latest year value' - 'previous year half-year value'. In other words 6m + 12m - 6m

But in order to calculate LTM, multiple conditions need to be satisfied. There must be previous period in last year value and last full year value. On top of that, all these values should be in same currency. The challenge is that data is not regular as I do not have always quarter, half-year, year, etc. records. It changes from company to company.
I created below sample input data. My data initially does not include these NULL months. I create those to facilitate rolling if applicable. Let me further clarify case by case:
- CompanyA June 2018 period is simple to implement. LTM can be calculated for June 2018 as June 2017 half-year and December 2017 full-year is available in USD terms. LTM sales value is June 2018 + December 2017 - June 2017. Please note that I do not need LTM Sales as a new column. Instead, I need as a new row where Period is LTM.
- But, CompanyA March 2019 period is not ideal to calculate LTM as same period in last year (March 2018) is missing. Whereas, June 2019 period satisfies LTM conditions.
- Please also note that December is not always 'Year'. The data should not be taken into account as calendar based. Some companies have year-end a.k.a 'Year' other than December. As you can see CompanyB case. year-end is June and so half-year is December. However, December 2018 period does not satisfy LTM condition due to no previous period.
- CompanyD June 2019 period is suitable to calculate LTM. However, June 2020 is not ideal to calculate. Although this period satisfies required periods conditions, it fails to satisfy currency condition, as currency should be same for all periods subject to LTM.
- CompanyE March 2018, June 2018, and September 2018 are ideal to calculate LTM. However September 2018 is tricky here. As this period is Q3 and covers only three months. So, in this case LTM = Q3 + H1 + Year - Q3 - H1
- September 2019 is again tricky. It already covers 9 months, so I need 9 months coverage in previous year. In this case LTM = YTD + Year - Q3 - H1
As you can see the data is quite irregular:
- Not all companies have December year-end (Year)
- Interims are not stable. Q1, Q2, and Q3 as quarters (3months range) H1 as half year (6months range), YTD (9months range)
- Currency can be changing across periods
| Company | Date | Period | FX | Sales |
| CompanyA | 31.12.2016 | Year | USD | 1308 |
| CompanyA | 31.01.2017 | | | |
| CompanyA | 28.02.2017 | | | |
| CompanyA | 31.03.2017 | | | |
| CompanyA | 30.04.2017 | | | |
| CompanyA | 31.05.2017 | | | |
| CompanyA | 30.06.2017 | H1 | USD | 1767 |
| CompanyA | 31.07.2017 | | | |
| CompanyA | 31.08.2017 | | | |
| CompanyA | 30.09.2017 | | | |
| CompanyA | 31.10.2017 | | | |
| CompanyA | 30.11.2017 | | | |
| CompanyA | 31.12.2017 | Year | USD | 3680 |
| CompanyA | 31.01.2018 | | | |
| CompanyA | 28.02.2018 | | | |
| CompanyA | 31.03.2018 | | | |
| CompanyA | 30.04.2018 | | | |
| CompanyA | 31.05.2018 | | | |
| CompanyA | 30.06.2018 | H1 | USD | 1892 |
| CompanyA | 31.07.2018 | | | |
| CompanyA | 31.08.2018 | | | |
| CompanyA | 30.09.2018 | | | |
| CompanyA | 31.10.2018 | | | |
| CompanyA | 30.11.2018 | | | |
| CompanyA | 31.12.2018 | Year | USD | 4100 |
| CompanyA | 31.01.2019 | | | |
| CompanyA | 28.02.2019 | | | |
| CompanyA | 31.03.2019 | Q1 | USD | 2823 |
| CompanyA | 30.04.2019 | | | |
| CompanyA | 31.05.2019 | | | |
| CompanyA | 30.06.2019 | H1 | USD | 8511 |
| CompanyA | 31.07.2019 | | | |
| CompanyA | 31.08.2019 | | | |
| CompanyA | 30.09.2019 | | | |
| CompanyA | 31.10.2019 | | | |
| CompanyA | 30.11.2019 | | | |
| CompanyA | 31.12.2019 | Year | USD | 6494 |
| CompanyA | 31.01.2020 | | | |
| CompanyA | 29.02.2020 | | | |
| CompanyA | 31.03.2020 | | | |
| CompanyA | 30.04.2020 | | | |
| CompanyA | 31.05.2020 | | | |
| CompanyA | 30.06.2020 | H1 | USD | 3714 |
| CompanyB | 30.06.2017 | Year | EUR | 165 |
| CompanyB | 31.07.2017 | | | |
| CompanyB | 31.08.2017 | | | |
| CompanyB | 30.09.2017 | | | |
| CompanyB | 31.10.2017 | | | |
| CompanyB | 30.11.2017 | | | |
| CompanyB | 31.12.2017 | | | |
| CompanyB | 31.01.2018 | | | |
| CompanyB | 28.02.2018 | | | |
| CompanyB | 31.03.2018 | | | |
| CompanyB | 30.04.2018 | | | |
| CompanyB | 31.05.2018 | | | |
| CompanyB | 30.06.2018 | Year | EUR | 2242 |
| CompanyB | 31.07.2018 | | | |
| CompanyB | 31.08.2018 | | | |
| CompanyB | 30.09.2018 | | | |
| CompanyB | 31.10.2018 | | | |
| CompanyB | 30.11.2018 | | | |
| CompanyB | 31.12.2018 | H1 | EUR | 3019 |
| CompanyB | 31.01.2019 | | | |
| CompanyB | 28.02.2019 | | | |
| CompanyB | 31.03.2019 | | | |
| CompanyB | 30.04.2019 | | | |
| CompanyB | 31.05.2019 | | | |
| CompanyB | 30.06.2019 | Year | EUR | 5940 |
| CompanyC | 30.06.2020 | H1 | EUR | 140 |
| CompanyC | 31.07.2020 | | | |
| CompanyC | 31.08.2020 | | | |
| CompanyC | 30.09.2020 | Q3 | EUR | 85 |
| CompanyD | 30.06.2018 | H1 | USD | 798 |
| CompanyD | 31.07.2018 | | | |
| CompanyD | 31.08.2018 | | | |
| CompanyD | 30.09.2018 | | | |
| CompanyD | 31.10.2018 | | | |
| CompanyD | 30.11.2018 | | | |
| CompanyD | 31.12.2018 | Year | USD | 2056 |
| CompanyD | 31.01.2019 | | | |
| CompanyD | 28.02.2019 | | | |
| CompanyD | 31.03.2019 | | | |
| CompanyD | 30.04.2019 | | | |
| CompanyD | 31.05.2019 | | | |
| CompanyD | 30.06.2019 | H1 | USD | 2200 |
| CompanyD | 31.07.2019 | | | |
| CompanyD | 31.08.2019 | | | |
| CompanyD | 30.09.2019 | | | |
| CompanyD | 31.10.2019 | | | |
| CompanyD | 30.11.2019 | | | |
| CompanyD | 31.12.2019 | Year | EUR | 4778 |
| CompanyD | 31.01.2020 | | | |
| CompanyD | 29.02.2020 | | | |
| CompanyD | 31.03.2020 | | | |
| CompanyD | 30.04.2020 | | | |
| CompanyD | 31.05.2020 | | | |
| CompanyD | 30.06.2020 | H1 | USD | 2494 |
| CompanyE | 31.12.2016 | Year | EUR | 4591 |
| CompanyE | 31.01.2017 | | | |
| CompanyE | 28.02.2017 | | | |
| CompanyE | 31.03.2017 | Q1 | EUR | 1214 |
| CompanyE | 30.04.2017 | | | |
| CompanyE | 31.05.2017 | | | |
| CompanyE | 30.06.2017 | H1 | EUR | 2493 |
| CompanyE | 31.07.2017 | | | |
| CompanyE | 31.08.2017 | | | |
| CompanyE | 30.09.2017 | Q3 | EUR | 1333 |
| CompanyE | 31.10.2017 | | | |
| CompanyE | 30.11.2017 | | | |
| CompanyE | 31.12.2017 | Year | EUR | 5177 |
| CompanyE | 31.01.2018 | | | |
| CompanyE | 28.02.2018 | | | |
| CompanyE | 31.03.2018 | Q1 | EUR | 1406 |
| CompanyE | 30.04.2018 | | | |
| CompanyE | 31.05.2018 | | | |
| CompanyE | 30.06.2018 | H1 | EUR | 2855 |
| CompanyE | 31.07.2018 | | | |
| CompanyE | 31.08.2018 | | | |
| CompanyE | 30.09.2018 | Q3 | EUR | 1546 |
| CompanyE | 31.10.2018 | | | |
| CompanyE | 30.11.2018 | | | |
| CompanyE | 31.12.2018 | Year | EUR | 6357 |
| CompanyE | 31.01.2019 | | | |
| CompanyE | 28.02.2019 | | | |
| CompanyE | 31.03.2019 | | | |
| CompanyE | 30.04.2019 | | | |
| CompanyE | 31.05.2019 | | | |
| CompanyE | 30.06.2019 | | | |
| CompanyE | 31.07.2019 | | | |
| CompanyE | 31.08.2019 | | | |
| CompanyE | 30.09.2019 | YTD | EUR | 5419 |
| CompanyE | 31.10.2019 | | | |
| CompanyE | 30.11.2019 | | | |
| CompanyE | 31.12.2019 | Year | EUR | 7419 |
| CompanyF | 31.12.2018 | Year | GBP | 5299 |
| CompanyF | 31.01.2019 | | | |
| CompanyF | 28.02.2019 | | | |
| CompanyF | 31.03.2019 | Q1 | GBP | 1398 |
| CompanyF | 30.04.2019 | | | |
| CompanyF | 31.05.2019 | | | |
| CompanyF | 30.06.2019 | H1 | GBP | 2638 |
| CompanyF | 31.07.2019 | | | |
| CompanyF | 31.08.2019 | | | |
| CompanyF | 30.09.2019 | YTD | GBP | 3849 |
| CompanyF | 31.10.2019 | | | |
| CompanyF | 30.11.2019 | | | |
| CompanyF | 31.12.2019 | Year | GBP | 5332 |
| CompanyF | 31.01.2020 | | | |
| CompanyF | 29.02.2020 | | | |
| CompanyF | 31.03.2020 | Q1 | GBP | 1970 |
| CompanyF | 30.04.2020 | | | |
| CompanyF | 31.05.2020 | | | |
| CompanyF | 30.06.2020 | H1 | GBP | 4397 |
| CompanyF | 31.07.2020 | | | |
| CompanyF | 31.08.2020 | | | |
| CompanyF | 30.09.2020 | YTD | GBP | 5661 |
Desired output to be added back to original data:
| Company | Date | Period | FX | Sales |
| CompanyA | 30.06.2018 | LTM | USD | 3805 |
| CompanyA | 30.06.2019 | LTM | USD | 1697 |
| CompanyD | 30.06.2019 | LTM | USD | 3458 |
| CompanyE | 31.03.2018 | LTM | EUR | 5369 |
| CompanyE | 30.06.2018 | LTM | EUR | 5539 |
| CompanyE | 30.09.2018 | LTM | EUR | 5752 |
| CompanyE | 30.09.2019 | LTM | EUR | 7375 |
| CompanyF | 31.03.2020 | LTM | GBP | 5904 |
| CompanyF | 30.06.2020 | LTM | GBP | 7091 |
| CompanyF | 30.09.2020 | LTM | GBP | 7144 |
I think it requires Batch Macro kind of approach. I am quite new and not much sophisticated to be honest. Any guidance would be deeply appreciated.
Thanks a lot