如何同时过滤列值和该数据子集的 MAX(column)?
How can I filter on both a columnA value and the MAX(columnB) of a subset of that data?
我有以下数据问题,我想弄清楚如何封装在 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 查询语言,但有什么帮助吗?
=unique(filter(A2:C,A2:A="Washington"))
Returns "Washington" 玩家的所有行,但(可能)丢失了有关该游戏中得分最多的人的数据。有没有办法将该行也包含在过滤器中?
您可以通过在 Google Apps 脚本中创建的 custom function 来完成此操作。为此,请按照下列步骤操作:
- 在您的电子表格中,select工具 > 脚本编辑器打开绑定到您的文件的脚本。
- 在脚本编辑器中复制此函数,并保存项目:
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
),如您在此处所见:
参考:
我有以下数据问题,我想弄清楚如何封装在 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 查询语言,但有什么帮助吗?
=unique(filter(A2:C,A2:A="Washington"))
Returns "Washington" 玩家的所有行,但(可能)丢失了有关该游戏中得分最多的人的数据。有没有办法将该行也包含在过滤器中?
您可以通过在 Google Apps 脚本中创建的 custom function 来完成此操作。为此,请按照下列步骤操作:
- 在您的电子表格中,select工具 > 脚本编辑器打开绑定到您的文件的脚本。
- 在脚本编辑器中复制此函数,并保存项目:
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
),如您在此处所见: