I see two methods:
- Power Query - must have Excel for Windows 2010 or higher.
- Formula - must have Excel 2019 or 365.
Power Query (PQ) Method
This may look involved, but it takes less than 5 minutes and will be done completely by mouse. Once done, the data can be updated by simply clicking refresh.
First convert the table into an excel table and import into the PQ editor by going to the Data tab and selecting From Table/Range:
Say yes to table having headers:
This will open the PQ editor. Select the agent name field and from the Transform tab, select Fill Down.
Now select the agent name and column1 and then right-click and choose Unpivot Other Columns.
Now your data are almost there - they are normalized into columns with attributes,
so you need to pivot them into the form that you proposed above. Next step is to select Column1 and then Pivot from the Transform tab. It will bring up a window where you can select Value as the field that you want to pivot:
Click OK and now you can see the table that you wanted:
Now you need to get this back into Excel. On the Home tab, click Close and Load to:
and then choose Table and New worksheet:
And you're done.
If you get new data added to your data, you can just right-click the table you created above and select Refresh and it will update the table with the new data. If you want to apply it to future tables of data, it is easily modified without having to repeat the whole process.
Formula Method
This is already a long post and this method requires some serious formula engineering, so I will only highlight this method for now, but if it is interesting, say so in the comments and I will complete the post.
This is a formula that unpivots, but it is not suited to your table because:
- it needs those merged names to be populated into the cells below
(i.e. fill-down) and
- it treats blank cells as zeros.
In Power Query parlance, we would say that you want to "Unpivot C1:H16 By A1:B16".
=LET( unPivMatrix, C1:H16,
byMatrix, A1:B16,
upC, COLUMNS( unPivMatrix ),
byC, COLUMNS( byMatrix ),
dmxR, MIN( ROWS( unPivMatrix ), ROWS( byMatrix ) ) - 1,
upCells, dmxR * upC,
upSeq, SEQUENCE( upCells,, 0 ),
upHdr, INDEX( INDEX( unPivMatrix, 1, ), 1, SEQUENCE( upC ) ),
upBody, INDEX( unPivMatrix, SEQUENCE( dmxR ) + 1, SEQUENCE( 1, upC ) ),
byBody, INDEX( byMatrix, SEQUENCE( dmxR ) + 1, SEQUENCE( 1, byC ) ),
attr, INDEX( upHdr, MOD( upSeq, upC ) + 1 ),
mux, INDEX( upBody, upSeq/upC + 1, MOD( upSeq, upC ) + 1 ),
demux, IFERROR( INDEX(
IFERROR( INDEX( byBody,
IFERROR( INT( SEQUENCE( upCells, byC,0 )/byC/upC ) + 1, MOD( upSeq, upC ) + 1 ),
SEQUENCE( 1, byC + 1 ) ),
attr ),
upSeq + 1, SEQUENCE( 1, byC + 2 ) ),
mux ),
demux
)
To modify this would require an equivalent of Fill-Down, which is a little challenging, but possible. It would also require a filter at the end to remove those zeros.
This would not deliver your final output, it would only give you the first step. You would need to then pivot the unpivoted output which could be done by a normal excel pivot table or by writing yet another complex formula to pivot it, which is hard.
So... if you have Excel for Windows 2010 or higher, I would go for Power Query. It was designed for problems just like yours.