Formula to use to achieve the header into each row [duplicate] - google-sheets

This question already has answers here:
Excel/Sheets combine row column content and find respecting value
(2 answers)
Closed 1 year ago.
I need to convert some data in my Google sheet. Attached is the screenshot on how I currently have the data and how I am looking to format the data into.

Derived from MattKing's answer, I've added transpose in conjunction with some manipulation on the concatenation part to follow the type of sorting your Required Data Format had. This should give you the same output you provided above.
Sample Data:
Formula:
=arrayformula(split(flatten(transpose(A2:A6&" "&B1:D1)&"|"&transpose(B2:D6)),"|"))
Where:
A2:A6 is the range of your project names
B1:D1 is the range of your headers
B2:D6 is the range of your dates
Result:
EDIT:
If you are expecting blank cells in your dates such as the sample below (as pgSystemTester mentioned in the comments section):
You need to add query and exclude those rows that doesn't have dates.
=arrayformula(query({split(flatten(transpose(A2:A6&" "&B1:D1)&"|"&transpose(B2:D6)),"|")}, "where Col2 is not null"))

Just to give another option, and spurred on by this (likely repeat) question
=LET(x, $I$2:$L$4,
myrows, ROWS(x),
mycols, COLUMNS(x),
mycount, SEQUENCE(myrows*mycols),
car, $H$2:$H$4, color, $I$1:$L$1,
mylist, car&" "&color,
mycolumn, INDEX(mylist, CEILING(mycount/mycols,1), IF(MOD(mycount,mycols)=0, mycols,MOD(mycount,mycols))),
mydata, INDEX(x, CEILING(mycount/mycols,1), IF(MOD(mycount,mycols)=0, mycols,MOD(mycount,mycols))),
IF(SEQUENCE(1,2)=1, mycolumn, mydata))

assuming your data starts in cell Sheet1!A2 and extends indefinitely down and indefinitely over, try this:
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(Sheet1!A3:A&" "&Sheet1!B2:2&"|"&OFFSET(Sheet1!B3,,,9^9,9^9)),"|",0,0),"where Col2 is not null",0))

You can see a sample sheet here, with several of answers illustrated on each tab.
Formula With No App Scripts
This is an update and includes a formula that was stolen from a different answer that appears to nail it. (Player() is officially the Jack Bauer of Spreadsheets... don't ask how he gets things done!).
=INDEX(QUERY(SPLIT(FLATTEN(IF(B2:E="",,A2:A&" "&B1:E1&"×"&B2:E)), "×"),
"where Col2 is not null"))
Make sure this is placed off to the right so that it doesn't spill into itself.
My Original App Scripts Solution
This is honestly not needed any more, but I worked on it for half an hour and maybe someone might find it helpful if they have trouble sleeping.
/**
* Creates 2 Columns of Data
*
* #param {range} theRangeValues The Table Range.
* #return The two columns of data with all combinations based on rows and columns
* #customfunction
*/
function buildTwoColumnsData(theRangeValues) {
var allValues = [];
var i = 1;
var cCell = theRangeValues[0][i];
while (cCell != '' && i < theRangeValues[0].length) {
var theEnd = cCell;
var g = 1;
var rCell = theRangeValues[g][0];
while (rCell != '' && g < theRangeValues.length) {
allValues.push([rCell + " " + theEnd, theRangeValues[g][i]])
g++;
if (g < theRangeValues.length) {
rCell = theRangeValues[g][0];
}
}
i++;
if (i < theRangeValues[0].length) {
cCell = theRangeValues[0][i];
}
}
return allValues;
}

Related

Arrayformula to preserve a running/cumulative balance when inserting new rows

The top right cell (Natwest) is a list from a range using data validation.
The Opening Balance 1,000.00 is sourced from another sheet using a lookup formula.
Using simple if statements, the cumulative balance is then produced - according to the Amount column and whether the Natwest account occurs in the Dr(+) or Cr (-) column
i.e. =if(B4=$D$1,D3+A4,if(C4=$D$1,D3-A4,D3)) and copied down.
Natwest
Amount Dr Cr Balance
1,000.00
100.00 Natwest Account 1 1,100.00
200.00 Account 2 Natwest 900.00
400.00 Natwest Account 1 1,300.00
It works fine, except that when a new row is inserted, the if statement formula is not copied into the new row.
I am looking for an arrayformula solution (or other formula inside the cell solution), so that the Cumulative Balance still works, but doesn't need to be copied into column D new row - when a new row(s) are inserted.
(I don't mind the Natwest (drop down from the list) or the Opening Balance 1,000.00 to be moved elsewhere if required for a solution.)
Thanks for your help.
Something adding up in between the same range of the arrayformula is always going to be tricky with circular dependency. I suggest to get the initial value and add it the SUMIF of second column and substract the SUMIF of second column up to each value. With BYROW you can do it like this:
=BYROW(A4:A,LAMBDA(each,SUMIF(INDIRECT("B4:B"&ROW(each)),D1,A4:each)-SUMIF(INDIRECT("C4:C"&ROW(each)),D1,A4:each)+D3))
Alternate solution:
You can use this custom function from AppScript for automatically calculating cumulative balance
Code:
function customFunction(startnum, key, range) {
var res = [];
var current = startnum;
range.forEach((x) => {
res.push(x.map((y, index) => {
return y == key && index == 1 ? current = (current + x[0]) : (y == key && index == 2 ? current = (current - x[0]) : null)
}).filter(c => c))
})
return res;
}
Custom Function Parameters:
=customFunction(startnum, key, range)
startnum = opening balance
key = Account name
range = cell range
Sample output:
=customFunction(D3,D1,A4:C)

How to concatenate ranges using formulas, even when there are blanks to be considered on Google Sheets?

The problem consists of concatenating multiple horizontal ranges containing product1, brand1, price1, product2, brand2, price2 and so on, even when the cells are blank.
Here's a link to a working example:
https://docs.google.com/spreadsheets/d/1poZW2JAEu419BnOzXVe7-Sq777moXvH_o16NKZtkjAE/edit?usp=sharing
I have tried:
=transpose(sheet1A2:C),transpose(sheet1D:F)
{sheet1A2:C,sheet1D:F}
I have also tried filtering it, but none has worked so far.
Any help will be appreciated.
Cheers,
Antonio
use:
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(A3:A4&"×"&
FILTER(A3:4; REGEXMATCH(A2:2; "Product.*"))&"×"&
FILTER(A3:4; REGEXMATCH(A2:2; "Brand.*"))&"×"&
FILTER(A3:4; REGEXMATCH(A2:2; "Price.*"))); "×");
"where Col2 is not null"))
update:
=ARRAYFORMULA(SPLIT(FLATTEN(A3:A4&"×"&
FILTER(A3:4; REGEXMATCH(A2:2; "Product.*"))&"×"&
FILTER(A3:4; REGEXMATCH(A2:2; "Brand.*"))&"×"&
FILTER(A3:4; REGEXMATCH(A2:2; "Price.*"))); "×"))
Alternative Solution:
You can also try this bound script below and add it to your sheet for a simpler function use:
function transposeBy3(data) {
var oneData = []
var final = [];
var start = 0;
var end = 3;
data.forEach(raw => {
raw.forEach(value => {
oneData.push(value);
})
})
if(oneData.length%3 == 0){
for(y=0; y<oneData.length; y++){
final.push(oneData.slice(start, end));
start = start + 3;
end = end + 3;
}
}
return final;
}
Sample
After saving the bound script on your spreadsheet file, on cell B11, you can put this custom function =transposeBy3(B3:J4) as seen below
I will answer according to what you have shared, with the assumption that your headers will not be "Product1" or "Brand1" but rather actual names of products and brands which are not similar to one another.
First, never put dissimilar charts or results below a working database that will grow over time. For this reason, I have added two new sheets to your spreadsheet. The first is a duplicate of your first sheet ("Página1 - Erik") which only has your database headers and data. The results formula is then in another sheet ("Erik Help"), in cell A2. This formula refers to the cleaned sheet "Página1 - Erik":
=ArrayFormula(SPLIT(FLATTEN(FILTER('Página1 - Erik'!A3:A&"|"&FILTER('Página1 - Erik'!B3:J;MOD(COLUMN('Página1 - Erik'!B3:J3)-2;3)=0)&"|"&FILTER('Página1 - Erik'!B3:J;MOD(COLUMN('Página1'!B3:J3)-3;3)=0)&"|"&FILTER('Página1 - Erik'!B3:J;MOD(COLUMN('Página1 - Erik'!B3:J3)-4;3)=0);'Página1 - Erik'!A3:A<>""));"|";1;0))
It is a similar approach to what player0 offered, but instead of relying on similarity of header text, it relies on column patterns.
I will leave it to you to modify the formula as necessary to apply to your actual data set when the time comes.

Summarizing data from multiple sheets

I have a google sheet like this example to track scores for disc golf:
https://docs.google.com/spreadsheets/d/1uxDFXg2kivZWKICeVklugyXH1OWqsq_s5qXZYzgHkt8/edit?usp=sharing
It works great for tracking day to day scores but it would be really awesome to have a single sheet at the beginning that could say everyones total scores that they have gotten. Also note that the names may not be the same in each sheet.
So in this example I would want to have a new sheet that would automatically calculate the scores from the other sheets and show:
Mike 67,71,65
George 83,70
Phillip 79,72,65
John 66,71
Henry 69
I am very unfamiliar with excel formulas and have been struggling to get this started. Any help would be greatly appreciated.
You can use a Google Apps Script to accomplish what you are looking for. The idea of the code is that it will iterate over every Sheet in your Spreadsheet, gather all the players and all their values, and finally create a summary and put it into the "Summary" sheet (that sheet must exist in your Spreadsheet, with strictly the same name):
function updateSummary() {
var sheets = SpreadsheetApp.getActive().getSheets();
var summarySheet = SpreadsheetApp.getActive().getSheetByName('Summary');
var allScores = {};
for (var i=0; i<sheets.length; i++) {
if (sheets[i].getName() == 'Summary') continue;
var nColumns = sheets[i].getLastColumn();
var names = sheets[i].getRange(1, 1, 1, nColumns).getValues()[0];
var scores = sheets[i].getRange(20, 1, 1, nColumns).getValues()[0];
for (var j=0; j<nColumns; j++) {
var currentName = names[j];
var currentScore = scores[j];
if (!allScores.hasOwnProperty(currentName))
allScores[currentName] = [];
allScores[currentName].push(currentScore);
}
}
summarySheet.clear();
for (var key in allScores) {
var row = [key].concat(allScores[key]);
summarySheet.appendRow(row);
}
}
This will create, with the data given, the following data in the "Summary" Sheet:
Instead, if you prefer to have two columns as you described in your question (with the second one holding every score separated by commas), you would simply need to replace the last for-loop in the code above for the following one:
for (var key in allScores) {
var row = [key].concat(allScores[key].join(','));
summarySheet.appendRow(row);
}
Finally, you can create an image in the "Summary" sheet which can serve as a button to run the script. To do so:
Within your Sheet, click on Insert>Image>Image over cells.
Select any image of your choice.
Select the newly created image and click on the three dots icon that appears on the top-right corner of the image.
Click on "Assign script" and put the function name (in this case, updateSummary) and click on OK.
try:
=QUERY(TRANSPOSE({
'MikeGeorgePhillipJohn 121519'!A1:D20,
'MikeGeorgePhillipJohn 122019'!A1:D20,
'MikeJosephPhillipHenry 122719'!A1:D20}),
"select Col1,sum(Col20)
where Col1 is not null
group by Col1
label sum(Col20)''", 0)
=ARRAYFORMULA(SPLIT(TRANSPOSE(QUERY(QUERY(TRANSPOSE({
'MikeGeorgePhillipJohn 121519'!A1:D20,
'MikeGeorgePhillipJohn 122019'!A1:D20,
'MikeJosephPhillipHenry 122719'!A1:D20}),
"select max(Col20)
where Col1 is not null
group by Col20
pivot Col1", 0),,999^99)), " "))

Generate string like Q,R,S...AB where AB is last column in Google Sheet

I have a complex Google Sheet query that works great except when a Google Sheet doesn't have as many columns as I use in my formula.
Here's what the formula looks like now:
=sum(filter(query(INDIRECT("'" & A2 & "'!$A$7:$23"),"select Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH where B='"&C2&"'",0),query(INDIRECT("'" & A2 & "'!$A$7:$23"),"select Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH where B='PROJECT'",0) >=date(2017,1,1),query(INDIRECT("'" & A2 & "'!$A$7:$23"),"select Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH where B='PROJECT'",0) <=date(2017,12,31)))
It works great. But the problem is I run it against many worksheets and some don't have e.g. column AG,AH and end at AF at which point I get an error.
So what I need is a way to generate the string Q,R,S....[Name of Last Column in Sheet] and then I can use that instead of my hard-coded Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH but I cannot figure out how to do that.
Any help is greatly appreciated. Thanks!
Per comments above, final formula was:
LEFT("Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,A‌​L,AM,AN,AO,AP,AQ,AR,‌​AS,AT,AU,AV,AW,AX,AY‌​,AZ,BA,BB,BC ",2*Columns(INDIRECT(A2&"!1:1"))-33+IF(Columns(INDIRECT(A2&"‌​!1:1"))>26,Columns(I‌​NDIRECT(A2&"!1:1"))-‌​26,0))
where column A contains the list of worksheets (tabs) in the Google Sheet. Put this in B2, and then copied it down. I am not marking this as the correct answer since others gave a correct formula-based answer but this did the trick for me.
This can be done with built-in functions:
On a helper sheet, let say you name it, helper, fill up range with letters A to Z, let say A1:A26
Let say that on B1 you write the following formula:
=ArrayFormula({A1:A26;TRANSPOSE(SPLIT(JOIN(",",SUBSTITUTE(QUERY(TRANSPOSE(A1:A26)&A1:A26,,27)," ",",")),","))}) . This will create a list of column letter headers.
On each new worksheet use columns(1:1) to get the total number of columns.
To get your string of column headers, then you could use something like :
JOIN(",",OFFSET(helper!B1,16,0,columns(1:1)-16))
QUERY(helper!B:B,"select B limit "&columns(1:1)-7&" offset 7")
NOTE:
If you decide to have only one helper sheet and use it on several spreadsheets, then use
QUERY(IMPORTRANGE(your_url,"helper!B:B"),"select Col1 limit "&columns(1:1)-7&" offset 7")
This can be done with script. Without seeing you spreadsheet, it is hard to know exactly what you need, but this should be close. I get the variables from Sheet1 and return the formula to Sheet1. Adjust the sheet name to fit your needs. This will look at your data sheets based on the variable sheet name determine the last column. Determine the column letters and build the string the query needs. It then sets the new query formula. I added a menu to run it from.
function onOpen() {
SpreadsheetApp.getActiveSpreadsheet().addMenu(
'Create Data', [
{ name: 'Run', functionName: 'formula' },
]);
}
function formula(){
var ss= SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getSheetByName("Sheet1") //sheet where variables are
var sheet=s.getRange("A2").getValue()//variable sheet name
var sel=makeString(sheet) //get the select string of column letters
//Create formula and return to Sheet1 A3
var f= s.getRange("A3").setFormula('=sum(filter(query(INDIRECT("\'" & A2 & "\'!$A$7:$23"),"select '+sel+' where B=\'"&C2&"\'",0),query(INDIRECT("\'" & A2 & "\'!$A$7:$23"),"select '+sel+' where B=\'PROJECT\'",0) >=date(2017,1,1),query(INDIRECT("\'" & A2 & "\'!$A$7:$23"),"select '+sel+' where B=\'PROJECT\'",0) <=date(2017,12,31)))')
}
function makeString(sht){
var ss= SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getSheetByName(sht)
var lc=s.getLastColumn()
var rng=s.getRange(1, 17, 1, lc).getValues()
var str=''
var ltr=[]
for(var i=17;i<rng[0].length+1;i++){
ltr[i]= columnToLetter(i)
str=ltr.join(',')
}
var str1=str.substr(17)
return str1
}
function columnToLetter(column)
{
var temp, letter = '';
while (column > 0)
{
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
Let me know if you have any questions.

COUNTIF Statements: Range Across All Sheets + Cell Reference as Criterion

1) Range Across All Sheets:
I've googled everything but nothing. Basically, I need a formula that looks for the same range across all sheets.
My current formula looks like this:
=COUNTIF(Aug_15!$G:$G, "Shaun")+countif(July_15!$G:$G, "Shaun)+countif(June_15!$G:$G, "Shaun")+countif(May_15!$G:$G, "Shaun")+COUNTIF(Apr_15!$G:$G, "Shaun")+COUNTIF(Mar_15!$G:$G, "Shaun")
The issue I have is, as a month passes, a new sheet for the month is created. So this lowers the automation dramatically as you have to edit the formula every month. I'm basically looking for something that will search G:G across all sheets for that criteria.
So in my imaginary world, it would look something like this:
=COUNTIF(ALLSHEETS!$G:$G, "Shaun")
2) Cell Reference as Criterion
I'm trying to make the criteria look for something from another cell. For example, I'd replace "Shaun" with the cell L3. But it doesn't work! It searches for literally the two characters L and 3!
Is there anyway to make the criteria a value from another cell?
Many Thanks,
Shaun.
As Akshin Jalilov noticed, you will need a script to achieve that. I happen to have written a custom function for that scenario some time ago.
/**
* Counts the cells within the range on multiple sheets.
*
* #param {"A1:B23"} range The range to monitor (A1Notation).
* #param {"valueToCount"} countItem Either a string or a cell reference
* #param {"Sheet1, Sheet2"} excluded [Optional] - String that holds the names of the sheets that are excluded (comma-separated list);
* #return {number} The number of times the item appears in the range(s).
* #customfunction
*/
function COUNTALLSHEETS(range, countItem, excluded) {
try {
var count = 0,
ex = (excluded) ? Trim(excluded.split()) : false;
SpreadsheetApp.getActive()
.getSheets()
.forEach(function (s) {
if (ex && ex.indexOf(s.getName()) === -1 || !ex) {
s.getRange(range)
.getValues()
.reduce(function (a, b) {
return a.concat(b);
})
.forEach(function (v) {
if (v === countItem) count += 1;
});
};
});
return count;
} catch (e) {
throw e.message;
}
}
function Trim(v) {
return v.toString().replace(/^\s\s*/, "")
.replace(/\s\s*$/, "");
}
You can use the custom function in your spreadsheet like this:
=COUNTALLSHEETS("B2:B10", "Shaun")
or when 'Shaun' is in C2
=COUNTALLSHEETS("B2:B3", C2)
There is an optional parameter allowing you to provide a string with comma-separated sheet names you wish to exclude from the count. Don't use this paramater if you want to count ALL sheets.
See if that works for you ?
1) Range Across All Sheets:
The only way you can do that is via script, otherwise Spreadsheet functions cannot dynamically read sheets in the spreadsheet.
2) Cell Reference as Criterion
If the value of L3 is "Shaun" you can do this:
=COUNTIF(Aug_15!$G:$G, L3)
Make sure that you don't put L3 in quotes.

Resources