This can be accomplished with Power Query. The following queries assume that you have created Tables with your data, named "Table1" on Sheet1 and "Table2" on Sheet2. This will require 5 separate queries: 2 for the Tables of data, one function, a merge table, and one final query to combine the data. For all of these, add a new blank query (Data tab > Get Data > From Other Sources > Blank Query). You will also need to create a named range for cell D1 on Sheet1 titled "YearValue".
YearValue function
Paste the below code into the Advanced Editor (Power Query > View > Advanced Editor). Change the name (on the right) to "YearValue".
let
Source = Excel.CurrentWorkbook(){[Name="YearValue"]}[Content]{0}[Column1]
in
Source
Close & load to connection only.
Table 1 query
Paste the below code into the Advanced Editor (Power Query > View > Advanced Editor). Change the name (on the right) to "Table1".
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
TypeColumns = Table.TransformColumnTypes(Source,{{"AccountID", Int64.Type}, {"Cost", Int64.Type}}),
AddYearColumn = Table.AddColumn(TypeColumns, "Year", each YearValue)
in
AddYearColumn
Close & load to connection only.
Table 2 query
Paste the below code into the Advanced Editor (Power Query > View > Advanced Editor). Change the name (on the right) to "Table2".
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
TypeColumns = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"AccountID", Int64.Type}, {"Location", type text}, {"Phone#", type text}, {"Email type", type text}, {"Cost", Int64.Type}})
in
TypeColumns
Close & load to connection only.
Merge table query
Paste the below code into the Advanced Editor (Power Query > View > Advanced Editor). Change the name (on the right) to "MergeTable".
let
Source = Table.FromColumns({List.Distinct(List.Select(Table1[AccountID]&Table2[AccountID],each _ <> null))},type table[AccountID = text]),
MergeQueries = Table.NestedJoin(Source,{"AccountID"},Table1,{"AccountID"},"Table1",JoinKind.LeftOuter),
ExpandedTable1 = Table.ExpandTableColumn(MergeQueries, "Table1", {"AccountID", "Cost", "Year"}, {"Table1.AccountID", "Table1.Cost", "Table1.Year"}),
MergeQueries2 = Table.NestedJoin(ExpandedTable1,{"AccountID"},Table2,{"AccountID"},"Table2",JoinKind.LeftOuter),
ExpandedTable2 = Table.ExpandTableColumn(MergeQueries2, "Table2", {"Year", "AccountID", "Location", "Phone#", "Email type", "Cost"}, {"Table2.Year", "Table2.AccountID", "Table2.Location", "Table2.Phone#", "Table2.Email type", "Table2.Cost"}),
RemoveColumns = Table.RemoveColumns(ExpandedTable2,{"AccountID", "Table2.Year", "Table2.AccountID", "Table2.Cost"}),
ReorderColumns = Table.ReorderColumns(RemoveColumns,{"Table1.Year", "Table1.AccountID", "Table2.Location", "Table2.Phone#", "Table2.Email type", "Table1.Cost"}),
RenameColumns = Table.RenameColumns(ReorderColumns,{{"Table1.Year", "Year"}, {"Table1.AccountID", "AccountID"}, {"Table2.Location", "Location"}, {"Table2.Phone#", "Phone#"}, {"Table2.Email type", "Email type"}, {"Table1.Cost", "Cost"}})
in
RenameColumns
Close & load to connection only.
Combine tables query
Paste the below code into the Advanced Editor (Power Query > View > Advanced Editor). Change the name (on the right) to "CombineTables".
let
Source = Table.Combine({Table2, MergeTable}),
FilterRows = Table.SelectRows(Source, each ([Year] <> null)),
SortRows = Table.Sort(FilterRows,{{"AccountID", Order.Ascending}})
in
SortRows
Close & load worksheet (as desired).
HTH