In Excel, Insert Group Headers to Detail Data Rows in Each Group

esProcesProc
5 min read

Problem description & analysis

The Excel worksheet below contains multiple vertical subtable groups, which are separated by a blank row. In each group, the 2nd cells of both row 1 and row 2 contain subtable group headers and row 3 contains column headers; there isn’t detailed data in either the 1st column or the 6th column:

ABCDEF
1ATLANTIC SPIRIT
2Looe
3VesselSpeciesSizeKgDateLocation
4POLLACK22.523/04/2024
5POLLACK318.823/04/2024
6POLLACK415.423/04/2024
7LING31.923/04/2024
8WHITING20.423/04/2024
9
10BEADY EYE
11Plymouth
12VesselSpeciesSizeKgDateLocation
13BASS415.723/04/2024
14BASS53.223/04/2024
15
16BOY JACK
17Plymouth
18VesselSpeciesSizeKgDateLocation
19PLAICE10.823/04/2024
20BLONDE RAY114.323/04/2024
21BLONDE RAY31.623/04/2024
22SPOTTED RAY51.223/04/2024
23THORNBACK RAY16.323/04/2024
24THORNBACK RAY215.723/04/2024
25THORNBACK RAY310.923/04/2024
26THORNBACK RAY42.623/04/2024
27LOBSTER12.723/04/2024
28LOBSTER21.123/04/2024
29RAY BACKS142.123/04/2024

We need to insert the subtable group headers in row 1 and row 2 of each group into the 1st column and the 6th column respectively:

ABCDEF
1ATLANTIC SPIRIT
2Looe
3VesselSpeciesSizeKgDateLocation
4ATLANTIC SPIRITPOLLACK22.523/04/2024Looe
5ATLANTIC SPIRITPOLLACK318.823/04/2024Looe
6ATLANTIC SPIRITPOLLACK415.423/04/2024Looe
7ATLANTIC SPIRITLING31.923/04/2024Looe
8ATLANTIC SPIRITWHITING20.423/04/2024Looe
9
10BEADY EYE
11Plymouth
12VesselSpeciesSizeKgDateLocation
13BEADY EYEBASS415.723/04/2024Plymouth
14BEADY EYEBASS53.223/04/2024Plymouth
15
16BOY JACK
17Plymouth
18VesselSpeciesSizeKgDateLocation
19BOY JACKPLAICE10.823/04/2024Plymouth
20BOY JACKBLONDE RAY114.323/04/2024Plymouth
21BOY JACKBLONDE RAY31.623/04/2024Plymouth
22BOY JACKSPOTTED RAY51.223/04/2024Plymouth
23BOY JACKTHORNBACK RAY16.323/04/2024Plymouth
24BOY JACKTHORNBACK RAY215.723/04/2024Plymouth
25BOY JACKTHORNBACK RAY310.923/04/2024Plymouth
26BOY JACKTHORNBACK RAY42.623/04/2024Plymouth
27BOY JACKLOBSTER12.723/04/2024Plymouth
28BOY JACKLOBSTER21.123/04/2024Plymouth
29BOY JACKRAY BACKS142.123/04/2024Plymouth

Solution:

Use SPL XLL to enter the formula below:

=spl("=t=?.group@i(!~.ifn()),k=1,t.run(t1=~(k)(2),t6=~(k+1)(2),~.m(3+k:).run(~(1)=t1,~(6)=t6),k=2),t.conj()",A1:F29)

As shown in the picture below:

r/esProc_Desktop - In Excel, Insert Group Headers to Detail Data Rows in Each Group

Explanation:

group@i()function groups rows according to the specified condition; ifn() function returns the first non-null member; ~ represents is the current member and ~(6) represents the 6th member on the current member’s subordinate level; and m(i:) gets members from the ith to the last one.

10
Subscribe to my newsletter

Read articles from esProc directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

esProc
esProc

esProc SPL is a JVM-based programming language designed for structured data computation, serving as both a data analysis tool and an embedded computing engine. FREE download👉🏻: https://www.esproc.com/download-esproc