raym01
raym01

Reputation: 73

Google Sheets - Query Multiple Sheets That Have Different Structure

Seeking assistance regarding how to structure a query that will be processing data from multiple sheets (ie tabs), however both sheets have different data structure.

The first query (below) queries a tab that contains all of my expenses itemised. This sums them by month. =query(Expense_Data, "SELECT C, SUM(Q) where T Matches 'Expense' GROUP BY C ORDER BY C desc limit 3 label SUM(Q) 'Expenses'",1)

Example Data Output Below

Date Expenses
01/01/2021 -$1000
01/02/2021 -$1500
01/03/2021 -$1000

What I am seeking is to query another sheet which contains data (located in column G) that I wish to return based upon the date returned from the first query (located in column A), which I will then calculate the difference between. My issue is associating the 2 data sets together. Any support would be greatly appreciated!

Date Expenses Budget Difference
01/01/2021 -$1000 -$2000 -$XXXX
01/02/2021 -$1500 -$1500 -$XXXX
01/03/2021 -$1000 -$1500 -$XXXX

Upvotes: 2

Views: 1561

Answers (1)

player0
player0

Reputation: 1

try:

=QUERY(Expense_Input, 
 "select C,sum(Q) 
  where T matches 'Expense' 
  group by C 
  order by C desc 
  limit 3 
  label sum(Q) 'Expenses', C'Month' 
  format C'mmmm yyyy'", 1)

then:

={"Budget"; ARRAYFORMULA(IFNA(VLOOKUP(TO_TEXT(A13:A), 
 {'Expense Lookup (Monthly)'!C:C&" "&'Expense Lookup (Monthly)'!D:D, 
 SUBSTITUTE('Expense Lookup (Monthly)'!G:G, "$", )*1}, 2, 0)))}

and:

={"Difference"; INDEX(IF(A13:A="",,C13:C-B13:B))}

enter image description here


update

in one go:

=ARRAYFORMULA(QUERY({QUERY(Expense_Input, 
 "select C,sum(Q) 
  where T matches 'Expense' 
  group by C 
  order by C desc 
  limit 3 
  format C 'mmmm yyyy'", 1), IFNA(VLOOKUP(TEXT(INDEX(QUERY(Expense_Input, 
 "select C,sum(Q) 
  where T matches 'Expense' 
  group by C 
  order by C desc 
  limit 3",1),,1), "mmmm yyyy"), 
 {'Expense Lookup (Monthly)'!C:C&" "&'Expense Lookup (Monthly)'!D:D, 
 SUBSTITUTE('Expense Lookup (Monthly)'!G:G, "$", )*1}, 2, 0))}, 
 "select Col1,Col2,Col3,Col3-Col2 
  label Col1'Month',Col2'Expenses',Col3'Budget',Col3-Col2'Difference'"))

enter image description here

Upvotes: 2

Related Questions