Sorry, I tried to keep my question simple, but let me now open up it more. I want to calculate a value of my stock portfolio for all days. I have end-of-day quotes in a table called HistoricalQuotes:
[TABLE="class: grid, width: 192"]
<colgroup><col span="3"></colgroup><tbody>[TR]
[TD]Ticker
[/TD]
[TD]Date[/TD]
[TD]Price[/TD]
[/TR]
[TR]
[TD]KO[/TD]
[TD="align: right"]28.6.2007[/TD]
[TD="align: right"]128,06[/TD]
[/TR]
[TR]
[TD]MSFT[/TD]
[TD="align: right"]28.6.2007[/TD]
[TD="align: right"]328,06
[/TD]
[/TR]
[TR]
[TD]NOKIA[/TD]
[TD="align: right"]28.6.2007[/TD]
[TD="align: right"]228,06[/TD]
[/TR]
[TR]
[TD]KO[/TD]
[TD="align: right"]2.7.2007[/TD]
[TD="align: right"]102,07[/TD]
[/TR]
[TR]
[TD]MSFT[/TD]
[TD="align: right"]2.7.2007[/TD]
[TD="align: right"]302,07[/TD]
[/TR]
[TR]
[TD]NOKIA
[/TD]
[TD="align: right"]2.7.2007[/TD]
[TD="align: right"]202,07[/TD]
[/TR]
[TR]
[TD]KO[/TD]
[TD="align: right"]3.7.2007[/TD]
[TD="align: right"]103,07[/TD]
[/TR]
[TR]
[TD]MSFT[/TD]
[TD="align: right"]3.7.2007[/TD]
[TD="align: right"]303,07
[/TD]
[/TR]
[TR]
[TD]NOKIA[/TD]
[TD="align: right"]3.7.2007[/TD]
[TD="align: right"]203,07[/TD]
[/TR]
[TR]
[TD]KO[/TD]
[TD="align: right"]4.7.2007[/TD]
[TD="align: right"]104,07[/TD]
[/TR]
[TR]
[TD]NOKIA[/TD]
[TD="align: right"]4.7.2007[/TD]
[TD="align: right"]204,07[/TD]
[/TR]
[TR]
[TD]KO[/TD]
[TD="align: right"]5.7.2007[/TD]
[TD="align: right"]105,07[/TD]
[/TR]
[TR]
[TD]MSFT[/TD]
[TD="align: right"]5.7.2007[/TD]
[TD="align: right"]305,07[/TD]
[/TR]
[TR]
[TD]NOKIA[/TD]
[TD="align: right"]5.7.2007[/TD]
[TD="align: right"]205,07[/TD]
[/TR]
[TR]
[TD]KO[/TD]
[TD="align: right"]6.7.2007[/TD]
[TD="align: right"]106,07
[/TD]
[/TR]
[TR]
[TD]MSFT[/TD]
[TD="align: right"]6.7.2007[/TD]
[TD="align: right"]306,07
[/TD]
[/TR]
[TR]
[TD]NOKIA[/TD]
[TD="align: right"]6.7.2007[/TD]
[TD="align: right"]206,07[/TD]
[/TR]
[TR]
[TD]KO[/TD]
[TD="align: right"]9.7.2007[/TD]
[TD="align: right"]109,07
[/TD]
[/TR]
[TR]
[TD]MSFT[/TD]
[TD="align: right"]9.7.2007[/TD]
[TD="align: right"]309,07
[/TD]
[/TR]
[TR]
[TD]NOKIA[/TD]
[TD="align: right"]9.7.2007[/TD]
[TD="align: right"]209,07
[/TD]
[/TR]
</tbody>[/TABLE]
and in the other table I'm calculating my portfolio value. For the sake of simplicity I'm now assuming that I own same companies and same number of shares all the time. In reality this will vary, and an array of tickers and # of shares are calculated in another tab.
[TABLE="class: grid, width: 180"]
<colgroup><col span="2"></colgroup><tbody>[TR]
[TD]Date
[/TD]
[TD]Portfolio value
[/TD]
[/TR]
[TR]
[TD="align: right"]2.7.2007[/TD]
[TD="align: right"]10 124,20[/TD]
[/TR]
[TR]
[TD="align: right"]3.7.2007[/TD]
[TD="align: right"]10 184,20
[/TD]
[/TR]
[TR]
[TD="align: right"]4.7.2007[/TD]
[TD="align: right"]7 203,50
[/TD]
[/TR]
[TR]
[TD="align: right"]5.7.2007[/TD]
[TD="align: right"]10 304,20
[/TD]
[/TR]
[TR]
[TD="align: right"]6.7.2007[/TD]
[TD="align: right"]10 364,20[/TD]
[/TR]
</tbody>[/TABLE]
Portfolio value is calculated with the following formula:
=SUMPRODUCT({10;20;30};SUMIFS(HistoricalQuotes[Price]; HistoricalQuotes[Date]; $E2; HistoricalQuotes[Ticker];{"MSFT";"NOKIA";"KO"}))
If works perfectly, as long as there is a rate for the stock in HistoricalQuotes-table for a date when portfolio value is calculated. In case of MSFT 4th of July rate is (naturally) missing, as stock exchange is closed. So, my formula should use the previous day (3rd of July) rate for MSFT, when it's calculating portfolio value for 4th of July. I should use use date that is maximum of dates that are "<=" than the calculation date (here $E2).
I thought that I could use the following formula to calculate row numbers from HistoricalQuotes-table, which contain row-numbers that contain the correct price:
=(--(TRANSPOSE({"MSFT";"NOKIA";"KO"}) = HistoricalQuotes[Ticker]) ) * (--(HistoricalQuotes[Date]<=$E2)) * ROW(HistoricalQuotes)
=maximum( (--(TRANSPOSE({"MSFT";"NOKIA";"KO"}) = HistoricalQuotes[Ticker]) ) * (--(HistoricalQuotes[Date]<=$E2)) * ROW(HistoricalQuotes) )
{9;12;11}
If I got row numbers right, INDEX function would give me easily right price.
Unfortunately I have not been able to figure out how get the result I need.