我如何同时过滤columnA值和该数据子集的MAX(columnB)?

问题描述 投票:0回答:1

我有以下数据问题,我试图找出如何封装在Google表格公式或查询中:

Player     Game  Shots Scored
Washington  1         3
Lincoln     1         6
Roosevelt   1         4
Washington  2         3
Grant       2         1
Kennedy     2         3

我感兴趣的是计算平均得分作为该游戏中得分最高的球员的百分比。或者,换句话说:

For a Player P:
 Return the Average of(
    For Each Distinct Game containing P (
        P.Points / Max(Points)  
    )
 )

例如,使用上面的数据,我正在查看的结果是:

Player      Performance
Washington      75% [=(3/6+3/3)/2 * 100%]         
Lincoln         100%          
Roosevelt       66%               
Grant           33%       
Kennedy         100%          

尽管这种复合计算被证明很难翻译成Google查询语言,但是有帮助吗?

=独特(过滤器(A2:C,A2:A =“华盛顿”))

返回“华盛顿”玩家的所有行,但(可能)丢失该游戏中得分最高的人的数据。是否也可以在过滤器中包含该行?

google-sheets google-sheets-formula google-sheets-query
1个回答
0
投票

您可以通过在Google Apps脚本中创建的custom function完成此操作。为此,请按照下列步骤操作:

  • 在电子表格中,选择工具>脚本编辑器打开绑定到文件的脚本。
  • 在脚本编辑器中复制此功能,并保存项目:
function GETAVERAGES(input) {
  const games = Array.from(new Set(input.map(row => row[1]))).map(game => {
    let maxResult = Math.max(...input.filter(row => row[1] === game).map(row => row[2]));
    return [game, maxResult]; // Get max score for each game
  });  
  return Array.from(new Set(input.map(row => row[0]))).map(player => {
    let playerRows = input.filter(row => row[0] === player); // Filter rows according to player
    let percentages = playerRows.reduce((acc, row) => { // Percentage sum for current player
      let maxGame = games.find(game => game[0] === row[1])[1]; // Max score for current game   
      return acc + row[2] / maxGame; // Accumulated percentage
    }, 0);
    let average = percentages / playerRows.length; // Divide accumulated per number of games
    return [player, average]; // Return array of player name and corresponding average
  });
}
  • 现在,如果您返回电子表格,则可以像使用常规工作表公式一样使用此功能。您只需要提供适当的范围作为参数即可(在本例中为A2:C7),如下所示:

enter image description here

参考:

© www.soinside.com 2019 - 2024. All rights reserved.