SOLVED

Replace #N/A with prior available values on a 2D running array with ONE "spill" formula

%3CLINGO-SUB%20id%3D%22lingo-sub-2731006%22%20slang%3D%22en-US%22%3EReplace%20%23N%2FA%20with%20prior%20available%20values%20on%20a%202D%20running%20array%20with%20ONE%20%22spill%22%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2731006%22%20slang%3D%22en-US%22%3E%3CP%3EBelow%20is%20a%20small%20sample%20from%20close%20market%20prices%20for%203%20stock%20tickers%20using%20the%20STOCKHISTORY%20function%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Data.png%22%20style%3D%22width%3A%20283px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308784i4E6E50C26C6A6182%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Data.png%22%20alt%3D%22Data.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EI%20realized%20that%20when%20no%20data%20is%20available%20for%20a%20particular%20stock%2C%20it%20then%20returns%20the%20%23N%2FA%20error.%3C%2FP%3E%3CP%3EThe%20problem%20for%20me%20is%20that%20it%20screws%20up%20my%20day-to-day%20%25%20variance%20calculation%2C%20among%20other%20things%2C%20so%20I%20want%20to%20replace%20those%20%23N%2FA's%20with%20the%20last%20available%20trading%20day's%20price%2C%20like%20the%20screenshot%20below%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Goal.png%22%20style%3D%22width%3A%20205px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308785iBF44B598B5450402%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Goal.png%22%20alt%3D%22Goal.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23800000%22%3E%3CSTRONG%3E%3CU%3EBut%20here's%20the%20catch%3A%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%26nbsp%3Bonly%20a%20single-cell%20%22spill%22%20formula%20will%20work%20for%20me.%20On%20the%20above%20screenshot%2C%20it%20needs%20to%20be%20a%20formula%20on%20cell%20%22AQ2%22%20that%20%22spills%22%20the%20values%20down%20and%20across%2C%20the%20same%20way%20the%20STOCKHISTORY%20function%20works.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESince%20I%20don't%20know%20a%20way%20to%20make%20the%20STOCKHISTORY%20function%20do%20that%20%23N%2FA%20replacement%20job%20in%20and%20of%20itself%2C%20albeit%20it%20would%20be%20GREAT%20if%20it%20could%2C%20I%20had%20to%20find%20a%20workaround%20to%20make%20that%20happen%20for%20me.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20I%20did%20a%20couple%20of%20calculation%20steps%20in%20order%20to%20get%20the%20row%20numbers%20array%20that%20would%20serve%20as%20input%20to%20the%20final%20INDEX%20function%2C%20and%20managed%20to%20get%20to%20a%201D%20%22spill%22%20solution%20for%20each%20of%20the%203%20individual%20tickers%20columns%20on%20that%20example%2C%20as%20shown%20below%20(I%20just%20don't%20know%20how%20to%20trick%20those%203%20%3CFONT%20color%3D%22%23FF9900%22%3E%3CSTRONG%3Eyellow%3C%2FSTRONG%3E%3C%2FFONT%3E-shaded%20validation%20arrays%20into%20one%20consolidated%20formula%20without%20returning%20error)%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%221D_Spill_Row_Num.png%22%20style%3D%22width%3A%20971px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308789iE5E2FC0465D35614%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%221D_Spill_Row_Num.png%22%20alt%3D%221D_Spill_Row_Num.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EThe%20couple%20areas%20highlighted%20in%20%3CFONT%20color%3D%22%23800000%22%3E%3CSTRONG%3Ered%3C%2FSTRONG%3E%3C%2FFONT%3E%20indicate%20the%20rectified%20row%20numbers%20that%20will%20serve%20as%20replacements%20for%20the%20%23N%2FA's%20that%20have%20prior%20values%20to%20be%20used%20on%20their%20places.%3C%2FP%3E%3CP%3EHowever%20the%20formula%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DLET(Acumul_Range%3B%24F%242%23%3BRows_Range_Seq%3BSEQUENCE(ROWS(Acumul_Range))%3BCols_Range%3BCOLUMNS(Acumul_Range)%3B--MID(BYCOL(1%3BLAMBDA(Agreg_Range%3BCONCAT(TEXT(MOD(SMALL((Rows_Range_Seq*100000)%2B(SEQUENCE(%3BCols_Range)*(Acumul_Range%3DAgreg_Range))%3BRows_Range_Seq*Cols_Range)%3B100000)%3B%220000%22))))%3B(Rows_Range_Seq*4)-3%3B4))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CU%3E%3CSTRONG%3Eonly%20treats%20each%20column%20individually%2C%20%3C%2FSTRONG%3E%3CSTRONG%3Eand%3C%2FSTRONG%3E%3CSTRONG%3E%26nbsp%3Bthat's%26nbsp%3B%3C%2FSTRONG%3E%3CSTRONG%3Ethe%20piece%20that%20I'm%20missing%3C%2FSTRONG%3E%3C%2FU%3E%20to%20get%20to%20my%20final%20goal...%3C%2FP%3E%3CP%3E...%20which%20is%20the%20formula%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DINDEX(%24B%242%3A%24D%2411%3B%24AM%242%3A%24AO%2411%3BSEQUENCE(%3BCOLUMNS(%24B%241%3A%24D%241)))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3Bthat%20produces%20the%20final%20array%20previously%20shown%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222D_Spill_NA_Repl.png%22%20style%3D%22width%3A%20443px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308790iE96AE95B0044BC07%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222D_Spill_NA_Repl.png%22%20alt%3D%222D_Spill_NA_Repl.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20I%20can%20get%20that%20row%20number%20formula%20to%20work%20down%20and%20across%20a%202D%20range%2C%20then%20I%20would%20be%20able%20to%20replace%20that%20%3CSTRONG%3E%22%24AM%242%3A%24AO%2411%22%3C%2FSTRONG%3E%20piece%20of%20the%20INDEX%20function%20with%20a%20%3CSTRONG%3E%22%24AM%242%23%22%3C%2FSTRONG%3E%20and%20therefore%20get%20this%20baby%20kicking%20and%20screaming%20across%20the%20finish%20line.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222D_Spill_NA_Repl_Complete.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308791iD478364FCFFFE3EA%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%222D_Spill_NA_Repl_Complete.png%22%20alt%3D%222D_Spill_NA_Repl_Complete.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20ideas%2Fsuggestions%20on%20how%20to%20finesse%20that%20intermediate%20formula%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2731006%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2731785%22%20slang%3D%22en-US%22%3ERe%3A%20Replace%20%23N%2FA%20with%20prior%20available%20values%20on%20a%202D%20running%20array%20with%20ONE%20%22spill%22%20formul%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2731785%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1134010%22%20target%3D%22_blank%22%3E%40leolapa%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EConsider%20using%20IFERROR%20formula%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DIFERROR(YourFormula%2CPreviousCellValue)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3DIFERROR(YourFormula%2CC2)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2731982%22%20slang%3D%22en-US%22%3ERe%3A%20Replace%20%23N%2FA%20with%20prior%20available%20values%20on%20a%202D%20running%20array%20with%20ONE%20%22spill%22%20formul%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2731982%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1134010%22%20target%3D%22_blank%22%3E%40leolapa%3C%2FA%3EGive%20the%20below%20a%20try%2C%20leverages%20MAKEARRAY%20as%20in%20previous%20solution.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DLET(%0A%20%20%20%20rng%2CB2%3AD11%2C%0A%20%20%20%20MAKEARRAY(%0A%20%20%20%20%20%20%20%20ROWS(rng)%2C%0A%20%20%20%20%20%20%20%20COLUMNS(rng)%2C%0A%20%20%20%20%20%20%20%20LAMBDA(i%2Cj%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20IF(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20i%3D1%2CINDEX(rng%2Ci%2Cj)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20IFERROR(INDEX(rng%2Ci%2Cj)%2CINDEX(rng%2Ci-1%2Cj))%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Below is a small sample from close market prices for 3 stock tickers using the STOCKHISTORY function:

Data.png

I realized that when no data is available for a particular stock, it then returns the #N/A error.

The problem for me is that it screws up my day-to-day % variance calculation, among other things, so I want to replace those #N/A's with the last available trading day's price, like the screenshot below:

Goal.png

But here's the catch: only a single-cell "spill" formula will work for me. On the above screenshot, it needs to be a formula on cell "AQ2" that "spills" the values down and across, the same way the STOCKHISTORY function works.

 

Since I don't know a way to make the STOCKHISTORY function do that #N/A replacement job in and of itself, albeit it would be GREAT if it could, I had to find a workaround to make that happen for me.

 

So I did a couple of calculation steps in order to get the row numbers array that would serve as input to the final INDEX function, and managed to get to a 1D "spill" solution for each of the 3 individual tickers columns on that example, as shown below (I just don't know how to trick those 3 yellow-shaded validation arrays into one consolidated formula without returning error):

1D_Spill_Row_Num.png

The couple areas highlighted in red indicate the rectified row numbers that will serve as replacements for the #N/A's that have prior values to be used on their places.

However the formula

 

 

=LET(Acumul_Range;$F$2#;Rows_Range_Seq;SEQUENCE(ROWS(Acumul_Range));Cols_Range;COLUMNS(Acumul_Range);--MID(BYCOL(1;LAMBDA(Agreg_Range;CONCAT(TEXT(MOD(SMALL((Rows_Range_Seq*100000)+(SEQUENCE(;Cols_Range)*(Acumul_Range=Agreg_Range));Rows_Range_Seq*Cols_Range);100000);"0000"))));(Rows_Range_Seq*4)-3;4))

 

 

only treats each column individually, and that's the piece that I'm missing to get to my final goal...

... which is the formula

 

 

=INDEX($B$2:$D$11;$AM$2:$AO$11;SEQUENCE(;COLUMNS($B$1:$D$1)))

 

 

 that produces the final array previously shown:

2D_Spill_NA_Repl.png

 

If I can get that row number formula to work down and across a 2D range, then I would be able to replace that "$AM$2:$AO$11" piece of the INDEX function with the hash reference "$AM$2#" and therefore get this baby kicking and screaming across the finish line.

 

2D_Spill_NA_Repl_Complete.png

 

Any ideas/suggestions on how to finesse that intermediate formula?

 

Thanks in advance...

9 Replies

@leolapa 

Consider using IFERROR formula

 

=IFERROR(YourFormula,PreviousCellValue)

=IFERROR(INDEX($B$2:$D$11;$AM$2:$AO$11;SEQUENCE(;COLUMNS($B$1:$D$1)));C2)

 

=IFERROR(YourFormula,C2)

@leolapaGive the below a try, leverages MAKEARRAY as in previous solution.

=LET(
    rng,B2:D11,
    MAKEARRAY(
        ROWS(rng),
        COLUMNS(rng),
        LAMBDA(i,j,
            IF(
                i=1,INDEX(rng,i,j),
                IFERROR(INDEX(rng,i,j),INDEX(rng,i-1,j))
            )
        )
    )
)

 

That solution would indeed work just fine in case it was 100% guaranteed that STOCKHISTORY would return only one consecutive date with #N/A.
However in real world there might be occasions where #N/A may show up in two or more consecutive dates, and in those cases that solution would still return #N/A since it doesn't look up for the valid price from the last available date.
I tried to apply that solution just as you did, but the thing is that it only addresses cases with only one instance of #N/A, since it just pulls the value from one row above.
Since there might be instances where STOCKHISTORY will return two or more consecutive dates with #N/A, how can I adapt that solution so it will look for the price from the last available date? A recursive LAMBDA approach? And if so, how to setup the exit path so the formula will return no error? Still wrapping my head around that...
Can you try using some sample where it will occur?
best response confirmed by leolapa (Occasional Contributor)
Solution

@leolapa  Ahh, sorry, misunderstood. This solution handles that case, however if the very first entry is an error, it will show the errors; only if there is a prior non-error value in the column will it show the value.

=LET(
    rng,$B$2:$D$11,
    rows_,SEQUENCE(ROWS(rng)),
    nonerror_rows,--(NOT(ISERROR(rng)))*rows_,
    working_rows,
        MAKEARRAY(
            ROWS(nonerror_rows),
            COLUMNS(nonerror_rows),
            LAMBDA(i,j,
                MAX(
                    1,
                    IF(
                        i=1,INDEX(nonerror_rows,i,j),
                        MAX(
                            INDEX(nonerror_rows,SEQUENCE(i),j)
                        )
                    )
                )
            )
        ),
    INDEX(rng,working_rows,SEQUENCE(,COLUMNS(rng)))
)

 

This is very similar to my answer on your prior post for last matching row.

The thing is that your proposed approach contains one "static" cell within it (in that case, C2), so my understanding is that requires it to be dragged along down and across whatever range size I'm dealing with, so since it's not a one-cell "spilled" type solution it won't work for me, as my goal is to provide a solution that will deal with a growing table (both in columns - that is dates, and rows - that is stock tickers).

@leolapa 

Just for the interest recursion solution. Not very practical since max about 300 rows spill size.

FillDown = lambda(array, [m],
    LET( n, IF(ISBLANK(m), rows(array), m),
        IF( n = 1, INDEX(array, 1),
            LET( a, FillDown(array, n-1),
                IF( SEQUENCE(n) < n, a,
                    IFNA(INDEX(array, n ), INDEX( a, n-1) )
                )
            )
        )
    )
);

and

=FillDown(STOCKHISTORY($C$1,A2,A2+300,0,0,1))