What i want is something like an aggr(... , Year, Year_after) with the sum of all amount of last invoice (sorting by date) for each contract of the given year. Here it will give :
Final table
Year
Year after
LastDayPeriod
Amount
2016
0
31/12/2016
50
2016
1
31/12/2017
450
2016
2
31/12/2018
450
2017
0
31/12/2017
0
2017
1
31/12/2018
0
2017
2
31/12/2019
0
2018
0
31/12/2018
300
2018
1
300
I was thinking about firstsortedvalue but something didnt work when i use aggr and firstsortedvalue, i have no result at the end :