gSheets: How to use SPLIT in ARRAYFORMULA over columns - google-sheets

For numbers x and y, I have cell data formatted as x#y.
An example row:
| A | B | C | D |
| ------ | ------ | ----- | ------ |
|10#100 | 10#120 | 8#150 | 5#175 |
I want to parse this type of row into two quantities: the sum of the x's and sum of y's.
With my example, I should have two cells:
33 and 545
Basically, I want to SUM the resulting array of SPLIT applied to each cell in A1:D1.
My attempt
=SUM(ARRAYFORMULA(SPLIT(A1:D1, "#")))
Unfortunately, this approach doesn't allow me to specify whether I want x or y (when I call SPLIT) and it seems to be returning x + y, rather than sum(i=1 to 4) x_i.

Try this:
=index(query(arrayformula(split(transpose(A1:D1), "#")),"select sum(Col1),sum(Col2) ",0),2)

Another option:
=ArrayFormula({SUM(INDEX(SPLIT(TRANSPOSE(A1:D1),"#"),0,1)),SUM(INDEX(SPLIT(TRANSPOSE(A1:D1),"#"),0,2))})

use:
=SUMPRODUCT(SPLIT(JOIN("#",A1:D1),"#"),ISEVEN(SEQUENCE(1,COUNTA(A1:D1)*2)-1))
F3= (replace ISEVEN -> ISODD)

use:
=ARRAYFORMULA(QUERY(QUERY(SPLIT(TRANSPOSE(A1:D1); "#");
"sum(Col1),sum(Col2)"); "offset 1"; 0))

Related

How to distribute data in google sheet?

I have a data as follows - ABCDEFGHIJKLMNOPQRSTUVWXYZ
I am coping these data into google sheet - resultant will be - all the data will be copied in one cell.
Cell01
ABCDEFGHIJKLMNOPQRSTUVWXYZ
How i can distribute data in a way that each cell contains one alphabet?
cell01 | cell02 | cell03 | cell04 | cell05 | cell06 |cell07 | cell08 | cell09 | cell10 |cell11
A | B | C | D | E | F | G | H | I | J | H
How do i do that?
With the ABC... string in cell A1, use this in column A of a free row:
=split(regexreplace(A1, "(.)", "$1µ"), "µ")

SUMPRODUCT but only on nonblank cells

Given:
Lp | COL1 | COL 2 | COL 3
ROW 1 | X | | X
ROW 2 | | X | X
ROW 3 | X | X |
ROW 4 | | |
ROW 5 | 1 | 1.5 | 2
ROW 6 | 2 | 1 | 3
I would like to use SUMPRODUCT of Row 1 with Row 5 (and then Row 6) but only in the places where row has X (or rather where it is non empty).
Expected result for Row 1: 1 * 2 + 2 * 3 = 8 (because first and last column is not empty)
Expected result for Row 2: 1.5 * 1 + 2 * 3 = 7.5 (second and last col not empty)
Expected result for Row 3: 1 * 2 + 1.5 * 1 = 3.5 (first and second non empty)
Expected result for Row 4: 0
I appreciate your help.
Use:
=SUMPRODUCT(($B$6:$D$6)*($B$7:$D$7)*(B2:D2<>""))
You can achieve the same thing without SUMPRODUCT.
Create another three columns COL1',2',3', replace
every X with the corresponding product using IF condition.
For example at COL1',ROW1 you write a formula such as =IF(A1="X", A$5\*A$6, 0)
(here A1 is COL1,ROW1)
and drag it to fill COL1',2',3'.
Then you do SUM over COL1',2',3'.

How to translate COUNTIFS formula in ARRAYFORMULA to automatically insert the formula in each row

With my countifs formula in column C I want to auto-number (running total) all occurrences of an identical string in column A (e.g. Apple or Orange) but only if on the same row where the string appears column B is of a certain type, e.g. if in column B the type is of "fruit" in column C auto number all occurrences of an identical string in column A. For each new string which is of type "fruit" start the numbering all over again.
The outcome should be like this:
+---+-----------+-------+---+--+
| | A | B | C | |
+---+-----------+-------+---+--+
| 1 | Apple | Fruit | 1 | |
| 2 | Apple | Fruit | 2 | |
| 3 | Mercedes | Car | 0 | |
| 4 | Mercedes | Car | 0 | |
| 5 | Orange | Fruit | 1 | |
| 6 | Orange | Fruit | 2 | |
| 7 | Apple | Fruit | 3 | |
+---+-----------+-------+---+--+
The formula in column C:
=COUNTIFS($A1:$A$1;A1;$B1:$B$1;"Fruit")
=COUNTIFS($A$1:$A2;A2;$B$1:$B2;"Fruit")
=COUNTIFS($A$1:$A3;A3;$A$1:$A3;"Fruit")
…and so on…
I want to translate this formula into an array formula and put this into the header so the formula will automatically expand.
No matter what I've tried it won't work.
Any help is truly appreciated!
Here's a link to a sheet: [https://docs.google.com/spreadsheets/d/1lgbuLbTSnyKkqr33NdVuDEv5eoXFwatX1rgeF9YpIks/edit?usp=sharing][1]
={"ARRAYFORMULA HERE"; ARRAYFORMULA(IF(LEN(B2:B), IF(B2:B="Fruit",
MMULT(N(ROW(B2:B)>=TRANSPOSE(ROW(B2:B))), N(B2:B="Fruit"))-
HLOOKUP(0, MMULT(N(ROW(B2:B)>TRANSPOSE(ROW(B2:B))), N(B2:B="Fruit")),
MATCH(VLOOKUP(ROW(B2:B), IF(N(B2:B<>B1:B), ROW(B2:B), ), 1, 1),
VLOOKUP(ROW(B2:B), IF(N(B2:B<>B1:B), ROW(B2:B), ), 1, 1), 0), 0), 0), ))}
demo spreadsheet
=ARRAYFORMULA(IF(LEN(B2:B), IF(B2:B="Fruit",
MMULT(N(ROW(B2:B)>=TRANSPOSE(ROW(B2:B))), N(B2:B="Fruit")), 0), ))

Google sheets wrap cell content in other content

Im looking to create a formula in one column that takes the content from the adjacent column and wraps it inside some other content, can anyone help with this?
For example, given:
A | B
1| | someText1
2| | someText2
3| | someText3
4| | someText4
expected outcome content for Col A, after applying appropriate formula:
A | B
1| wrap("someText1") | someText1
2| wrap("someText2") | someText2
3| wrap("someText3") | someText3
4| wrap("someText4") | someText4
I hope this makes sense, any help would be appreciated. Thanks
What i ended up doing: Add a function and applied it to the whole column A
function getAdjacentValue() {
var range = SpreadsheetApp.getActiveRange();
var col = range.getColumn();
var row = range.getRow();
var range2 = SpreadsheetApp.getActiveSheet().getRange(row,col+1);
return 'wrap("'+range2.getValue()+'")';
}
By combining MewX suggestion with arrayformula one can achieve the same for all column, with one formula:
=arrayformula("wrap(""" & B1:B4 & """)")
Explanation: & is the string concatenation operator, quote marks within a string are escaped by doubling them.

How to list most frequent text values within a range?

I'm an intermediate excel user trying to solve an issue that feels a little over my head. Basically, I'm working with a spreadsheet which contains a number of orders associated with customer account #s and which have up to 5 metadata "tags" associated with them. I want to be use that customer account # to pull the 5 most commonly occurring metadata tags in order.
Here is a mock up of the first set of data
Account Number Order Number Metadata
5043 1 A B C D
4350 2 B D
4350 3 B C
5043 4 A D
5043 5 C D
1204 6 A B
5043 7 A D
1204 8 D B
4350 9 B D
5043 10 A C D
and the end result I'm trying to create
Account Number Most Common Tag 2nd 3rd 4th 5th
5043 A C B N/A
4350 B D C N/A N/A
1204 B A C N/A N/A
I was trying to work with the formula suggested here:
=ARRAYFORMULA(INDEX(A1:A7,MATCH(MAX(COUNTIF(A1:A7,A1:A7)),COUNTIF(A1:A7,A1:A7),0)))
But I don't know how to a) use the customer account # as a precondition for counting the text values within the range. b) how to circumvent the fact that the Match forumula only wants to work with a single column of data and c) how to read the 2nd, 3rd, 4th, and 5th most common values from this range.
The way I'm formatting this data isn't set in stone. I suspect the way I'm organizing this information is holding me back from simpler solutions, so any suggestions on re-thinking my organization would be just as helpful as insights on how to create a formula to do this.
Implementing this kind of frequency analysis using built-in functions is likely to be a frustrating exercise. Since you are working with Google Sheets, take advantage of the custom functions, written in JavaScript and placed into a script bound to the sheet (Tools > Script Editor).
The function I wrote for this purpose is below. Entering something like =tagfrequency(A2:G100) in the sheet will produce desired output:
+----------------+-----------------+-----+-----+-----+-----+
| Account Number | Most Common Tag | 2nd | 3rd | 4th | 5th |
| 5043 | D | A | C | B | N/A |
| 4350 | B | D | C | N/A | N/A |
| 1204 | B | A | D | N/A | N/A |
+----------------+-----------------+-----+-----+-----+-----+
Custom function
function tagFrequency(arr) {
var dict = {}; // the object in which to store tag counts
for (var i = 0; i < arr.length; i++) {
var acct = arr[i][0];
if (acct == '') {
continue; // ignore empty rows
}
if (!dict[acct]) {
dict[acct] = {}; // new account number
}
for (var j = 2; j < arr[i].length; j++) {
var tag = arr[i][j];
if (tag) {
if (!dict[acct][tag]) {
dict[acct][tag] = 0; // new tag
}
dict[acct][tag]++; // increment tag count
}
}
}
// end of recording, begin sorting and output
var output = [['Account Number', 'Most Common Tag', '2nd', '3rd', '4th', '5th']];
for (acct in dict) {
var tags = dict[acct];
var row = [acct].concat(Object.keys(tags).sort(function (a,b) {
return (tags[a] < tags[b] ? 1 : (tags[a] > tags[b] ? -1 : (a > b ? 1 : -1)));
})); // sorting by tag count, then tag name
while (row.length < 6) {
row.push('N/A'); // add N/A if needed
}
output.push(row); // add row to output
}
return output;
}
You also could get this report:
Account Number Tag count
1204 B 2
1204 A 1
1204 D 1
4350 B 3
4350 D 2
4350 C 1
5043 D 5
5043 A 4
5043 C 3
5043 B 1
with the formula:
=QUERY(
{TRANSPOSE(SPLIT(JOIN("",ArrayFormula(REPT(FILTER(A2:A,A2:A<>"")&",",5))),",")),
TRANSPOSE(SPLIT(ArrayFormula(CONCATENATE(FILTER(C2:G,A2:A<>"")&" ,")),",")),
TRANSPOSE(SPLIT(rept("1,",counta(A2:A)*5),","))
},
"select Col1, Col2, Count(Col3) where Col2 <>' ' group by Col1, Col2
order by Col1, Count(Col3) desc label Col1 'Account Number', Col2 'Tag'")
The formula will count the number of occurrences of any tag.

Resources