google-sheetsgoogle-sheets-formula

Closing balance per currency from a transaction log?


I'm trying to get an accurate maximum balance for an FBAR based on a Revolut transaction log spreadsheet.

The problem I've run into is that on a given day, I may have a series of transactions similar to the following:

Type Date Amount Currency Balance
TOPUP 2024-01-01 11:00:00 1000 CHF 1500.00
EXCHANGE 2024-01-101 11:01:00 1100 EUR 1100.00
EXCHANGE 2024-01-01 11:02:00 -1000 CHF 500

I'm looking for the closing balance on each date in each currency, so in this case I want to record 1100 EUR and 500CHF for the day. I.e., for each day + currency, I'd want the balance corresponding to the maximum timestamp in the date column. I then plan to normalize everything to USD using the GOOGLEFINANCE currency conversion function, add up the closing balances, and find the max daily sum over the year.

I can get the maximum value for a given day for each currency, but that gives me an artificially high balance because the TOPUP amount is included in both the CHF balance and the EUR balance, even though it just moved from one to the other. E.g., in the example above, I'd get a max of 1100 EUR and 1500 CHF, when in reality the overall maximum was just 1500 CHF, or 1100 EUR + 500 CHF.

I can see how to do it in SQL, or maybe some kind of app script, but I'm hoping there's a way to express what I want here in a native function. I realize that I can just eyeball it for FBAR reporting, but at this point I'm personally invested in getting an accurate result.

This one has got me a bit stumped. Any suggestion on what's the best approach here?


Solution

  • Here's one way to do that:

    =map(unique(tocol(D2:D, 1)), lambda(cur, let(
      maxTime, maxifs(B2:B, D2:D, cur),
      hstack(
        cur,
        filter(E2:E, D2:D = cur, B2:B = maxTime)
      )
    )))
    

    See map(), unique(), tocol(). let(), maxifs(), hstack() and filter().