+ Reply to Thread
Results 1 to 15 of 15

Sum values between zeros in a column

  1. #1
    Registered User
    Join Date
    09-03-2019
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    4

    Sum values between zeros in a column

    In cycling there is a GPX file (more often than not it comes with an altitude parameter). In attached file it is in 1st column (it measures meters above sea level).
    2nd column show climbing, 3rd column is downhill (made using simple IF in Excel)
    I want to calculate the length (sum) of a a current climb or "green area".
    What Excel formula should I use to get the "green" sums (seperately).

    I know this can be done in SQL (by adding some constant and then using simple "group by").
    I can do this in imperative programming language (say C, but it takes time).
    Is there a simple solution in EXCEL?

    I then want to get to know top 10 climbs in the area (where GPX comes from).
    Attached Images Attached Images
    Last edited by Glenn Kennedy; 09-03-2019 at 12:29 PM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: Tricky sums in EXCEL

    You probably can use simple SUMIF
    like
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Assuming the columns on a screenshot are A:C
    Best Regards,

    Kaper

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,078

    Re: Tricky sums in EXCEL

    Assuming your data starts in A1, then in B2 copied down
    =IF(C2=0,A2-A1,0)

  4. #4
    Registered User
    Join Date
    09-03-2019
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    4

    Re: Tricky sums in EXCEL

    Tried both formulas. Didn't do expected result. I attached original file (copy) with columns (A and B).
    If we look at column C.

    Here is manual calculation
    C8 = SUM(B6 + B7+ B8)
    C12 = SUM(B10 + B11 + B12)
    C14 = B14

    How to automate C8, C13, C14
    Attached Files Attached Files

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,078

    Re: Tricky sums in EXCEL

    Unfortunately I misunderstood what you are trying to do and what you need is beyond my knowledge of formulae.

  6. #6
    Registered User
    Join Date
    09-03-2019
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    4

    Re: Tricky sums in EXCEL

    No problem.

    The results will allow me to get TOP 10 climbs (sorted numbers in EXCEL simply as that).
    I will add column C with some magic_number then import all 3 columns in SQL database and
    use select sum... group by magic_number.

  7. #7
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Tricky sums in EXCEL

    I would use a "helper column" (additional cells, which can hidden) to track cumulative sums. In your example, enter the following formula into D2 and copy down the column:

    =IF(B2=0,0,B2+D1)

    Then enter the following formula into C2 and copy down the column:

    =IF(AND(D2>0,B3=0),D2,"")

    -----
    Unrelated observation.... Your use of SUM in SUM(B6 + B7+ B8) is superfluous and serves no useful purpose. Write either SUM(B6,B7,B8) or simply B6+B7+B8.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,814

    Re: Tricky sums in EXCEL

    I don't know my way around SQL or C, so I don't know if this is easier in the spreadsheet programming language or not. It seems like, whatever programming language you choose to use, the program must do the same things (identify each "group", then sum for that group). Ultimately, the approach I would use feels something like the "group by constant" thing you describe in SQL. In a spreadsheet, I would probably go with some variation of:

    1) identify groups. Enter 0 in C2. In C3, use a simple IF() function to increment groups =IF(AND(B2=0,B3>0),C2+1,C2).
    2) Either a pivot table (https://www.excel-easy.com/data-****...ot-tables.html ) or SUMIFS() to sum column B based on the values in column C (https://support.office.com/en-us/art...6-611cebce642b ).
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  9. #9
    Forum Expert 63falcondude's Avatar
    Join Date
    08-22-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    6,266

    Re: Tricky sums in EXCEL

    Try this in C2:

    =IF(OR(B1<>0,B2=0),"",SUM(B2:INDEX(B2:B$1000,MATCH(0,B2:B$1000,0)-1)))

  10. #10
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,705

    Re: Tricky sums in EXCEL

    I put this formula in D2, so that you can compare with the sums you already have in column C:

    =IF(B2=0,"",IF(B3=0,SUM(B$2:B2)-SUM(D$1:D1),""))

    If you want to have it in C2 you just need to change the column in the final SUM:

    =IF(B2=0,"",IF(B3=0,SUM(B$2:B2)-SUM(C$1:C1),""))

    It only produces the SUM at the end of each block (i.e. if the following value is zero) and it works by adding all the numbers in column B up to that point and then subtracts any numbers in column D (or C) up to the previous row. It took some time to calculate in my version, but it seems to produce the correct results.

    Hope this helps.

    Pete

  11. #11
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,986

    Re: Tricky sums in EXCEL

    Your thread title is kinda weak. We really expect them to resemble the sort of thing you'd use if you were using Google. Since you're new here, I have changed it for you. Please bear this in mind next time that you start a thread
    Last edited by Glenn Kennedy; 09-03-2019 at 12:30 PM.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  12. #12
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,705

    Re: Tricky sums in EXCEL

    I keep forgetting that you are a Mod now, Glenn.

    Pete

  13. #13
    Registered User
    Join Date
    09-03-2019
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    4

    Re: Tricky sums in EXCEL

    Quote Originally Posted by joeu2004 View Post
    I would use a "helper column" (additional cells, which can hidden) to track cumulative sums. In your example, enter the following formula into D2 and copy down the column:

    =IF(B2=0,0,B2+D1)

    Then enter the following formula into C2 and copy down the column:

    =IF(AND(D2>0,B3=0),D2,"")

    -----
    Unrelated observation.... Your use of SUM in SUM(B6 + B7+ B8) is superfluous and serves no useful purpose. Write either SUM(B6,B7,B8) or simply B6+B7+B8.

    Worked perfectly!
    Last edited by ghr2; 09-03-2019 at 01:31 PM.

  14. #14
    Forum Expert 63falcondude's Avatar
    Join Date
    08-22-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    6,266

    Re: Sum values between zeros in a column

    Glad we could help.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  15. #15
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,986

    Re: Tricky sums in EXCEL

    Very often... so do I!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Tricky Excel Problem!!!!
    By RF5 in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 09-27-2018, 01:06 PM
  2. Very Tricky problem in EXCEL
    By sramidi in forum Excel General
    Replies: 1
    Last Post: 01-23-2012, 05:31 AM
  3. Tricky Excel mailout problem...
    By Whizzo in forum Excel General
    Replies: 1
    Last Post: 04-07-2009, 08:37 AM
  4. Tricky Excel Code
    By theoffice in forum Excel General
    Replies: 5
    Last Post: 06-15-2007, 11:24 AM
  5. Tricky Tricky episode 2!!!
    By mhax in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-21-2006, 10:15 AM
  6. tricky excel formula question
    By tdols in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-15-2006, 06:50 AM
  7. OK, a really tricky one now for Excel gurus!
    By tim :/ in forum Excel General
    Replies: 2
    Last Post: 10-25-2005, 08:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1