cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Register for our Discovery Summit 2024 conference, Oct. 21-24, where you’ll learn, connect, and be inspired.
Choose Language Hide Translation Bar
scott1588
Level IV

Setting Values in a Column with Choose

I'm having what I'm sure is a simple issue with setting values in a column using the Choose function. Below is a test script along with a table showing desired output.

 

Names Default To Here( 1 );

startTime = 3742070400;
endTime = 3742074000;

dt = New Table( "Test",
	Add Rows( 60 ),
	New Column( "Timestamp", Format( "m/d/y h:m", 19 ), Set Display Width( 175 ) )
);

For Each Row( :Timestamp[] = Sequence( startTime, endTime, 60 ) ); 

aggOption = 1; // This will eventually be a pulldown menu with choices

New Column( "Agg Minute",
	Numeric,
	"Ordinal",
	Format( "Best", 12 ),
	Set Display Width( 116 )
);

// Script below is the problem. I tried For Each Row and Set Values and a combination
// of both but the column always is blank. See Interpolated Data table for what it
// should look like.

dt:"Agg Minute"n << For Each Row(
	Choose( aggOption,
		If(
			Minute( :Timestamp ) < 15, 15,
			Minute( :Timestamp ) < 30, 30,
			Minute( :Timestamp ) < 45, 45,
			0
		),
		If(
			Minute( :Timestamp ) < 14, 0,
			Minute( :Timestamp ) < 29, 15,
			Minute( :Timestamp ) < 44, 30,
			45
		),
		If(
			0 < Minute( :Timestamp ) <= 15, 15,
			15 < Minute( :Timestamp ) <= 30, 30,
			30 < Minute( :Timestamp ) <= 45, 45,
			0
		)
	)
);

Notice when you run the script the Agg Minute column is blank. I've tried Set Values and For Each Row and a combination of both but I can't seem to get it to work.

 

As always, any assistance is greatly appreciated.

 

Thanks.

2 REPLIES 2
scott1588
Level IV

Re: Setting Values in a Column with Choose

I can get it to work as a column formula if I set a local variable. But then I can't use my combo box result.

 

Here is that code. There is obviously much more I need to learn about JSL. It is very easy to auto-generate code. But often when I try to customize it, it breaks and I don't know enough yet to fix it.

 

Arrgh.

 

New Column( "Agg Minute",
	Numeric,
	"Ordinal",
	Format( "Best", 12 ),
	Set Display Width( 116 ),
	Formula(
		Local( {aggOption = 1},
			Choose( aggOption,
				If(
					Minute( :Timestamp ) < 15, 15,
					Minute( :Timestamp ) < 30, 30,
					Minute( :Timestamp ) < 45, 45,
					0
				),
				If(
					Minute( :Timestamp ) < 15, 0,
					Minute( :Timestamp ) < 30, 15,
					Minute( :Timestamp ) < 45, 30,
					45
				),
				If(
					0 < Minute( :Timestamp ) <= 15, 15,
					15 < Minute( :Timestamp ) <= 30, 30,
					30 < Minute( :Timestamp ) <= 45, 45,
					0
				)
			)
		)
	) 
	
);
jthi
Super User

Re: Setting Values in a Column with Choose

You are using For Each Row incorrectly. You don't send it to a column, it is a function you provide with (optional) data table and body argument and within body you can set values to columns.

 

Here is fixed version

Names Default To Here(1);

startTime = 3742070400;
endTime = 3742074000;

dt = New Table("Test",
	Add Rows(60),
	New Column("Timestamp", Format("m/d/y h:m", 19), Set Display Width(175))
);

For Each Row(:Timestamp[] = Sequence(startTime, endTime, 60)); 

aggOption = 1;

New Column("Agg Minute", Numeric, "Ordinal", Format("Best", 12), Set Display Width(116));


For Each Row(dt,
	:"Agg Minute"n = Choose(aggOption,
		If(
			Minute(:Timestamp) < 15, 15,
			Minute(:Timestamp) < 30, 30,
			Minute(:Timestamp) < 45, 45,
			0
		),
		If(
			Minute(:Timestamp) < 14, 0,
			Minute(:Timestamp) < 29, 15,
			Minute(:Timestamp) < 44, 30,
			45
		),
		If(
			0 < Minute(:Timestamp) <= 15, 15,
			15 < Minute(:Timestamp) <= 30, 30,
			30 < Minute(:Timestamp) <= 45, 45,
			0
		)
	)
);
-Jarmo