Sum horizontally from left to right until I meet a 0

I need to sum starting from left only if the most left double column is >0. If the condition is met I need to move right and stop only when I find a 0.
In the picture below I show what I would like to sum adding the total in a new column.
I don’t know where to start. The number of column can vary in time,

Hi @Paolodam

See this wf sum_horizontally.knwf (30.3 KB).

gr. Hans


Hello @Paolodam
If I understood properly the logic of the challenge, and your size data affords row looping processing. This can be a valid solution:

20230317_sum_horizontally_conditional_v0.knwf (70.7 KB)

Using the sample data from previous @HansS ’ post.



The number of rows as variable fix the length of the listed values (number of columns), by controlling the window length in moving aggregation node.


Hello Hans, thank you for your suggestion. It works but it evaluates “0” only on first column. I have to stop the sum also if I encounter another “0” moving right. If i add a 1 in your table the result I want is still 3 and not 4, because in column 2 there’s a “0”. The solution of @gonhaddock works just fine.
Immagine 2023-03-20 083419

1 Like

Thank you a lot. I see your wf works perrfectly, I have understood the logic but I’m not sure exactley how the moving aggragation works.

1 Like

Hello @Paolodam and sorry for my late answer.

The Moving Aggregator is basically a trimmed operator. It operates back or forward, in a predefined moving window length.

In your solution the window length is controlled with a variable (the total table length from Extract Table Dimensions) and the operator is ‘Product’. This means it will operate the accumulated product for all the list array (column); therefore when there is a 0 in the array, the result for the rest of the array will be 0.

Afterwards we convert this string to a logic factor ‘0’ == FALSE (0) and ‘<> 0’ == TRUE (1) aiming to process the conditional values table; this can be improved in the workflow because as all the samples were positives, then the rule TRUE is defined from ‘>0’ .

I hope this helps on clarifying


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