cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Have your say in shaping JMP's future by participating in the new JMP Wish List Prioritization Survey
Choose Language Hide Translation Bar

Increment a value for Run Number

I am trying to develop a formula that increments up the Run #. It should count up for each time a Shop Order has a new Process ID and reset back to 1 when a new Shop Order has not been run before. As shown below the Shop Order 532 has 3 different Process IDs, but they are not in order so it still increments up to 3 on the final one even though it isn’t in order. I created a column manually showing what I would want as an ideal outcome for the formula column. How would I write a formula to deal with this issue?

 

 

dkraeuter_sunne_0-1619545627775.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Increment a value for Run Number

OK....my oversight again....I think the approach below solves the issue

Names Default To Here( 1 );
dt = Current Data Table();

// Create a column that when the data table is sorted, it can be returned to the original
dt << New Column( "theRow", formula( Row() ) );

// Remove the formula to turn the values into static values
:theRow << delete formula;

// Sort the data into an order where all Process ID for a given Shop Order are together
dt << sort( by( :Shop Order, :Process ID, :theRow ), replace table( 1 ) );

// Create the Run # column
dt << New Column( "Run #",
	formula(
		If( Row() == 1 | :Shop Order != Lag( :Shop Order ),
			count = 1,
			If( :Process ID != Lag( :Process ID ),
				count
				++)
		);
		count;
	)
);

// Remove the formula to turn the values into static values
:"Run #"n << delete formula;

// Sort back into the original row order
dt << sort( by( :theRow ), replace table( 1 ) );

// Delete the no longer needed theRow column
dt << delete columns( :theRow );
Jim

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: Increment a value for Run Number

Here is a formula you can use.  Create the new column Run #.  I suggest you make the modeling type "Ordinal".  Open the Formula Editor for the new column, and paste in the following.

If( Row() == 1,
	:Process ID << set property( "Value Order", {Common Order( 0 ), Row Order Levels( 1 )} );
	Summarize( byGroup = by( :Process ID ) );
	:Process ID << delete property("Value Order");
);

Below is a script that will do the column creation and apply the formula for you

Names Default To Here( 1 );
dt = Current Data Table();

dt << New Column( "Run #",
	ordinal,
	formula(
		If( Row() == 1,
			:Process ID << set property( "Value Order", {Common Order( 0 ), Row Order Levels( 1 )} );
			Summarize( byGroup = by( :Process ID ) );
			:Process ID << delete property("Value Order")
		);
		Contains( byGroup, :Process ID );
	)
);
Jim

Re: Increment a value for Run Number

Thank you for this. It doesn't quite do what I am looking for. See the mismatch between the "Ideal Outcome Column for Run #" and the "Run #". The way the formula is currently working is just incrementing up no matter if a new SO is started. 

 

dkraeuter_sunne_0-1619550890491.png

 

txnelson
Super User

Re: Increment a value for Run Number

Sorry, I missed the link with Shop Order.  That changes everything and makes the formula much simpler

If( Row() == 1 | :Shop Order != Lag( :Shop Order ),
	count = 1,
	If( :Process ID != Lag( :Process ID ),
		count
		++)
);
count;
Jim

Re: Increment a value for Run Number

That was closer, but it restarted the count for Shop Order # 532 at 1 the second time that it was listed since there was another Shop Order between runs 2 and 3. The image below shows the highlighted cells where it deviated from the Ideal Outcome. 

 

dkraeuter_sunne_0-1619561255001.png

 

 

txnelson
Super User

Re: Increment a value for Run Number

OK....my oversight again....I think the approach below solves the issue

Names Default To Here( 1 );
dt = Current Data Table();

// Create a column that when the data table is sorted, it can be returned to the original
dt << New Column( "theRow", formula( Row() ) );

// Remove the formula to turn the values into static values
:theRow << delete formula;

// Sort the data into an order where all Process ID for a given Shop Order are together
dt << sort( by( :Shop Order, :Process ID, :theRow ), replace table( 1 ) );

// Create the Run # column
dt << New Column( "Run #",
	formula(
		If( Row() == 1 | :Shop Order != Lag( :Shop Order ),
			count = 1,
			If( :Process ID != Lag( :Process ID ),
				count
				++)
		);
		count;
	)
);

// Remove the formula to turn the values into static values
:"Run #"n << delete formula;

// Sort back into the original row order
dt << sort( by( :theRow ), replace table( 1 ) );

// Delete the no longer needed theRow column
dt << delete columns( :theRow );
Jim