HomeGoogle DocsSpreadsheetHow to Get the Row Where Target Sum Is Reached in Google...

How to Get the Row Where Target Sum Is Reached in Google Sheets

If you’re working with running totals—like daily sales, expenses, or donations—you might want to find the exact row where your target is first reached. That’s where this little trick using SCAN and XMATCH comes in handy.

You can use it to return the target sum reached row in Google Sheets, either as the actual row number or just the relative position within your range.

Why Find the Target Sum Reached Row?

Let’s say you’re tracking sales day by day and want to know when you hit your monthly target. Once the running total reaches or exceeds your goal, you might want to:

  • Pull related data from that row (like the date)
  • Highlight it in a chart
  • Or trigger other calculations

To do that, you first need to find the row where the target sum is reached.

Sample Setup

You have:

  • Dates in column A → A2:A
  • Sales volumes in column B → B2:B
  • A target amount (say 200) in cell D2

Now let’s find the row where the running total reaches or passes that target.

Formula to Find Target Sum Reached Row in Google Sheets

Here’s the formula:

=XMATCH(D2, SCAN(0, B2:B, LAMBDA(acc, val, acc + val)), 1) + ROW(B2) - 1

How This Formula Works

Let’s break it down step by step:

  • SCAN(0, B2:B, LAMBDA(acc, val, acc + val))
    This part creates a running total of the values in column B. It accumulates each value on top of the previous total, row by row.
  • XMATCH(D2, ..., 1)
    XMATCH searches the running total for the first value that’s greater than or equal to the target in cell D2. The 1 at the end tells it to find the smallest value that is greater than or equal to the target.
  • +ROW(B2) - 1
    Since XMATCH returns the position within the range (starting from 1), we add ROW(B2) - 1 to convert it into the actual row number on the sheet.

If you only need the position within the range and not the actual sheet row number, you can remove the last part:

=XMATCH(D2, SCAN(0, B2:B, LAMBDA(acc, val, acc+val)), 1)

Example

SCAN and XMATCH formula returning the target sum reached row in Google Sheets with example data

If you enter 200 in cell D2, the formula will return row 9, which is where the running total first hits or passes the target.

If you remove +ROW(B2)-1, you’ll get 8, which is the position in the range B2:B.

What If the Target Isn’t Reached?

If the values in B2:B never add up to the target in D2, the formula will return #N/A.

Bonus: Lookup a Value When the Target Is Reached

Finding the row is often just the first step. You might also want to return a value from another column, like the date or a label, once the target is hit.

Instead of using the row number with INDEX, you can use XLOOKUP directly with the running total.

Get the Date When the Target Is Reached

=XLOOKUP(D2, SCAN(0, B2:B, LAMBDA(acc, val, acc + val)), A2:A, "Target is not reached!", 1)

This pulls the corresponding date from column A.

Get the Reached Total Itself

You can also return the cumulative total when the target is hit:

=XLOOKUP(D2, SCAN(0, B2:B, LAMBDA(acc, val, acc + val)), SCAN(0, B2:B, LAMBDA(acc, val, acc + val)), "Target is not reached!", 1)

To make it cleaner and avoid repeating the SCAN function, wrap it in LET:

=LET(rs, SCAN(0, B2:B, LAMBDA(acc, val, acc + val)), XLOOKUP(D2, rs, rs, "Target is not reached!", 1))

This is more efficient and easier to maintain.

Prashanth K V
Prashanth K V
Your Trusted Google Sheets and Excel Expert Prashanth K V is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Top Discussions

More like this

How to Use the SHEET and SHEETS Functions in Google Sheets

The SHEET and SHEETS functions let you retrieve information about worksheets in a Google...

How to Create a Self-Healing Table of Contents in Google Sheets

A table of contents makes navigating large Google Sheets workbooks much easier. However, a...

Sort a Tab Name List Dynamically by Workbook Order in Google Sheets

When your workbook contains many sheets (tabs), you may create a table of contents...

7 COMMENTS

  1. Thanks, Prashanth, that works. The last problem is when the target sum isn’t reached.

    Is there a way to pick a value to return in this case?

    It currently attempts to output an array: “Array result was not expanded because it would overwrite data in XX.”

  2. Thanks Prashanth. This article is almost exactly what I need.

    I have one more contstraint. I have an additional comment that is either blank, or has an “x”. I would like to limit the rows being added together to be the ones where that entry has an “x” in this additional column.

    I tried changing SUMIF to SUMIFS (as well as moving the 3rd argument to be the 1st argument), but that resulted in the error message “Array arguments to SUMIFS are of different size.” Is it possible to add this additional constraint?

    • HI, Brad Garcia,

      Thanks for your feedback. The SUMIFS won’t work that way. We can use a LAMBDA formula to replace the running total SUMIF.

      Also, we require replacing MATCH with XMATCH.

      My sample data is as follows.

      A2:A – Dates
      B2:B – Amount
      C2:C – Contains “x” or blank.
      F2 – Lookup amount (target sum).

      Formula:

      =index(A2:A,
      xmatch(F2,
      if(C2:C="x",scan(0,if(C2:C="x",B2:B,0),lambda(a,v,a+v))),
      1))

      Resources:
      1. Conditional Running Total.
      2. XMATCH.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.