Skip to main content
Best answer

Consolidate duplicate fields and aggregate corresponding values as outputs

  • January 30, 2022
  • 9 replies
  • 287 views

Troy Tessalone,

Is there a way to identify unique specific fields and then aggregate their corresponding values for records with duplicate specified fields and output that sum with the respective specified field? I’m horrible at explaining things so below is an example.

I have a Get request that pulls all records from a view in Airtable and below is a sample Test Action:

records:

     1:

          id: rec1234567890

          fields:

               Username: Sam

               Value: 1

     2:

 

          id: recasdg654896

          fields:

               Username: Bob

               Value: 1

     3:

 

          id: recaga54asg64

          fields:

               Username: Sam

               Value: 1

     4:

 

          id: rec987654135

          fields:

               Username: Sam

               Value: 1

Basically I would want it to output Username: Sam ; Value: 3 and Username: Bob ; Value: 1 because Sam is in there 3 times, each time with a Value of 1 and Bob is only in there 1 time with a value of 1.

Best answer by Troy TessaloneBest answer by Troy Tessalone

@cothraw1 

Regardless, here’s the Code step…

CONFIG

 

CODE

let Username = inputData.Username.split(",");
let Value = inputData.Value.split(",");
let Distinct = [];
let Total = [];

Username.forEach((i) => {
  if (!Distinct.includes(i)) {
    Distinct.push(i);
    Total.push(0);
  }
});

for (let i = 0; i < Username.length; i++) {
  let x = Distinct.indexOf(Username[i]);
  Total[x] += parseInt(Value[i]);
}

output = [{Distinct, Total, Username, Value}];

 

RESULTS

 

OUTPUT of ARRAYS

 

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

9 replies

Troy Tessalone
Forum|alt.badge.img+14

Hi @cothraw1 

What apps are you using in the Zap?

Please post screenshots with how the data is returned from the Zap step that you are trying to use.


  • Author
  • New
  • 4 replies
  • January 30, 2022

Hey Troy,

 

The only app I’m using to get this data is I’m pulling it from an Airtable view. If I can get it to aggregate values per unique username, I’ll post it back to Airtable later in the zap which is why I need it to output individual usernames and aggregate values per username. The trigger for the zap is just a daily schedule and the 2nd step is a Get request to pull the Airtable data from a particular view.  I have it setup to create a loop from line items afterwards. However, that caused issues. I initially had it setup to find the username and look to see what the current value is for that username in Airtable and then just add each value to its current value as it loops through but it loops through so quickly that it doesn’t give Airtable enough time to update its value from a previous value being added to it before the next duplicate username tries to find it again. So using the above example, where Sam is supposed to ultimately have a value of 3, it just ends up with a value of 1. I figured it would be better to have Zapier add the values together and only have it look in Airtable once before updating it.


Troy Tessalone
Forum|alt.badge.img+14

Hi @cothraw1 

I initially had it setup to find the username and look to see what the current value is for that username in Airtable and then just add each value to its current value as it loops through but it loops through so quickly that it doesn’t give Airtable enough time to update its value from a previous value being added to it before the next duplicate username tries to find it again.

Have you tried adding a Delay step to the Zap? https://zapier.com/apps/delay/help

 

Or have you tried using Rollup fields in Airtable? https://support.airtable.com/hc/en-us/articles/360042807233-Rollup-field-overview


  • Author
  • New
  • 4 replies
  • January 30, 2022

Troy,

I have not tried a delay because I didn’t think that would help because, as an example, if two “Sams” enter the delay 1 second apart then they should be released from the delay 1 second apart which means the delay wouldn’t accomplish anything. Is that correct or is my understanding wrong?


  • Author
  • New
  • 4 replies
  • January 30, 2022

I have not tried using a roll up field because I’m not entirely certain how to use them but I don’t mind giving it a shot.


Troy Tessalone
Forum|alt.badge.img+14

@cothraw1 

Maybe give the Delay After Queue a try:

 


Troy Tessalone
Forum|alt.badge.img+14

@cothraw1 

Airtable Rollups will depend on your intended use case.

It’s not entirely clear how you are trying to configure your Zap logic and how those values would be updated in the Airtable Base.

Perhaps you can outline the desired behavior from end to end.


Troy Tessalone
Forum|alt.badge.img+14
  • Zapier Expert
  • 30995 replies
  • Answer
  • January 30, 2022

@cothraw1 

Regardless, here’s the Code step…

CONFIG

 

CODE

let Username = inputData.Username.split(",");
let Value = inputData.Value.split(",");
let Distinct = [];
let Total = [];

Username.forEach((i) => {
  if (!Distinct.includes(i)) {
    Distinct.push(i);
    Total.push(0);
  }
});

for (let i = 0; i < Username.length; i++) {
  let x = Distinct.indexOf(Username[i]);
  Total[x] += parseInt(Value[i]);
}

output = [{Distinct, Total, Username, Value}];

 

RESULTS

 

OUTPUT of ARRAYS

 


  • Author
  • New
  • 4 replies
  • January 30, 2022

Troy,

That works perfect! Thanks so much!