Transform one to many data to columns - google-sheets

Given this data:
How do I transform it to look like this:
There are two columns in the data source, key (title) and value (responsibility).
I need to transform it such that we have the key column (title) and then n columns where n is the highest number of value a key has, eg 3 in the picture above. Hence the columns should be:
Title, 1, 2, 3.
The values in each column 1, 2, 3 should be corresponding to values in the original data.
Any combination of formula is welcomed - I believe a combination of Transpose and/or Query (pivot) is appropriate but I cannot put it together.
In case this is too complex we can put an enumeration directly in the data source, but it would be nice to be able to have the formula work without it. Eg:
Example sheet:
https://docs.google.com/spreadsheets/d/1InYZ12VuuaSg0s3fiFTCx8BnwEan5JsqpsNBF973lWc/edit?usp=sharing

try:
=QUERY({A:C},
"select Col1,max(Col3)
where Col1 is not null
group by Col1
pivot Col2", 1)
or:
=ARRAYFORMULA(QUERY({A:A, COUNTIFS(A:A, A:A, ROW(A:A), "<="&ROW(A:A)), B:B},
"select Col1,max(Col3)
where Col1 is not null
group by Col1
pivot Col2", 1))

Related

Efficient way to collate/aggregate specific data in Google Sheets

I'm looking for an efficient way to gather and aggregate some date in Google Sheets. I've been looking at the query function, pivot tables, and Index + Match formulas, but so far I've not found a way that brings me to the result I'm looking for. I have a set of data which looks more or less as follows.
The fields with an X represent irrelevant data which I don't want to show up in my end result. They only serve to illustrate that there are columns of data that I don't want in between the columns of data that I do want. The data in those columns is of varying types and of varying values per type, they are not actually fields with an "X" in it. Only the fields with numbers are of interest along with the related names at the top and left of those. The intent is to create a list that looks more or less like this.
I've highlighted those yellow fields because that data has been aggregated. For example, in the original file field D3 shows a relation between Laura and Pete with the number 1, and field L3 also shows a relation between Laura and Pete, so the number in that field is to be added to the number in the other field resulting in an aggregated total of 2 for that particular combination.
I would really appreciate any suggestions that can help me get to an elegant and efficient solution for this. The only solutions I can come up with would involve multiple "in-between" sheets and there just has to be a better way.
UPDATE:
Solved by applying the solution in player0's answer. I just had to switch around the order of Col1 and Col2 in the formula to get the table sorted the way I needed it. Formula looks like below now. Many thanks to both player0 and Erik Tyler for their efforts.
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col2,Col1,sum(Col3)
where Col2 is not null
and Col3 is not null
group by Col2,Col1
label sum(Col3)''", ))
try:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"where Col3 is not null and Col2 is not null", ))
update:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col1,Col2,sum(Col3)
where Col3 is not null
and Col2 is not null
group by Col1,Col2
label sum(Col3)''", ))
Given your current data set (which only appears to extend to Col N), place the following somewhere to the right of Col N:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(D2:N)),,D1:N1&"~ "&A2:A&"|"&D2:N),A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
It would be better if this were placed in a different sheet from the original data. Supposing that your original data sheet is named Sheet1, place the following version of the above formula into a new sheet:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A:A)))),,Sheet1!D1:1&"~ "&Sheet1!A2:A&"|"&INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A2:A))),Sheet1!A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
This separate-sheet approach and formula allows for the original data to extend indefinitely past Col N.

How do I return a result based on criteria versus totals given?

I have a sheet that looks similar to this:
So column A and column B are combined along with a number in column C. What I am trying to do is add up each value in each column (for example: add each C column for each time "Cat" appears, and "Dog" and "Grass", etc) and then find the value in columns A and B that is the highest, and return that value.
So for example, in my example above, Dog would be the formula result because it's C column totals to 28. Is there a formula (or, most likely, a combination of formulas) that can accomplish this?
Also, is it possible to do the inverse: so it would show the least combined value?
for max:
=INDEX(QUERY({A:A, C:C; B:B, C:C},
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
order by sum(Col2) desc
label sum(Col2)''", 0), 1, 1)
for min:
=INDEX(QUERY({A:A, C:C; B:B, C:C},
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
order by sum(Col2) asc
label sum(Col2)''", 0), 1, 1)

How do I find the most common value in more than one column?

When given multiple columns or rows, is there a formula that would return the most common value in the range? Example:
The formula would return Bird in the example above given columns A and B because Bird shows up 4 times, more than any other value.
try:
=INDEX(QUERY({A:A; B:B},
"select Col1,count(Col1)
group by Col1
order by count(Col1) desc"), 2, 1)

Perform a lookup to find out values from another column and summarises them using Google Sheets

Example data below.
I want to be able to sum the values in Col2 for each occurrence of Col 1, depending on the values in 'Other Cols' that are applied in combination with the value in Col1
Col1------Col2-----Other Cols
A---------40-------other data
A-------------------other data
A-------------------other data
B---------30-------other data
B-------------------other data
C-------------------other data
C-------------------other data
C---------90-------other data
For example, the values in 'other data' might mean the value where Col1 = B is not to included, so the correct outcome is 130 (40+90)
If possible I want to be able to achieve the above in a single Query.
In the real-life data there are over 2,000 rows of data and roughly 200 different values for Col1 (growing in size on a daily basis!!)
What I've been able to do myself!
1) I've created a Query that outputs a row for each valid occurrence of Col1 according to the selection criteria applied to 'Other Data', i.e.
A
C
2) Logically what I want to do next, but I can't do it because I don't know how to, is look back into the original data to find out the Col B values for the values for A and C (i.e. 40 and 90)
3) Then after that, I want to be able to sum the values identified (i.e. 40 + 90), so that in one single Query/cell the answer 130 is returned!!!
Being able to achieve step (2) would be very useful???
Doing (2) + (3) would be perfect!!!
(Note, the value for Col2 is unique to each set of values for Col1 )
=SUM(IFERROR(QUERY(A2:C,
"select sum(B)
where C = 'yes'
group by A
label sum(B)''", 0)))
=ARRAYFORMULA(SUM(QUERY(UNIQUE({A2:A,
IF(C2:C="",, VLOOKUP(ROW(B2:B), IF(
QUERY(A2:C, "select B order by A desc,B desc", 0)<>"", {ROW(B2:B),
QUERY(A2:C, "select B order by A desc,B desc", 0)}), 2)), C2:C}),
"select sum(Col2) where Col3='yes' group by Col1 label sum(Col2)''", 0)))

Count rows in a dataset filtered by multiple conditions

I have a dataset I need to filter in a very specific way and then count the number of rows in it. I've been sitting on this problem for almost three hours but for no use.
I need to perform the following filters on the dataset:
remove all duplicates from only one text-based column (column A), and let the other column reorganize accordingly.
keep only rows where column B is = 'O'
keep only rows where column C is not empty.
but I don't want to store this new filtered dataset anywhere, I just need to know how many rows will it contain. so the filtration and counting of rows need to happen in a single command.
any ideas?
Thanks!
=COUNTA(QUERY(QUERY(QUERY(A1:C,
"select *", 0),
"select Col1, Col2, Col3 where Col3 is not null and Col2 ='O'", 0),
"select count(Col1) group by Col1 label count(Col1)''", 0))

Resources