Different from your approach, here I first constructDefine a new tablefunction dsblookup
by appending a column ("B") to look up the dsB
, whose value is obtained from ds2
via the ID
key:
lookup[x_] := Query[SelectFirst[#ID == x &], "B"]@ds2
dsb =
then replace the missing values,
SortBy["ID"]@ds3[All, SortBy["ID"]@ds[All<|#,"B" Append[#-> Replace[#B, "B"Missing["Unmatched"] -> lookup[#ID]]lookup[#ID]]|> &]
you may then join this with or just lookup the B
value in all rows of ds3
from ds2
:,
SortBy["ID"]@JoinAcross[dsbSortBy["ID"]@ds3[All, ds2<|#, {"ID","B" "Date"},-> "Outer"]lookup[#ID]|> &]