Struggling to combine runs within the same execution

I’m struggling to combine items in my n8n workflow because they end up in different runs within the same execution.

The Decide Next Lookup node determines which branch an item goes to.
Based on that decision, the item follows a specific lookup path, but eventually they all come back together at the Good? node, which decides whether the result is good enough or if we should try another lookup method.

Because of this loop, my Results CSV node shows multiple runs in a single execution (for example, 3 runs → 3 CSV files).

What I’d like instead is a single final CSV file that combines all finished items, no matter how many times they ended up going back to the “Decide” node.

Does anyone know the best pattern to aggregate those items into one CSV at the end?

I found other topics about the same issue but it did not solve my issue.

I even tried what is shown in that workflow: Merge multiple runs into one | n8n workflow template

What am I missing?

Thanks

If I understand you correctly, your logic outputs a row of information. You then want to add it to a table, in your case a CSV file, then you want to call the entire worksheet. I do this with an n8n native datatable. (see attached image)

You should be able to do this with an ‘add row’ node, then when your run is done, do a ‘get many rows’ node.

You need to add the data into somewhere database, you can also use Datatable for that.

I thought the Datatables were used to save data across several workflow executions.

In my case, the “runs” are still part of the same execution:

And what is wrong with the link that you provided in your question. it does what you want. add the code node in the template before convert to csv

@ramin1

It does not seem to work.

I still get 3 runs output in the “Merge items” Code node.

Is that line correct?

results = results.concat($("Cleanup").all(0, i));

I replacedNoOp with Cleanup (the previous node?).

Looking at it, it looks like the last run combined all items (I have only 5 in total), is that the expected behavior?

How do I then just use that last run output?

Thanks

Your Merge Items Code Node is inside a loop and run 3 times. you must put it outside the loop (it must run once) and then attach csv node

@ramin1

I see, thanks for highlighting this. :folded_hands:

However, I don’t have just one “logic” node like NoOp in the example, what should the merging code contain where it uses results.concat($(“NoOp”).all(0, i)); ? The “Done?” node?

@yamikep87

You must post your workflow so we can help you. or DM me and maybe i can help you

@ramin1 I appreciate that. I don’t see a way to DM you when clicking on your username. How do I DM you?

@yamikep87

there is a orange message button

@ramin1 Maybe it’s disabled because I am a new member? I don’t see a message button.

I Sent you a message, but if you can copy and paste your workflow here

Tip: Insert your workflow by clicking ‘</>’ and pasting in the workflow’s code

I tried to move the main logic into a subworkflow but the issue is the same.
Because one item needs to go through the main logic again, this means “Decide Next Lookup” runs a 2nd time so there will be 2 runs for all the downstream nodes, including for the CSV output.
It seems that there is no way to merge all items at the end.
Is that just how n8n is intended to work?

I was able to fix my issue by breaking my original flow down into 2 sub workflows.
One containing the loop and one containing the main logic to do in the loop.
That seemed to do the trick.

Big thanks for @ramin1 for trying to solve my problem. Appreciate it.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.