This is a type of Pivot Table, but with no aggregation of the results (Not something that can be done in the regular Pivot Tables for Excel)
But you can do this in Power Query, available in Windows Excel 2010+ and Office 365
To use Power Query
- Select some cell in your Data Table
Data => Get&Transform => from Table/Range
- When the PQ Editor opens:
Home => Advanced Editor
- Make note of the Table Name in Line 2
- Paste the M Code below in place of what you see
- Change the Table name in line 2 back to what was generated originally.
- Read the comments in the code and explore the
Applied Steps to understand the algorithm
M Code
let
//Read in the data
//Change table name in next line to actual table name in your worksheet
Source = Excel.CurrentWorkbook(){[Name="Table22"]}[Content],
//Custom function
//see credits for explanation
pivot = fnPivotAll(Source, "Column1","Column2"),
//set data types
#"Changed Type" = Table.TransformColumnTypes(pivot,{
{"No.", Int64.Type},
{"Code", type text},
{"Date", type date}})
in
#"Changed Type"
Note that you will need to use a custom function for this solution
- To enter the custom function:
Data => GetData => From other sources => Blank Query
- When the PQ Editor opens:
Home => Advanced Editor
- Replace the code with the code below
- over on the right hand menu, change Query Name to
fnPivotAll
- See the link in the credits for understanding the algorithm and reason for doing it this way.
//credit: Cam Wallace https://www.dingbatdata.com/2018/03/08/non-aggregate-pivot-with-multiple-rows-in-powerquery/
//rename fnPivotAll
(Source as table,
ColToPivot as text,
ColForValues as text)=>
let
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColToPivot, ColForValues, each _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table,
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames)),
#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames),
#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
in
#"Expanded Values"
`