简体   繁体   中英

How to compare latest value in range with averages based on multiple criteria using formulas in Google Sheets efficiently?

I got the following formula giving me the right result, but since it'll be in a couple of a thousand rows, it takes too long to calculate and, sometimes, it doesn't even return the result:

=(INDEX($F$2:$F,MATCH(maxifs($B$2:$B,$A$2:$A,$A2),$B$2:$B,0),)>Averageifs($F$2:$F,$B$2:$B,"<="&MAXIFS($B$2:$B,$A$2:$A,A2),$B$2:$B,">="&WORKDAY(MAXIFS($B$2:$B,$A$2:$A,A2),-10)))

Here's a copy containing dummy data and the hurdle highlighted, if you could help with any pointers: https://docs.google.com/spreadsheets/d/1sTehnvkjDtZE6PUpXYdiMFKZFwA10EUbzEdszWPDryY/edit?usp=sharing

Thank you!

You can consider using Custom Formula to process your data with Apps Script.

Sample Code:

/**
 * @customfunction
 */
function compareCloseDateToDays(value) {
  var result = value.flat().filter(String);
  var latest = result[result.length-1];
  var tenDays = result[result.length-11];
  Logger.log(latest);
  Logger.log(tenDays);
  return(latest>tenDays);

}

/**
 * @customfunction
 */
function compareCloseDateToAve(value) {
  var result = value.flat().filter(String);
  var latest = result[result.length-1];
  var tenAve = result.slice(result.length-10);
  var sum = 0;
  var ave = 0;
  for (var i = 0; i < tenAve.length; i++) {
    sum += tenAve[i];
  }

  ave = sum/tenAve.length;
  Logger.log(latest);
  Logger.log(ave);
  return(latest>ave);
}

How to use:

  • Provide range of close values in the function
    =compareCloseDateToDays(F7:F)
    =compareCloseDateToAve(F7:F)

Output:

在此处输入图像描述

在此处输入图像描述


Note:

In your original formula, you include a criteria where column A should match A8 when getting the max value of the date.

You can use Filter() to filter column F before calling the custom functions.

Sample Scenario:

在此处输入图像描述

  • I changed A109 to "test"

Then use this function: =compareCloseDateToDays(filter(F7:F,A7:A=A8)) The output is "TRUE"

在此处输入图像描述

  • Last Value = 14.35
  • Value from 10 workdays = 14.27

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM