Subtract first non-zero row while non-zero in column

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,
Kicking myself that I haven't figured out this yet on my own.
See the mini-sheet below and here is what I'm trying to do:
  • The Aton_Delta (C) column is data that comes in, from that I have a helper column that has a "1" in it if the C column data is non-zero
  • The DeltaFromStart (D) column is the result I am trying to get.
  • The value in column D should use the first occurrence of a non-zero in column C (12 for example) and subtract it from each subsequent non-zero value in the C column until a zero is encountered again.
  • It then starts over when another non-zero delta value is encountered in column C.
I've gotten close using OFFSET or counting with the helper row, but for some reason I can't quite get it right. I can't filter, sequence or anything like that without causing bigger problems elsewhere.
Thank you for your help.

Book2
ABCDEFGHIJ
1HelperRun DateAton_∆∆FromStartDesired result in column D
26/13/2022000
36/12/2022000
46/11/2022000
516/10/20221212First non-zero delta is 12
616/9/2022164=16-12
716/8/20222513=25-12
816/7/20223220=32-12
916/6/20224-8=4-12
106/5/202200Delta is zero again so delta from start should = 0
116/4/202200Delta is zero again so delta from start should = 0
126/3/202200Delta is zero again so delta from start should = 0
136/2/202200Delta is zero again so delta from start should = 0
146/1/202200Delta is zero again so delta from start should = 0
155/31/202200Delta is zero again so delta from start should = 0
165/30/202200Delta is zero again so delta from start should = 0
1715/29/202266First non-zero delta is 6
1815/28/20221711=17-6
1915/27/20223529=35-6
2015/26/20223832=38-6
2115/25/20224135=41-6
2215/24/2022126=12-6
2315/23/20221812=18-6
245/22/202200Delta is zero again so delta from start should = 0
255/21/202200Delta is zero again so delta from start should = 0
265/20/202200Delta is zero again so delta from start should = 0
275/19/202200Delta is zero again so delta from start should = 0
285/18/202200Delta is zero again so delta from start should = 0
295/17/202200Delta is zero again so delta from start should = 0
305/16/202200Delta is zero again so delta from start should = 0
315/15/202200Delta is zero again so delta from start should = 0
325/14/202200Delta is zero again so delta from start should = 0
335/13/202200Delta is zero again so delta from start should = 0
Sheet1
Cell Formulas
RangeFormula
D6,D18D6=C6-C5
D7,D19D7=C7-C5
D8,D20D8=C8-C5
D9,D21D9=C9-C5
D22D22=C22-C17
D23D23=C23-C17
B2B2=TODAY()
B3:B33B3=B2-1
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Maybe something like this

Cell Formulas
RangeFormula
B2B2=TODAY()
B3:B33B3=B2-1
D2:D33D2=IF(C2=0,0,IF(N(C1)=0,C2,C2-LOOKUP(2,1/(C$1:C1=0),C$2:C2)))


M.
 
Upvote 0
try this for another option:
Cell Formulas
RangeFormula
A2A2=TODAY()
A3:A33A3=A2-1
C2:C33C2=IF(B2=0,0,B2-INDEX(B$2:B2,XMATCH(0,B$2:B2,0,-1)+1))
 
Upvote 0
One more:

Book3
ABCD
1HelperRun DateAton_∆∆FromStart
26/13/202200
36/12/202200
46/11/202200
516/10/20221212
616/9/2022164
716/8/20222513
816/7/20223220
916/6/20224-8
106/5/202200
116/4/202200
126/3/202200
136/2/202200
146/1/202200
155/31/202200
165/30/202200
1715/29/202266
1815/28/20221711
1915/27/20223529
2015/26/20223832
2115/25/20224135
2215/24/2022126
2315/23/20221812
245/22/202200
255/21/202200
265/20/202200
275/19/202200
285/18/202200
295/17/202200
305/16/202200
315/15/202200
325/14/202200
335/13/202200
Sheet4
Cell Formulas
RangeFormula
D2:D33D2=IF(C2=0,0,IF(N(C1)=0,C2,C2-IF(C1=D1,C1,C1-D1)))
 
Upvote 0
Solution
Maybe something like this

Cell Formulas
RangeFormula
B2B2=TODAY()
B3:B33B3=B2-1
D2:D33D2=IF(C2=0,0,IF(N(C1)=0,C2,C2-LOOKUP(2,1/(C$1:C1=0),C$2:C2)))


M.

Thank you Marcelo, this works perfectly and thanks to KRice and Eric as those are alternatives I will bank away for other potential uses.
KRice's suggestion is the one I understand the easiest since I don't yet know what "N" is as in IF(N(C1)=0, but I will go look it up. Thanks guys!
 
Upvote 0
Thank you Marcelo,

You're welcome.
But... reviewing the problem I noticed that my formula doesn't work in certain scenarios.
I could adapt it for such situations, but I would use the formula suggested by Eric in post #4 - simpler and more robust.

M.
 
Upvote 0
I just noticed a problem with mine too. I was missing the correct result for the first non-zero that occurs after a 0 in the Aton column. I've needed to added a nested IF as well to handle that:
Cell Formulas
RangeFormula
A2A2=TODAY()
A3:A33A3=A2-1
C2:C33C2=IF(B2=0,0,IF(B1=0,B2,B2-INDEX(B$2:B2,XMATCH(0,B$2:B2,0,-1)+1)))
 
Upvote 0
I just noticed a problem with mine too. I was missing the correct result for the first non-zero that occurs after a 0 in the Aton column. I've needed to added a nested IF as well to handle that:
Cell Formulas
RangeFormula
A2A2=TODAY()
A3:A33A3=A2-1
C2:C33C2=IF(B2=0,0,IF(B1=0,B2,B2-INDEX(B$2:B2,XMATCH(0,B$2:B2,0,-1)+1)))
Thanks all, I have a working solution from your ideas and some alternatives.
 
Upvote 0
I don't yet know what "N" is as in IF(N(C1)=0, but I will go look it up
In case you haven't found it yet, N() simply makes sure that the value is numeric. It's an old function left over from the early days of Excel that was used to ensure compatibility with other spreadsheet programs. My formula checks the previous row to see if column C is 0 or not. To avoid have a special check to see if the previous row is the header row, I use N(C1). If C1 is a number, nothing changes, if it's the header N(C1) turns it to 0 and I don't have to do anything else special.

Anyway, glad we could help!
 
Upvote 0
In case you haven't found it yet, N() simply makes sure that the value is numeric. It's an old function left over from the early days of Excel that was used to ensure compatibility with other spreadsheet programs. My formula checks the previous row to see if column C is 0 or not. To avoid have a special check to see if the previous row is the header row, I use N(C1). If C1 is a number, nothing changes, if it's the header N(C1) turns it to 0 and I don't have to do anything else special.

Anyway, glad we could help!
Ah, now N makes sense in context of header. thanks!
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,979
Members
448,934
Latest member
audette89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top