This formula is quite complex! Let me help you simplify it. The formula appears to be:
- Querying refund data from your Statement sheet
- Grouping and pivoting the data
- Filtering to only show columns containing "order-id"
- Removing the header row
Here are several ways to reduce complexity:
Option 1: Use Named Ranges
First, create a named range for your data:
- Select
Statement!G1:R up to your last row
- Name it something like
StatementData
Then simplify to:
=QUERY(
FILTER(
QUERY(StatementData,"SELECT H, SUM(O) WHERE G MATCHES 'Refund' GROUP BY H,J,R PIVOT M,N LABEL SUM(O) ''",0),
REGEXMATCH(QUERY(StatementData,"SELECT H, SUM(O) WHERE G MATCHES 'Refund' GROUP BY H,J,R PIVOT M,N LIMIT 0"),"order-id")
),
"SELECT * OFFSET 1", 0
)
Option 2: Break into Helper Columns
Create intermediate calculations:
Cell A1 (Base Query):
=QUERY(INDIRECT("Statement!G1:R"&'Last Rows'!$B$2),"SELECT H, SUM(O) WHERE G MATCHES 'Refund' GROUP BY H,J,R PIVOT M,N LABEL SUM(O) ''",0)
Cell A2 (Final Result):
=QUERY(FILTER(A1#,REGEXMATCH(INDEX(A1#,1,),"order-id")),"SELECT * OFFSET 1",0)
Option 3: Simplified Single Formula
If you know which columns will contain "order-id", you can specify them directly:
=QUERY(
INDIRECT("Statement!G1:R"&'Last Rows'!$B$2),
"SELECT H, SUM(O) WHERE G MATCHES 'Refund' AND (M CONTAINS 'order-id' OR N CONTAINS 'order-id') GROUP BY H,J,R PIVOT M,N LABEL SUM(O) '' OFFSET 1",
0
)
Option 4: Using LET Function (if available)
=LET(
data, INDIRECT("Statement!G1:R"&'Last Rows'!$B$2),
pivoted, QUERY(data,"SELECT H, SUM(O) WHERE G MATCHES 'Refund' GROUP BY H,J,R PIVOT M,N LABEL SUM(O) ''",0),
headers, INDEX(pivoted,1,),
QUERY(FILTER(pivoted,REGEXMATCH(headers,"order-id")),"SELECT * OFFSET 1",0)
)
The simplest approach would be Option 3 if you know the column structure, or Option 1 with named ranges for better readability and maintenance.