简体   繁体   中英

array formula: sum to date

I have a number of invoices:

invoice # start end paid on amount paid to date (hardcoded)
1 01/01/2020 30/01/2020 01/02/2020 £10.00 £10.10
2 01/02/2020 20/02/2020 01/03/2020 £7.50 £17.60
3 21/02/2020 30/02/2020 01/03/2020 £2.50 £20.10
4 01/01/2000 30/01/2000 01/03/2000 £0.10 £0.10

Where the invoices

  • are not necessarily sorted by start and end date
  • are not necessarily sorted by paid date.
  • might not have a paid-on value.

I want to add a field called paid to date that would show me the amount I have been paid so far where it would add the amount for:

  • the current invoice
  • the invoices that were paid prior to this invoice's paid-on date.
  • the invoices with the same paid-on date as this invoice but with a start date <= this invoice's start date.

Effectively mirroring the hard-coded column.

This is how I do it with a query (which might not be the simplest/most elegant way of doing it)

=Index(
    query(
      A1:E10, 
      "select SUM(E)
      where D is not null
      and (
        D < date '"& TEXT( D2,"yyyy-mm-dd")&"' 
        OR (
          D = date '"& TEXT( D2,"yyyy-mm-dd")&"'
          and
          B <= date '"& TEXT( B2,"yyyy-mm-dd")&"'
        )
      )"
    ),
    2, 1
  )

which is all well and good. but I want to be able to do it with array-formula, so I can have it auto-generated for me.

I tried using it inside array-formula but the value is only ever generated for the first row. I guess it's misinterpreting the range I am passing as the range of the query function, ie A1:E10 . is there an easy way of fixing it?

Do I need to use VLookup?

Here is a sample spreadsheet.

try:

=INDEX(IFNA(VLOOKUP(A2:A, {INDEX(SORT({A2:B, D2:E}, 2, 1, 3, 1),,1), 
 MMULT(TRANSPOSE((ROW(E2:E)<=TRANSPOSE(ROW(E2:E)))*
 INDEX(SORT({A2:B, D2:E}, 2, 1, 3, 1),,4)), SIGN(E2:E))}, 2, 0)))

在此处输入图像描述

If you have Excel 365, you can Filter directly.
Note that I am using a table with structured references. Then you don't need to adjust the range references as you add/remove rows from the table.

=SUM(FILTER([amount],([paid on]<[@[paid on]])+(([paid on]=[@[paid on]])*([start]<[@start])),0),[@amount])

This part does the filtering:

...([paid on]<[@[paid on]])+(([paid on]=[@[paid on]])*([start]<[@start]))...

在此处输入图像描述

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