Forum Discussion
Merge in Power Query based on conditions
- 8 years ago
My suggestion would be to join on TransactionID and remove the results where the CategoryID's are equal.
As you require an inner join, this also means removing empty nested tables.let
Source = Table.NestedJoin(Table1,{"TransactionID"},Table1,{"TransactionID"},"t2",JoinKind.Inner),
FilteredOnCategory = Table.ReplaceValue(Source,each [t2],(Earlier) => Table.SelectRows(Earlier[t2],each [CategoryID] <> Earlier[CategoryID]), Replacer.ReplaceValue,{"t2"}),
RestoredType = Value.ReplaceType(FilteredOnCategory,Value.Type(Source)),
FilteredNoEmptyTables = Table.SelectRows(RestoredType, each not Table.IsEmpty([t2])),
Expanded_t2 = Table.ExpandTableColumn(FilteredNoEmptyTables, "t2", {"CategoryID"}, {"t2.CategoryID"})
in
Expanded_t2The RestoredType step restores the column types from the Source table, as Table.ReplaceValues resets all column types to any.
I am familiar with EARLIER() in DAX. This is very interesing!
Earlier[t2] and the CategoryID refer to [t2] table with nested Table1 but Earlier[CategoryID] looks at he Source[CategoryID]... -- this is where it's little confusing. So, we have each [CategoryID] <> Earlier[CategoryID] . If Ealire[t2] is looking at nested table result, why wouldn't Earlier[CategoryID] look at nested result too, it has "ealier" prefix and it's logicly to amuse that.
To put it in a different way, why Earlier[CategoryID] with prefix Earlier is not looking at nested tables (Earlier [t2]) but at the source?
Thanks
An example at the Source step.
Earlier[CategoryID] is a column in Source.
Earlier[t2] is also a column in Source.
Earlier[Category] is not inside Earlier[t2].