Subscribe Bookmark RSS Feed

Formula to use previous value if not missing, otherwise use value from last occupied cell

terapin

Community Trekker

Joined:

Jun 23, 2011

I'm trying to write a formula in Col3 that adds the current value of Col1 with the lagged value of Col2 but I'm running into a pickle trying to figure out how to carry forward the last observation from Col2 when missing data is encountered and to use this value at the first instance of non-missing data.  That is,

If( !IsMissing( Col1)

     Col1 + Lag(Col2,1);

     create variable that contains current Col2 value;


//Else

     Col1 + previously stored value from Col2;

);

I would appreciate any suggestions on how best to accomplish this.  Thanks.

Row#Col1Col2Col3
121
2132
3154
4277
5..
6..
7..
8289
92910
1011110
11..
13..
1421213
1521314
1611414
1721516
1 ACCEPTED SOLUTION

Accepted Solutions
Solution

I think this formula will give you what you're after:

If(Is Missing(:Column 1), :Column 1, Summation(i = 1, Row(), :Column 1))

Hope this helps.

-Jerry

7 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

Here is one version of the formula that works:

If( Row() == 1, lagval = . ); 

x = Sum( :col 1, lagval ); 

If( Is Missing( col 2 ) == 0, 

       lagval = :col 2 

);

x;

 

Jim
Jeff_Perkinson

Community Manager

Joined:

Jun 23, 2011

I think there's an error in your description that we'll have to clear up before we can give you a precise solution. Your conditional says if that if Col1 is NOT MISSING then add it to the lagged value of Col2, and if Col1 IS MISSING then add that to a previously stored value of Col2.

You can't add a missing Col1 to anything and get anything other than missing.

Having said that, I would build this table out one column at a time. Specifically, I would create a "non-missing Col2" column which had no missing values using the formula:

11375_JMPScreenSnapz015.png

This formula sets "non-missing Col2" to Col2 when Col2 isn't missing and sets it to the Lag of "non-missing Col2" when Col2 is missing.

11376_JMPScreenSnapz016.png

You can then use a Lag(non-missing Col2, 1) in your final formula.

I hope that helps.

-Jeff

-Jeff
terapin

Community Trekker

Joined:

Jun 23, 2011

Thanks Jim and Jeff,

I think I made the issue more complicated than it needs to be in my description and table layout and can see how that may have led you to your suggestions.

While I understand your suggestion Jeff about creating a new column that holds the non-missing data I'm hoping to accomplish my work without creating new columns since I'll be using this formula for many different columns.  Let me try to be more clear about what I'm trying to accomplish by way of the table below.

I'm trying to create a running sum of Column 1 in Column 2.  Without creating any additional columns, I was hoping to create a variable that contains the current running sum value in Column 2 and adds that to the value of Column 1.

My issue is not knowing the best way to create and use a variable that contains the present value for Column 2 so that this value can be "carried" across rows of missing Column 1 data.  Column "Result I'm Looking For" shows what I would like the formula in Column 2 to produce.  Hopefully this description is a bit more clear about what I'm looking to accomplish.

11378_pastedImage_1.png

Solution

I think this formula will give you what you're after:

If(Is Missing(:Column 1), :Column 1, Summation(i = 1, Row(), :Column 1))

Hope this helps.

-Jerry

markschahl

Community Trekker

Joined:

Jun 18, 2012

I run into this problem all of the time and use the solution give by Jerry. I analyze process measurements (temperature, pressure, flow, etc.) that are retrieved from a compressed-data-historian. Every few thousand points, there can be a missing value.

Example below calculates if reactor is running or not based on 3 point moving average of catalyst flow, F.Cat >= 11 (anonymized yes, you get the reference...). If F.Cat is missing, calc uses previous row's value of "Running?".

If(Row() < 3, 0, Is Missing(:F.Cat), Lag(:Name("Running?"), 1), (Lag(:F.Cat, 2) + Lag(:F.Cat, 1) + :F.Cat) / 3 >= 11, 1, 0)

vince_faller

Super User

Joined:

Mar 17, 2015

Have we always been able to have a column Formula be self-referential?  I never realized I could do this. 

terapin

Community Trekker

Joined:

Jun 23, 2011

Thanks everyone for your comments and help. 

I think Jerry's suggestion works the best for me and didn't require the use of a local variable which is what I was wanting to use.