0

I have two sets of data in excel, set 1 is the raw data, and set 2 is a bridge table. The desired output is also added. How should I prepare for this formula.

set 1:

enter image description here

set 2:

enter image description here

output expected:

enter image description here

cottontail
  • 10,268
  • 18
  • 50
  • 51
Harry
  • 331
  • 1
  • 4
  • 14

2 Answers2

2

Here, a solution that assumes a variable number of headers and no specific pattern in the column names. Assumed no Excel version constraints as per tags listed in the question. In cell H1, put the following formula which spills the entire result all at once:

=LET(in, A1:F5, lk, A8:B12, header, DROP(TAKE(in,1),,1), A, TAKE(lk,,1), 
 B, DROP(lk,,1), data, DROP(in,1,1), REDUCE(TAKE(in,,1), UNIQUE(B),
 LAMBDA(ac,bb, LET(f, FILTER(A, B=bb),values, CHOOSECOLS(data,XMATCH(f, header)),
  sum, MMULT(values, SEQUENCE(ROWS(f),,1,0)), HSTACK(ac, VSTACK(bb, sum))))))

Here it the output: excel output

We use LET function with two input ranges only: in, lk, so the rest of the names defined depend on such range names. It makes the formula easy to maintain and to adapt to your real scenario.

Using DROP and TAKE we extract each portion of the input ranges: header, data, A, B (columns from the second table). We use REDUCE/HSTACK pattern to concatenate the column of the result on each iteration. Check my answer from the question: how to transform a table in Excel from vertical to horizontal but with different length for more information.

We iterate by unique values of B and for each value (bb) we select the column A values (f). We use XMATCH to select the corresponding index columns from header (it doesn't include the date column). We use CHOOSECOOLS to select the corresponding columns from data (values). Now we need to sum by column, and we use MMULT for that. The result is in sum name. Finally, we use HSTACK to concatenate the selected columns one each iteration, including as header the unique values from B.

Note: Instead of MMULT function, you can use the following array function, it is a matter of personal preferences:

BYROW(values, LAMBDA(x, sum(x)))
David Leal
  • 6,373
  • 4
  • 29
  • 56
  • Thank you so much for this help. It's out of my knowledge range, but it does work. Through my test, it seems that if there are null values or unused rows in bridge table, errors will be returned. Is it right? One more thing is that this work is to automate process from set 1 to output. If the column sequence in output has been fixed as "Date, A, C, B, how can I update this formula. – Harry Feb 11 '23 at 01:36
  • @Harry I am glad it works. To prevent empty rows in the bridge table, you can wrap the input with `TOCOL` as follows: `=LET(in, A1:F5, lk, A8:B12, header, DROP(TAKE(in,1),,1), A, TOCOL(TAKE(lk,,1),1),B, TOCOL(DROP(lk,,1),1), data, DROP(in,1,1), REDUCE(TAKE(in,,1), UNIQUE(B), LAMBDA(ac,bb,LET(f, FILTER(A, B=bb), values, CHOOSECOLS(data,XMATCH(f, header)),sum, MMULT(values, SEQUENCE(ROWS(f),,1,0)), HSTACK(ac, VSTACK(bb, sum))))))`, try it and let me know. About your second question, please elaborate more. – David Leal Feb 11 '23 at 01:54
  • @Harry about your second question, the order of the columns in the output is based on how they are presented in the bridge table. You can wrap `UNIQUE(B)` with `SORT` or `SORTBY`, to specify specific order for example or just sort the bridge table input data. Without more details it is difficult to imagine your real scenario, but I guess using `SORT(UNIQUE(B))` ensures,`A, B, C` order for example. – David Leal Feb 11 '23 at 02:14
0

You could try SUMIFS with the wild card character for each row. For example, for the first column, put the following formula and drag it down.

=SUMIFS($B2:$F2,$B$1:$F$1,"=A*")

res

Then do the same thing for the other columns, e.g. for column B:

res2

cottontail
  • 10,268
  • 18
  • 50
  • 51
  • thanks for your answer. Headers here are just examples. The actual headers are much more complicated. And there are lots of columns. This method cannot solve my problem. – Harry Feb 11 '23 at 00:08
  • @Harry, next time, please clarify that in your question upfront, so we can make assumption based on your real problem. – David Leal Feb 11 '23 at 00:52