Merge data using left join

673d23dd68cf08627e90e16be103471a

Updated at March 20th, 2020

If you have new data fields to add to a project, or you need to replace existing data you can use a left join process (e.g. appending specialties to an existing data file, or to bring in coded values of open-ends.). A left join keeps data in the left table, and only bring in the data on the right table if there is a matched key.

In the example below, we use a data process to import coded open-ends into an existing Protobi project. The process code joins two files - the primary data file (main) and the new file with the coded open-ends ("OE").

var rows= data["main"] //primary data file
var OE = data["OE"]    //auxiliary data file 

// Keys are merge keys. 
// Keeps are all the fields to add to the main data file or replace existing data.

var keys = ["responseid", "respid"]
var keep = ['q19_OE','q27_OE']

rows = CSV.left_join(rows, OE, keys, keep);

Optionally - Instead of an array, keys and/or keep can be an object of key pairs if the two files do not have identical column names, such as capitalization changes or name differences. For example:

var keys = {"responseid":"ResponseID", "respid":"RespID"}
var keep = {"q19_OE" : "q19_OE_coded", "q27_OE" : "q27_OE_coded"}


Was this article helpful?