Google Sheets REDUCE Formula Not Sorting Kit Items Correctly

I’m working with an inventory spreadsheet called “INVENTORY” that tracks equipment by location and reference numbers. I created a summary report that pulls data using this formula:

=reduce(tocol(,1),unique(filter(INVENTORY!M:M,INVENTORY!N:N=C4)),lambda(x,y,ifna(vstack(x,regexreplace(y,"(^.*? - )",),filter(INVENTORY!G:K,INVENTORY!M:M=y)))))

The issue is that equipment belonging to sets gets placed at the end of the list rather than appearing in the correct sequence. When I run the formula, individual items show up first in order, but then all the set items get dumped at the bottom instead of being mixed in where they belong based on their original position.

I’ve tried modifying different sections of the formula but haven’t been able to fix the ordering problem. The set items should appear inline with the regular items based on their original sequence in the source data.

Any suggestions on how to adjust this formula to maintain proper ordering?

The ordering mess happens because REDUCE just tacks on each filtered group one after another instead of keeping the original row positions from your inventory sheet. I ran into this exact thing building equipment reports. You need to lock in the original row order before doing any grouping. Add a sort based on the original row numbers from your INVENTORY sheet. Maybe create a helper column that grabs the original row position first, then work that into your REDUCE lambda. VSTACK should follow the original order when it combines everything. Or just do it in two steps - let REDUCE collect all the data first, then slap a SORT function on the whole result using your original inventory sequence. Keeps your filtering logic intact but fixes the ordering mess at the end.

You’re hitting a classic spreadsheet wall here. REDUCE processes items one by one but doesn’t keep the original row order when you’re filtering and stacking data like this.

I’ve run into this exact issue tons of times at work. The problem is Google Sheets formulas get messy fast when you need proper ordering plus complex data transformations.

Skip the formula wrestling match. Set up an automated workflow that grabs your inventory data, sorts it right, and updates your summary report. You’ll get consistent ordering without the headache.

Create a scenario that reads from your sheets, processes everything with proper sorting logic, then writes back the correctly ordered results. It runs automatically when your inventory changes, so your reports stay accurate and properly sequenced.

Way more reliable than trying to hack complex formulas together, especially when you need to keep data relationships and ordering intact.

The Problem:

You’re encountering an ordering issue in your Google Sheets summary report. The formula you’re using correctly pulls data from your “INVENTORY” sheet, but items belonging to sets appear at the end of the list instead of maintaining their original order. Individual items appear correctly, but set items are grouped incorrectly at the bottom.

:thinking: Understanding the “Why” (The Root Cause):

The core issue lies in how your formula handles filtering and combining data. The UNIQUE function, used within your REDUCE function, disrupts the original row order from your “INVENTORY” sheet. UNIQUE processes and re-orders the unique reference numbers before the REDUCE function can work with them. The REDUCE function then iterates through these re-ordered unique values, combining data one by one without regard for the original row order. Each LAMBDA function iteration processes a single unique value independently, losing the positional context from your original inventory data. Consequently, the final output using vstack doesn’t reflect the original sequence. The filtering and stacking actions break the original sequence, causing the set items to cluster together at the end instead of being integrated according to their initial position.

:gear: Step-by-Step Guide:

Step 1: Preserve Original Row Order:

The most effective solution is to pre-process your data to maintain the original row order before applying the UNIQUE function. Add a helper column to your “INVENTORY” sheet (let’s say column ‘L’) to capture the original row number using the formula =ROW(). This formula should be placed in cell L2 and then automatically copied into subsequent cells until the end of your data. This way, each row keeps a record of its original position.

Step 2: Modify your REDUCE Formula:

Modify your existing REDUCE formula to incorporate this new row number information. The altered formula should look something like this:

=SORT(
  REDUCE(
    tocol(,1),
    UNIQUE(FILTER(INVENTORY!M:M,INVENTORY!N:N=C4)),
    LAMBDA(
      x,
      y,
      IFNA(
        VSTACK(
          x,
          {
            INDEX(INVENTORY!L:L,MATCH(y,INVENTORY!M:M,0)),
            REGEXREPLACE(y,"(^.*? - )",""),
            FILTER(INVENTORY!G:K,INVENTORY!M:M=y)
          }
        ),
        x
      )
    )
  ),
  1,TRUE
)

This modified formula now includes the original row number (INDEX(INVENTORY!L:L,MATCH(y,INVENTORY!M:M,0))) within each row, ensuring the original row order is maintained during the REDUCE and VSTACK operations. The entire result is then sorted (using SORT()) using the original row order captured in the first column, using that column as the sorting key. This ensures the correct sequence of items in your summary report.

Step 3: Test Thoroughly:

After implementing these changes, thoroughly test your formula with different scenarios, ensuring that the items, including those belonging to sets, are now correctly ordered in your summary report.

:mag: Common Pitfalls & What to Check Next:

  • Helper Column Accuracy: Verify that your helper column (column L) correctly assigns row numbers to each row in your “INVENTORY” sheet. Check for any inconsistencies or errors in the row numbering.
  • Formula Errors: Carefully double-check the updated REDUCE formula for any syntax errors. Google Sheets is unforgiving when it comes to parenthesis matching. If you still encounter issues, check cell references and function arguments within your formula. Make sure all INVENTORY! references align correctly to your sheet’s structure.
  • Data Consistency: Ensure your “INVENTORY” sheet is free of inconsistencies or errors that could impact filtering and sorting. Look for any duplicated values in column M that might interfere with the MATCH function’s ability to locate the correct row number.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.