cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Choose Language Hide Translation Bar
View Original Published Thread

How to filter rows of a data table with dynamical number of conditions?

Robbb
Level III

 

Hi everybody,

 

I would like to select rows based on the entries of selected rows from another table. Below a code snippet. The last row is not valid, but it should clarify what I mean.

 

selected_row_idx = DT << Get Selected Rows;
measurement_starts = DT:MeasurementStart[selected_row_idx];
measurement_ends = DT:MeasurementEnd[selected_row_idx];
DTmeas << Select Where(measurement_starts[1] <= :TimeStamp <= measurement_ends[1] & measurement_starts[2] <= :TimeStamp <= measurement_ends[2] & ... & measurement_starts[n] <= :TimeStamp <= measurement_ends[n]);

How can I write the last row in some dynamical way, so that I have as many conditions there connected with "&" as I have rows selected in table DT? I could write a loop and filter subsets iteratively, but this is kind of lame. Or I could put together the last row as a string and parse and execute it, but that is not nice, either. Is there an elegant way to do this?

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User


Re: How to filter rows of a data table with dynamical number of conditions?

I think you could use matrices, sql, building expression,...

Names Default To Here(1);

dt1 = New Table("Untitled",
	Add Rows(3),
	Compress File When Saved(1),
	New Column("A", Numeric, "Continuous", Format("Best", 12), Set Values([1, 1, 5])),
	New Column("B", Numeric, "Continuous", Format("Best", 12), Set Values([2, 3, 6]))
);
dt1 << Select Rows(1::3);
starts = dt1[dt1 << get selected rows, {"A"}];
ends = dt1[dt1 << get selected rows, {"B"}];
dt1 << clear select;

dt2 = New Table("Untitled 2",
	Add Rows(5),
	Compress File When Saved(1),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([1, 2, 3, 4, 5]))
);

dt2 << Select Where(
	N Items(Loc((starts <= :Column 1)` + (ends >= :Column 1)` >= 2)) > 0
)
-Jarmo

View solution in original post

2 REPLIES 2
jthi
Super User


Re: How to filter rows of a data table with dynamical number of conditions?

I think you could use matrices, sql, building expression,...

Names Default To Here(1);

dt1 = New Table("Untitled",
	Add Rows(3),
	Compress File When Saved(1),
	New Column("A", Numeric, "Continuous", Format("Best", 12), Set Values([1, 1, 5])),
	New Column("B", Numeric, "Continuous", Format("Best", 12), Set Values([2, 3, 6]))
);
dt1 << Select Rows(1::3);
starts = dt1[dt1 << get selected rows, {"A"}];
ends = dt1[dt1 << get selected rows, {"B"}];
dt1 << clear select;

dt2 = New Table("Untitled 2",
	Add Rows(5),
	Compress File When Saved(1),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([1, 2, 3, 4, 5]))
);

dt2 << Select Where(
	N Items(Loc((starts <= :Column 1)` + (ends >= :Column 1)` >= 2)) > 0
)
-Jarmo
Robbb
Level III


Re: How to filter rows of a data table with dynamical number of conditions?

Very nice, thank you!

Recommended Articles

No recommendations found