Group sum in each row according to some condition set in columns

Sadia1989

New Member
Joined
Oct 3, 2019
Messages
8
I have a data set of roadway and roadway segments vehicle count calculations.
Data set contains:

Road_ID= ID of each road
Seg_num= Index of each segment in each Road_ID
V1= Vehicle counts of type 1 in each Seg_num
V2= Vehicle counts of type 2 in each Seg_num
V3= Vehicle counts of type 3 in each Seg_num
V1cum= Cumulative sum of V1 by Seg_num and Road_ID,
V2cum= Cumulative sum of V2 by Seg_num and Road_ID,
V3cum= Cumulative sum of V3 by Seg_num and Road_ID.
Data frame looks like:

Road_IDSeg_numV1V1cumV2V2cumV3V3cum
11001100
12000100
133535141511
14205572234
15237832526
16280025915
17484328015
18387432116
19390234117
110090034421
111090034627
11217107741027
1133110243128
1140110447230
21220011
22463312
23394746
243122917
2501241329

<tbody>
</tbody>
I need to restart cumulative sum in V1cum, V2cum and V3cum after reaching 5 in all these (V1cum, V2cum and V3cum) columns and also when starting a new Road_ID. And get the cumulative sum values from V1cum, V2cum and V3cum before restarting the cumulative sum and place in each row of that range in separate column named, V1sum, V2sum and V3sum.

My expected result is

Road_IDSeg_numV1V1cumV1sumV2V2cumV2sumV3V3cumV3sum
1100781125006
1200780125006
13353578141525116
1420557872225346
1523787832525266
162290079910
174693370910
1839947711010
1933202291111
11003200294511
111032002961111
11217202079901111
113333226113
114003466233
21229007116
22469337126
23399477466
24333226113
25003466233

<tbody>
</tbody>


Please help me reaching this result. V1cum, V2cum, V3cum columns are for better understanding, but I need only columns V1sum, V2sum and V3sum columns.

Thanks in advance.

Sadia
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
41,785
Office Version
365
Platform
Windows
I'm struggling to understand. :confused:

I need to restart cumulative sum in V1cum, V2cum and V3cum after reaching 5 in all these (V1cum, V2cum and V3cum) columns and also when starting a new Road_ID.
This seems to be the same requirement as in your other thread and, as far as I know, answered there. Your expected results here for V1Cum do not appear to match your description as I cannot see anything restarting after 5.


V1cum= Cumulative sum of V1 by Seg_num and Road_ID,
Even that does not make sense to me. In your sample, for any Road_ID every segment number is different so to me a cumulative sum by segment number would just be the V1 value for every row.


What is the logic for why/when 78 changes to 9 or 9 changes to 20 in the V1sum column?


Clearly I do not understand the problem so at this stage cannot sugget anything.
 

Sadia1989

New Member
Joined
Oct 3, 2019
Messages
8
Thank you so much for responding to my problem.

You are correct that this problem has the same requirement as in my previous thread. However it is more complicated after adding some other conditions. As you noticed that V1cum and V2cum did not restart after reaching 5. The reason behind that is, all the columns which are V1cum, V2cum and V3cum have to reach 5 simultaneously. Therefore, when V1cum>=5 and V2cum>=5 and V1cum>=5, the cumulative sum restarted in each of these columns.

I made mistake writing the definition of V1cum, V2cum and V3cum, the correction would be "not for each segment". V1cum is the cumulative sum of V1 for each roadway.

The logic behind changing the V1sum from 78 to 9 is that, 78 is the sum of the V1 when cumulative sum of V1, cumulative sum of V2 and cumulative sum of V3, all reaches 5. Therefore, V1sum is the sum of V1 column for the first five rows. Similarly V2sum and V3sum are the sum of V2 and V3 columns respectively for the first five rows. Therefore, I get what is the exposure of the vehicle type V1, V2 and V3 on the first 5 segments on the road with Road_ID 1.

I hope this clarifies your understanding.

Thanks,
Sadia
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
41,785
Office Version
365
Platform
Windows
A bit clearer, thanks.
I get a different result to you for the highlighted cells below but the same for all the rest I think. Hopefully they are mistakes at your end (cumulative of x and 0 is x not 0 as it was in D11:D12, J8 etc?). :eek:

Each formula copied down and then each column copied to the corresponding other columns.

Excel Workbook
ABCDEFGHIJK
1Road_IDSeg_numV1V1cumV1sumV2V2cumV2sumV3V3cumV3sum
2110 1125006
31200780125006
413353578141525116
51420557872225346
61523787832525266
7162290079910
8174693370910
91839947711010
101933202291111
1111003200294511
12111032002961111
1311217202079901111
14113333226113
15114033466233
1621229007116
1722469337126
1823399477466
1924333226113
2025033466233
Cumulative Sums (2)
 

Sadia1989

New Member
Joined
Oct 3, 2019
Messages
8
Dear Peter,

Thanks a ton! You solved my problem! I was struggling to solve this in other pieces of software, however failed to complete due to not knowing those very deeply.

In D2 cell formula, I changed "<=5" to "<5", and the whole thing worked like a magic to get the correct output!

I apologies for my mistakes in the cells you mentioned.

For the highlighted cells which have different values than the requirement (<5) as a result of being the last segments of a road. I want to combine those segment/segments with the latest previous segments, counting as a single summing cluster (which might violet the requirement by continuing summation after reaching 5). For example, V1sum is the sum of E19:E20=3 and E16:E18=9 and road finishes after E20. Therefore, I want to sum V1 from C16:C18 and get the sum of E16:E20=12. Now, I am trying to do it in a separate column. I would also appreciate your help in this regard.

Thanks again. You are a life saver!
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
41,785
Office Version
365
Platform
Windows
For the highlighted cells which have different values than the requirement (<5) as a result of being the last segments of a road. I want to combine those segment/segments with the latest previous segments, counting as a single summing cluster (which might violet the requirement by continuing summation after reaching 5). For example, V1sum is the sum of E19:E20=3 and E16:E18=9 and road finishes after E20. Therefore, I want to sum V1 from C16:C18 and get the sum of E16:E20=12. Now, I am trying to do it in a separate column.
I'm afraid I do not understand that at all.
Can you show anything that would help make it clearer?
 

Sadia1989

New Member
Joined
Oct 3, 2019
Messages
8
Hi Peter,

I am trying to clarify what I want to do next.

Suppose, The current output is like


ABCDEFGHIJK
1Road_IDSeg_numV1V1cumV1sumV2V2cumV2sumV3V3cumV3sum
21100781125006
31200780125006
413353578141525116
51420557872225346
61523787832525266
7162290079910
8174693370910
91839947711010
101933202291111
1111003200294511
12111032002961111
1311217202079901111
14113333226113
15114033466233
1621229007116
1722469337126
1823399477466
1924333226113
2025033466233

<tbody>
</tbody>
I want to merge the sum of Road_ID 1, seg_num 14 and 15 to the sum of seg_num 10 to 13 as it does not meet the criterion ">5 or =5". Similarly, in other cases too. Therefore, the corrected output would look like,


ABCDEFGHIJK
1Road_IDSeg_numV1V1cumV1sumV2V2cumV2sumV3V3cumV3sum
21100781125006
31200780125006
413353578141525116
51420557872225346
61523787832525266
7162290079910
8174693370910
91839947711010
1019332322151114
11110032302154514
121110323021561114
13112172023791501114
14113323232111511214
15114023234151521414
162122120013119
172246123313129
182339124713469
1924312122913179
20250121241313299

<tbody>
</tbody>

I put, another column for V1sum_corrected= L2==IF(AND(A3=A2, OR(AND(E2>=5, E3<5), AND(H2>=5, H3<5), AND(K2>=5, K3<5 ))), E3+E2, E3), but it is not working properly. I would appreciate any help.


I am sorry for late reply. I was traveling last two days.

Thanks in advance.

Sadia

<tbody>
</tbody>
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
41,785
Office Version
365
Platform
Windows
I want to merge the sum of Road_ID 1, seg_num 14 and 15 to the sum of seg_num 10 to 13 as it does not meet the criterion ">5 or =5". Similarly, in other cases too. Therefore, the corrected output would look like,
I can see your argument for the end segments for V1 and V3 (green cells) but I cannot see the reasoning fo V2 as the V2cum would again reach the criterion of >=5 in rows 15 and 20 using the previous calculation . Can you clarify further?


Excel Workbook
ABCDEFGHIJK
1Road_IDSeg_numV1V1cumV1sumV2V2cumV2sumV3V3cumV3sum
21100781125006
31200780125006
413353578141525116
51420557872225346
61523787832525266
7162290079910
8174693370910
91839947711010
1019332322151114
11110032302154514
121110323021561114
13112172023791501114
14113323232111511214
15114023234151521414
162122120013119
172246123313129
182339124713469
1924312122913179
20250121241313299
Cumulative Sums (4)
 

Sadia1989

New Member
Joined
Oct 3, 2019
Messages
8
Hi Peter,

Thank you so much for your response.

The requirement for adding the end segment to the previous one is, none of them can be <5. If all the end segments highlighted (green and blue) is greater than or equal to 5 then we can leave those portions as it is. If any of the end segment among V1, V2 and V3 is <5, we need to add all the end segments to their respective previous segment. Therefore, the summing range for V1, V2 and V3 would always be same.

I hope this explanation helps to understand.

Thanks,

Sadia
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
41,785
Office Version
365
Platform
Windows
Can you define what you mean by an "end segment"? Since all the segments have different numbers, is it just the last row for each road?
 

Forum statistics

Threads
1,078,219
Messages
5,338,918
Members
399,267
Latest member
Danielle1017

Some videos you may like

This Week's Hot Topics

Top