Search code examples
google-sheetsgoogle-sheets-formulauniqueidentifier

Sum a unique query on multiple pages in Google Sheets


This is a tough one!

Sheet I'm working on: https://docs.google.com/spreadsheets/d/1fIBFq4q179k6-5YuhoFZ-3X84tMMkLmIUeV9-KpjN9I/edit?usp=sharing

On the 'Report' Tab, in cell C22 and D22 I am trying to figure out how to Total the amount that was paid in the months of November and October. Across all tabs (and to include future tabs as they are added).

You can see on the individual stock number pages (Example 'PT-1010!' K14:K) that the payments are already broken down by total for the month. How do I search across all pages, find out if it says 'November-20' and count them all up - and spit out one number?

My brain hurts lol - any help is very much appreciated!!


Solution

  • This may help. (See my sample sheet here)

    enter image description here

    This grabs an array of all of the payments, with a date less than now, using indirect addressing from a list of the stock numbers from the Customers page. I pull this list into a location (U2 in my testing) using this formula:

    =query(Customers!A5:A,"where A<>'' order by A",0)
    

    So that is always a current/dynamic list.

    Then the main formula uses indirect addressing to pull the tab names from this list, and collects all of the data. I added the filters to reduce the number of rows collected, since you have lots of future dates.

    Then I use a query to filter by the desired month, using indirect addressing to the date cells in Report. This allows the exact same formula to be used for both C22 (Nov.) and D22 (Oct.).

    The main formula, calculating the totals paid in each month, is:

    =query({filter(indirect(U3 &"!B7:E"),indirect(U3 &"!B7:B")<Now());
      filter(indirect(U4 &"!B7:E"),indirect(U4 &"!B7:B")<Now());
      filter(indirect(U5 &"!B7:E"),indirect(U5 &"!B7:B")<Now());
      filter(indirect(U6 &"!B7:E"),indirect(U6 &"!B7:B")<Now());
      filter(indirect(U7 &"!B7:E"),indirect(U7 &"!B7:B")<Now());
      filter(indirect(U8 &"!B7:E"),indirect(U8 &"!B7:B")<Now());
      filter(indirect(U9 &"!B7:E"),indirect(U9 &"!B7:B")<Now());
      filter(indirect(U10 &"!B7:E"),indirect(U10 &"!B7:B")<Now());
      filter(indirect(U11 &"!B7:E"),indirect(U11 &"!B7:B")<Now());
      filter(indirect(U12 &"!B7:E"),indirect(U12 &"!B7:B")<Now());
      filter(indirect(U13 &"!B7:E"),indirect(U13 &"!B7:B")<Now());
      filter(indirect(U14 &"!B7:E"),indirect(U14 &"!B7:B")<Now());
      filter(indirect(U15 &"!B7:E"),indirect(U15 &"!B7:B")<Now());
      filter(indirect(U16 &"!B7:E"),indirect(U16 &"!B7:B")<Now());
      filter(indirect(U17 &"!B7:E"),indirect(U17 &"!B7:B")<Now());
      filter(indirect(U18 &"!B7:E"),indirect(U18 &"!B7:B")<Now());
      filter(indirect(U19 &"!B7:E"),indirect(U19 &"!B7:B")<Now())  },
      "select sum(Col4) where Col4 >0 and Col1 >= date '"& text(indirect(address(row()-3,column()  ,4,1)),"yyyy-mm-dd") &"' 
                                      and Col1 <  date '"& text(indirect(address(row()-3,column()-1,4,1)),"yyyy-mm-dd") &"' label sum(Col4) '' ",0)
    

    Using just formulae, instead of a script, I can't think of another way to cycle through multiple tabs. You could repeat the FILTER lines in the formula to handle as many customers as you need. If you are going to have a lot more, then you might need to go to a script. You might need dummy tabs, if you extend the formula now, to handle future customers.

    And, I think some error checking may be required, to handle possible errors in the indirect addressing to tabs. Possibly a test to ensure we aren't pulling a blank value from the list of stock numbers, which equate to the tab names. Or maybe an IFERROR can cover those, one for each FILTER statement.

    Let me know if this helps.

    If you remove the date criteria in the last rows, and change the "sum(Col4)" to "Col1, Col4 order by Col1", you get all the data records with payments (column E in each tab, correct?) and can verify the result.