I have a table that looks like this:
+---------+--------------+----------------+-------------+----------------+
| Product | Prior Period | Current Period | Next Period | Values_Current |
+---------+--------------+----------------+-------------+----------------+
| AAA | DEC21 | JAN22 | FEB22 | 1234 |
+---------+--------------+----------------+-------------+----------------+
| AAA | JAN22 | FEB22 | MAR22 | ABCD |
+---------+--------------+----------------+-------------+----------------+
| AAA | FEB22 | MAR22 | APR22 | abcd |
+---------+--------------+----------------+-------------+----------------+
| BBB | DEC21 | JAN22 | FEB22 | 5678 |
+---------+--------------+----------------+-------------+----------------+
| BBB | JAN22 | FEB22 | MAR22 | EFGH |
+---------+--------------+----------------+-------------+----------------+
| BBB | DEC22 | MAR22 | APR22 | efgh |
+---------+--------------+----------------+-------------+----------------+
I would like to generate the following table, where I can have the value of current period, previous period, and next period all in the same row.
+---------+--------------+----------------+-------------+--------------+----------------+-------------+
| Product | Prior Period | Current Period | Next Period | Values_Prior | Values_Current | Values_Next |
+---------+--------------+----------------+-------------+--------------+----------------+-------------+
| AAA | DEC21 | JAN22 | FEB22 | N/A | 1234 | ABCD |
+---------+--------------+----------------+-------------+--------------+----------------+-------------+
| AAA | JAN22 | FEB22 | MAR22 | 1234 | ABCD | abcd |
+---------+--------------+----------------+-------------+--------------+----------------+-------------+
| AAA | FEB22 | MAR22 | APR22 | ABCD | abcd | N/A |
+---------+--------------+----------------+-------------+--------------+----------------+-------------+
| BBB | DEC21 | JAN22 | FEB22 | N/A | 5678 | EFGH |
+---------+--------------+----------------+-------------+--------------+----------------+-------------+
| BBB | JAN22 | FEB22 | MAR22 | 5678 | EFGH | efgh |
+---------+--------------+----------------+-------------+--------------+----------------+-------------+
| BBB | DEC22 | MAR22 | APR22 | EFGH | efgh | N/A |
+---------+--------------+----------------+-------------+--------------+----------------+-------------+
How exactly do I achieve that?
Right now I am thinking about doing self join for 2 times:
- 1st time with df[prior period] = df[current period] as matching key
- 2nd time with df[next period] = df[current period] as matching key ...
Is this a right approach? If not, is there a better way to do this?
Much appreciation for your help!