Hi @huynhduc
Welcome to KNIME Forum. This was not easy (at least for me ) . I hope it covers all your possible combinations, (I added some extra cases). So check the results with your real world data. See this wf looping_calculations.knwf (82.8 KB)
gr. Hans