4.7 Sort records with duplicate values by the specified order

 

In the following data tables, Header1 field stores index numbers and there could be duplicate values. As the following shows, there are several 101 (highlighted in the red boxes) in each Header1 field:

imagepng imagepng

We want to rearrange data in sheet data by the order of data in sheet Target. The rule, take 101 as an example, is that the first 101 corresponds to the first record where Header1 is 101, the second 101 corresponds to the second record where Header1 is 101, and the third 101 is recorded as an empty row because there isn’t a third matching record in sheet data.

SPL script:

A
1 =T(“data.xlsx”;“Target”).derive(:key)
2 = T(“data.xlsx”;“data”).derive(:key)
3 =A1.group(Header1).run(~.run(key=Header1/“_”/#))
4 =A2.group(Header1).run(~.run(key=Header1/“_”/#))
5 =A2.align(A1:key,key)

A1 Import data of sheet Target, and add a new column named key to generate key values to which records in sheet data will be aligned.
A2 Import data of sheet Data, and add a new column named key to generate key values to which records in sheet Target will be aligned.
A3 Group A1 by Header1, loop each group and each record in the group, and assign values to key field – which are Header1 value + an underlined character + its ordinal number in the group.
A4 Same as A3.
A5 Align A1’s key to A2’s key.

Execution result:

Header1 Header2 Header3
101 H2_1 H3_1
104 H2_8 H3_8
101 H2_5 H3_5
102 H2_2 H3_2
103 H2_3 H3_3
103 H2_6 H3_6