You can do this with the following table and mapping.
1. Table setup

2. Mapping code
// Get all the nodes including segments that go with the DG1
var dg1Groups = message.getAllNodes('DG1[group=!DG1]');
//Cycle each group to see if the other dg1 groups need to be removed
for (var i = 0; i < dg1Groups.length; i++) {
//Parse the group from a string to a hl7 message object
var dg1Group = qie.parseHL7String(dg1Groups[i]);
//Check if the code should be alone in the message.
var specialProcess = qie.doTableLookup(dg1Group.getNode('DG1-3.1'), 'N', 'dg1Codes', 'Code', 'Special Process');
if (StringUtils.equalsIgnoreCase('Y', specialProcess)) {
// if it is special then remove all but that dg1 group
message.removeAllNodes('DG1[group=!DG1]');
message.addChild('/', 'DG1', dg1Group);
// since we found one we break the for loop to stop processing all othe DG1
break;
}
}