I am trying to calculate the sales volume difference between a given month and the same month one year back (e.g Jan 2021 - Jan 2020). The tricky part is that it should be done on customer level. My table now looks like this:
Date | Customer_number | Sales_Volume |
---|---|---|
14.01.2020 | 1111 | 1000 |
13.01.2020 | 2222 | 2000 |
21.02.2020 | 1111 | 4000 |
17.02.2020 | 2222 | 5000 |
22.01.2021 | 1111 | 1500 |
24.01.2021 | 2222 | 2600 |
19.02.2021 | 1111 | 4700 |
18.02.2021 | 2222 | 5800 |
And my output should look like this:
Date | Customer_number | Sales_Volume | Volume_Difference |
---|---|---|---|
14.01.2020 | 1111 | 1000 | 0 |
13.01.2020 | 2222 | 2000 | 0 |
21.02.2020 | 1111 | 4000 | 0 |
17.02.2020 | 2222 | 5000 | 0 |
22.01.2021 | 1111 | 1500 | 500 |
24.01.2021 | 2222 | 2600 | 600 |
19.02.2021 | 1111 | 4700 | 700 |
18.02.2021 | 2222 | 5800 | 800 |
This is for a calculated column:
Column =
VAR _y = YEAR([Date])
VAR _m = MONTH([Date])
VAR _c = [Customer_number]
VAR _tbl_prev = FILTER(ALL('Table'), [Customer_number] = _c && MONTH([Date]) = _m && YEAR([Date]) = _y - 1)
VAR _tbl_curr = FILTER(ALL('Table'), [Customer_number] = _c && MONTH([Date]) = _m && YEAR([Date]) = _y)
VAR _result = SUMX(_tbl_curr, [Sales_Volume]) - SUMX(_tbl_prev, [Sales_Volume])
RETURN IF(COUNTROWS(_tbl_prev), _result, 0)