【问题标题】:How to get last row which matches values in 2 columns on Google Sheet如何获取与 Google Sheet 上 2 列中的值匹配的最后一行
【发布时间】:2021-06-03 18:12:49
【问题描述】:

我有一个交易表,其中包含每次购买时购买的物品的交易。最新交易将包含购买该商品后在该商品上花费的总金额。

我想根据项目(每天)创建一个随时间推移花费的金额的图表。我使用下面的公式来获取该商品最新交易的行号

ARRAYFORMULA(MAX(IF(Transaction!$B:$B="Apple",ROW(Transaction!$B:$B),-1))) --> returns 6'

但我似乎无法添加到此公式中以获取基于项目的最新行以及日期是否小于或等于我的图表日期

交易表

1 Date Item Amount Prev. Spent Curr. Spent
2 1 Jan 2020 Apple $10 $0 $10
3 1 Jan 2020 Banana $12 $0 $12
4 2 Jan 2020 Corn $3 $0 $3
5 2 Jan 2020 Banana $5 $12 $17
6 3 Jan 2020 Apple $50 $10 $60
7 5 Jan 2020 Banana $23 $17 $40

图表

Date Apple Banana Corn
1 Jan 2020 $10 $12 $0
2 Jan 2020 $10 $17 $3
3 Jan 2020 $60 $17 $3
4 Jan 2020 $60 $17 $3
5 Jan 2020 $60 $40 $3

【问题讨论】:

  • 您是否愿意使用 Apps 脚本 custom function
  • 是的,我不介意

标签: google-sheets google-sheets-formula


【解决方案1】:

为了用简单的公式解决你的问题,我会使用sumifs函数:

=sumifs($D:$D,$C:$C,I$15,$B:$B,$H17)+I16

Gsheet 数据如下所示:

【讨论】:

  • 谢谢!没想到这个!这完美无缺
  • 欢迎,只要你觉得简单易懂:)
【解决方案2】:

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

  • 在您的电子表格中,选择工具 > 脚本编辑器以打开与您的文件绑定的脚本。
  • 在脚本编辑器中复制此函数,并保存项目(查看内联 cmets 以了解有关该函数的作用的更多信息):
function getTable(values) {
  let times = values.map(row => row[0].getTime());
  const minTime = Math.min(...times); // Get first day in series
  const maxTime = Math.max(...times); // Get last day in series
  times = getTimes(minTime, maxTime); // Get all days between minimum and maximum
  const items = [...new Set(values.map(row => row[1]))]; // Get unique list of items
  let targetValues = [["Date", ...items]]; // Header row
  for (let i = 0; i < times.length; i++) { // Loop through all days
    const time = times[i];
    const dayRows = values.filter(row => row[0].getTime() === time); // Get rows with this day
    if (dayRows) {
      const rowValues = items.map((item, j) => { // Loop through all items in day rows
        const foundRow = dayRows.find(row => row[1] === item); // Find row corresponding to day and item
        const previous = i === 0 ? 0 : targetValues[targetValues.length - 1][j + 1]; // Value in previous row
        if (foundRow) return foundRow[2] + previous;
        else return 0 + previous;
      });
      targetValues.push([new Date(time), ...rowValues]);
    } else { // If day is not in source data, values are the same than previous row
      targetValues.push([new Date(time), ...targetValues[targetValues.length - 1].slice(1)]);
    }
  }
  return targetValues;
}

function getTimes(minTime, maxTime) {
  let allTimes = [minTime];
  let currentTime = minTime;
  while (currentTime < maxTime) {
    let date = new Date(currentTime);
    date.setDate(date.getDate() + 1);
    currentTime = date.getTime();
    allTimes.push(currentTime);
  }
  return allTimes;
}
  • 现在,如果您返回到您的电子表格,您可以使用函数getTable,就像您使用常规工作表公式一样。您只需提供适当的范围作为参数(在本例中为 A2:E7),如下所示:

参考:

【讨论】:

    【解决方案3】:

    我可以分两个阶段提供一个公式。

    1. Transactions标签:

    1. Pivot 标签、单元格A1

    =query({Transactions!A:F},"select Col2,max(Col4) where Col1 is not null group by Col2 pivot Col3",1)

    1. ChartData标签,单元格A1
    =arrayformula(
     array_constrain(
      {Pivot!A1:D1;
       (sequence(max(row(Pivot!A:A))-1,1,min(Transactions!$B:$B))),
       sumif(row(Pivot!B2:B),"<="&row(Pivot!B2:B),Pivot!B2:B),
       sumif(row(Pivot!C2:C),"<="&row(Pivot!C2:C),Pivot!C2:C),
       sumif(row(Pivot!D2:D),"<="&row(Pivot!D2:D),Pivot!D2:D)
      },
     counta(Pivot!A:A)+1,counta(Pivot!1:1)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多