With the current specifications, I give you the following code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
TypesModified = Table.TransformColumnTypes(Source,
{{"Customer", type text}, {"Action 1", type text},
{"Action2", type text}, {"ID", Int64.Type}}),
MergedQuery = Table.NestedJoin(TypesModified, {"Customer", "ID"},
Table2, {"Customer", "Action3"}, "Table2", JoinKind.LeftOuter),
Table2Developped = Table.ExpandTableColumn(MergedQuery, "Table2",
{"Action 1", "Action2"}, {"Table2.Action 1", "Table2.Action2"}),
AddAction1 = Table.AddColumn(Table2Developped, "The.Action1",
each if [Table2.Action 1]=null then [Action 1]
else [Table2.Action 1]),
AddAction2 = Table.AddColumn(AddAction1, "The.Action2",
each if [Table2.Action2]=null then [Action2]
else [Table2.Action2]),
SelectedColumns = Table.SelectColumns(AddAction2,
{"ID", "Customer", "The.Action1", "The.Action2"}),
PermutedColumns = Table.ReorderColumns(SelectedColumns,
{"Customer", "The.Action1", "The.Action2", "ID"}),
RenamedColumns = Table.RenameColumns(PermutedColumns,
{ {"The.Action1", "Action1"}, {"The.Action2", "Action2"}}),
Sorted = Table.Sort(RenamedColumns,{{"ID", Order.Ascending}})
in
Sorted
Note, this has been tested in Excel (see the Source=
line). The script is picky about column name and works with your tables. Beware, you have Action 1
with a space and Action2
, no space. This script needs a Table2
defined.
Note that the table expansion, after the Merge, doesn't preserve the order of the line, then the last action is a sort on ID.
Here, the idea is to get Action1 and Action2 from Table2 when available; and fallback to Table1 if not. This decision is on AddAction1=
and AddAction2=
lines.