cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

Add Counter for Unique Cases in Groups

Hi all, 

How can I write an integer value counter to increment unique values in column B within group A, to do this?

StumptownSlash_0-1683660565893.png

Here is what I have tried so far:

 

dt_Table1 = Data Table("Table1");
dt_Table1 << New Column("Dataset", Numeric, "Nominal", Set Selected);

Sum1 = dt_Table1 << Summary(Group(:Group, :Date));
Sum2 = Sum1 << Summary(Group(:Group));
:N Rows << Set Name("N Datasets in Group");

dt_Table1 << Update(
	with(Data Table(Sum2)),
	Merge Same Name Columns
Match Flag(0),
	Suppress foruma evaluation(0),
	By Matching Columns(:Group = :Group),
	Drop multiples(1, 0),
	Include Nonmatches(0, 0),
	Preserve main table order(1)
);

dt = dt_Table1;
For(i = 1, i <= :N Rows(dt), i++,
	While(Group_Number = GetValue(:Group[i]),
		N == Get Value(dt, :N Datasets in Group[i]),
		For(j = 1, j <= N, j++,
			:Dataset << Set Value == (j),

		),
		Group_Number = Group_Number + 1,

	),

);

Outputs:

StumptownSlash_0-1683663388740.png

 

StumptownSlash_0-1683662459921.png

 

 

10 REPLIES 10
hogi
Level XI

Re: Add Counter for Unique Cases in Groups

amazing!
Depending on the structure of the table, many orders of magnitude faster

 

but still far away from a solution for the right click New Formula Column menu

Names Default To Here( 1 );
dt = new table("Example",
New Column("A", character),
	New Column("B", character)
);
dt << add rows(20000);
for each row(
	:A = char(random integer(1,5));
	:B = char(random integer(1,1000));
);

t0 = hptime();
dt << New Column( "count",
	set each value(
		As Constant( Summarize( dt, bygroup = by( :a, :b) ) );
		N Items( Loc( bygroup[1], :a ) );
	)
);
(hptime() -t0)/1000000; //~ 1min

t0 = hptime();
dt << New Column("C", set each value(
	Col Sum(Row() == Col Min(Row(), :A, :B), :A);	
));
(hptime() -t0)/1000000; // < 1sec