How to use a filter inside an array formula?

喜欢而已 提交于 2019-12-07 22:29:02

问题


I have a sign-out form that I want to find out which ones are out using just formulas.

My thought process is to find the row number of the last instance of each label and if the "out" row number is higher than the "in" row number then the device is currently checked out.

Timestamp   Student     Name    Check out Chromebook    Check in Chromebook
2/26/2015   10:33:48    Bjorn   Spare1-01   
2/26/2015   10:33:59    Fred    Spare1-02   
2/26/2015   10:34:16    Bjorn                           Spare1-01
2/26/2015   10:34:39    Conor   Spare1-03   
2/26/2015   11:57:31    Conor   Spare1-01   
2/26/2015   11:57:49    Fred                            Spare1-02
2/26/2015   11:57:59    Bjorn   Spare1-02   

I am able to find find out all spares that have been checked out using: =sort(UNIQUE(C2:C))

I have been able to find the row number of the last instance of each using: =max(filter(row(C:C),C:C=F2)) (my sort unique function is in column F).

I would like to get my last row formula to work with an arrayformula so that I don't have to rely on manually dragging the formula down.

For example I would like this to work: =arrayformula(max(filter(row(C:C),C:C=F:F))) note the addition of arrayformula and the change from F:2 to F:F.

I am not quite sure why but I get a result of 1000.

Edit: My expected output would be "Last checkout row".

Out Unique  Last checkout row
Spare1-01   6
Spare1-02   8
Spare1-03   5

If I can figure out the adding it to the array formula I would also like to find a way to put in the sort (unique function so that it becomes a single formula which I enter in the column number and it spits out the last row numbers of each unique term).

Here is a link to a document that I am playing with: https://docs.google.com/spreadsheets/d/1jC0RPxUZSt7BCHRQI5vBXvoANbQlu9CjDNr2VV-MOOI/edit?usp=sharing

Edit (final formula used) (I can't thank @JVP enough for the help with this):

=ArrayFormula( if(vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:B, row(A2:A)},4,0), 4, 0) > iferror(vlookup(unique(filter(C2:C, len(C2:C))), sort({D2:D, A2:B, row(A2:A)},4,0),4,0),0), vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:C, row(A2:A)},5,0), {2,4,3}, 0), ))

回答1:


For your desired output (including the names), try:

=ArrayFormula({unique(filter(C2:C, len(C2:C))), vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:B, row(A2:A)},4,0), 4, 0)})

Following your thought process (ultimately you want to see if a device is currently checked in or out, right ?), try:

=ArrayFormula({unique(filter(C2:C, len(C2:C))), if(vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:B, row(A2:A)},4,0), 4, 0) > iferror(vlookup(unique(filter(C2:C, len(C2:C))), sort({D2:D, A2:B, row(A2:A)},4,0),4,0),0), "checked out", "checked in")})

Example sheet with last formula in F2




回答2:


Thank you @JPV Here is the final formula that I used, I changed things around so that all I had to do was enter in the number of the column in and number of the column out:

=sort(ArrayFormula( if(vlookup(unique(filter(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), len(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE)))), sort({INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE),row('Form Responses 1'!A2:A)},2,0), 2, 0) > iferror(vlookup(unique(filter(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), len(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE)))), sort({INDIRECT("'Form Responses 1'!"&"R2C5:C5", FALSE),row('Form Responses 1'!A2:A)},2,0), 2, 0),0), vlookup(unique(filter(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), len(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE)))), sort({INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), row('Form Responses 1'!A2:A), INDIRECT("'Form Responses 1'!"&"R2C1:C"&COUNTA('Form Responses 1'!1:1)&"", FALSE)},2,0), {3,4,6}, 0), )))

I am programably creating the script, and here is the code.

function createReportSheet(setName) {
  if (!setName){setName = "Items"};
  var destId = FormApp.getActiveForm().getDestinationId();
  var as = SpreadsheetApp.openById(destId);
  try{
  var sheet = as.insertSheet("Report Out: "+setName);
  } catch(err){
   var sheet = as.getSheetByName("Report Out: "+setName);
   sheet.clear(); 
  }  


  //build formula
  var formSheetName = "'Form Responses 1'!";
  var colOut = 4;
  var colIn = 5;
  var colToShow = "3,4,6"; //  A=3, B=4, C=5, .. 

  var frTimeStamp = formSheetName+"A2:A";
  var lastCol = "&COUNTA("+formSheetName+"1:1)&";

  var frSort = 'INDIRECT("'+formSheetName+'"&"R2C'+1+':C"'+lastCol+'"", FALSE)';
  var frIn  = 'INDIRECT("'+formSheetName+'"&"R2C'+colIn+':C'+colIn+'", FALSE)';
  var frOut = 'INDIRECT("'+formSheetName+'"&"R2C'+colOut+':C'+colOut+'", FALSE)';

  //break into parts
  var f_unique = "unique(filter("+frOut+", len("+frOut+")))";
  var f_sortIn = "sort({"+frOut+",row("+frTimeStamp+")},"+2+",0)";
  var f_vlookupIn = "vlookup("+f_unique+", "+f_sortIn+", "+2+", 0)";
  var f_sortOut = "sort({"+frIn+",row("+frTimeStamp+")},"+2+",0)";
  var f_vlookupOut = "vlookup("+f_unique+", "+f_sortOut+", "+2+", 0)"; 
  var f_sortDisp = "sort({"+frOut+", row("+frTimeStamp+"), "+frSort+"},"+2+",0)";
  var f_vlookupDisp = "vlookup("+f_unique+", "+f_sortDisp+", {"+colToShow+"}, 0)"; 
  var formula1 = "ArrayFormula( if("+f_vlookupIn+" > iferror("+f_vlookupOut+",0), "+f_vlookupDisp+", ))";
  var formula = "=sort("+formula1+")";

  // put formula into sheet
  sheet.getRange(2, 1).setFormula(formula);
  sheet.getRange("a:a").setNumberFormat("mm/dd/yy h:mm:s");

}


来源:https://stackoverflow.com/questions/28751620/how-to-use-a-filter-inside-an-array-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!