Here's one method.
I don't have time this evening to do a detailed explanation, but if you read the M Code and comments; and also follow along the Applied Steps, you should be able to figure it out.
Basics
- Create separate tables for each year
- Join pairs of tables using Product as the
Key
- Expand the paired tables
- Fill in the blank years and products
- Insert a blank row after each table pair
M Code
let
//Read in Data
//Change next two lines to reflect your actual data source
Source = Excel.CurrentWorkbook(){[Name="Table13"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Company", type text}, {"Product", type text}, {"Profit", Int64.Type}, {"Year", Int64.Type}}),
//create yearly table pairs
years = List.Sort(List.Distinct(#"Changed Type"[Year])),
tables = List.Generate(
()=>[t=Table.ExpandTableColumn(
Table.NestedJoin(
Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{0}), "Product",
Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{1}), "Product",
"Joined", JoinKind.FullOuter),"Joined",
{"Company","Product","Profit","Year"},{"Company1","Product1","Profit1","Year1"}),
idx=0],
each [idx] < List.Count(years)-1,
each [t=Table.ExpandTableColumn(
Table.NestedJoin(
Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{[idx]+1}),"Product",
Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{[idx]+2}), "Product",
"Joined", JoinKind.FullOuter),"Joined",
{"Company","Product","Profit","Year"},{"Company1","Product1","Profit1","Year1"}),
idx=[idx]+1],
each Table.InsertRows(
Table.FillDown(Table.FillUp([t],{"Year", "Year1"}),{"Year","Year1"}),
Table.RowCount([t]),
{[Company="", Product="", Profit=null, Year=null, Company1=null, Product1="", Profit1=null, Year1=null]})
),
#"Converted to Table" = Table.FromList(tables, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandTableColumn(#"Converted to Table", "Column1",
{"Company", "Product", "Profit", "Year", "Company1", "Product1", "Profit1", "Year1"}),
//Fill in the blank company and products
#"Fill in blanks" = Table.FromRecords(
Table.TransformRows(#"Expanded Column1",
(r)=> Record.TransformFields(r,{
{"Company", each if _ = null then r[Company1] else _},
{"Product", each if _ = null then r[Product1] else _},
{"Product1", each if _ = null then r[Product] else _}
} ))),
#"Removed Columns" = Table.RemoveColumns(#"Fill in blanks",{"Company1"}),
#"Removed Bottom Rows" = Table.RemoveLastN(#"Removed Columns",1),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Bottom Rows",{{"Company", type text}, {"Product", type text}, {"Profit", Int64.Type}, {"Year", Int64.Type}, {"Product1", type text}, {"Profit1", Int64.Type}, {"Year1", Int64.Type}})
in
#"Changed Type1"
