Order of occurrence of same value areas (formula

cerobit

New Member
Joined
Mar 7, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to generate special identifier (string) based on document prefix and some additional connecting data. I have tried various ways, but with no succes :cautious:

Please see example below with a form of desired output. Any additional helping columns can be used, but a single formula/column would be nice.

Thank you for your help :)

order_of_occurrence_of_areas.xlsx
ABCDEF
1Document prefixData connectorDesired output document prefix & order of occurrence of the same color/prefix (connected areas through data connector)Progress so far…
2A1A1A1
3A2A2A2
4B3B1B1
5C4C1C1
6B5B2B2
7A6A3A3
8B7B3B3
9A8A4A4
10A8A4A5
11B9B4B4
12B10B4B5
13C11C2C2
14C11C2C3
15C11C2C4
16A12A5A6
17A13A6A7
18C14C3C5
19C15C4C6
20B16B5B6
21B16B5B7
22B16B5B8
23A17A7A8
24C18C5C7
25C18C5C8
26C19C6C9
27A20A8A9
28A21A9A10
29A21A9A11
30A21A9A12
31A21A9A13
32A22A10A14
33B23B6B9
example
Cell Formulas
RangeFormula
F2:F33F2=A2 & IF(COUNTIF($A$2:$A$33, A2) > 1, COUNTIF($A$2:A2, A2), "")
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi @cerobit, Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

It seems to me that you have a issue in your expected results in rows 11 and 12, since the values in columns B are different and in column D you did not consider an increment..

1678232817226.png


I did the adjustment, following the pattern of the other examples and I show you the result below:

Dante Amor
ABCD
1Document prefixData connectorDesired output
2A1A1
3A2A2
4B3B1
5C4C1
6B5B2
7A6A3
8B7B3
9A8A4
10A8A4
11B9B4
12B9B4
13C11C2
14C11C2
15C11C2
16A12A5
17A13A6
18C14C3
19C15C4
20B16B5
21B16B5
22B16B5
23A17A7
24C18C5
25C18C5
26C19C6
27A20A8
28A21A9
29A21A9
30A21A9
31A21A9
32A22A10
33B23B6
Hoja6
Cell Formulas
RangeFormula
D2:D33D2=A2&SUMPRODUCT(($A$2:A2=A2)*(MATCH($A$2:A2&$B$2:B2, $A$2:A2&$B$2:B3,0)=ROW($B$2:B2)-1))


Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 1
Hi @DanteAmor !

Thank you for your reply.

You are right, the rows 11 and 12 are my bad, I have had an error in my problem's decription.

Your solution is exactly what I was trying to assemble. Than you again, this is awesome (y) :)

Regards,
cerobit
 
Upvote 1
Hello again,

may I ask you for a modification? I would like to skip some numbering based on an additional condition (non-zero values of a column). See below. Thank you.

order_of_occurrence_of_areas.xlsx
ABCDE
1Document prefixData connectorValueDesired output document prefix & order of occurrence of the same color/prefix (connected areas through data connector) When Value is zero, then only Prefix is displayed and the order is skipped. Order is not skipped, when there is/are zero/s, but even some other non-zero value/s within the same Prefix-Connector group (see cells C14, C15, C28).
2A1100A1
3A20A
4B3100B1
5C40C
6B5100B2
7A6100A2
8B70B
9A80A
10A80A
11B90B
12B100B
13C1150C1
14C110C1
15C110C1
16A12100A3
17A130A
18C14100C2
19C150C
20B160B
21B160B
22B160B
23A17100A4
24C180C
25C180C
26C19100C3
27A20100A5
28A210A6
29A2150A6
30A21100A6
31A21150A6
32A22100A7
33B23100B3
example (no zeros)
 
Upvote 0
For your original question, since you have Excel 365 I think you could use this simpler formula as it seems to produce the same results as given in post 2

25 01 03.xlsm
ABCD
1Document prefixData connectorDesired output
2A1A1
3A2A2
4B3B1
5C4C1
6B5B2
7A6A3
8B7B3
9A8A4
10A8A4
11B9B4
12B9B4
13C11C2
14C11C2
15C11C2
16A12A5
17A13A6
18C14C3
19C15C4
20B16B5
21B16B5
22B16B5
23A17A7
24C18C5
25C18C5
26C19C6
27A20A8
28A21A9
29A21A9
30A21A9
31A21A9
32A22A10
33B23B6
cerobit
Cell Formulas
RangeFormula
D2:D33D2=A2&ROWS(UNIQUE(FILTER(A$2:B2,A$2:A2=A2)))



For your follow-up question, this seems to work for the sample data but is pretty complex and I have not been able to come up with a simpler version without running into errors, incorrect results or circular references. :(
Anyway, see if it does what you want.

25 01 03.xlsm
ABCDE
1Document prefixData connectorValueResultsDesired
2A1100A1A1
3A20AA
4B3100B1B1
5C40CC
6B5100B2B2
7A6100A2A2
8B70BB
9A80AA
10A80AA
11B90BB
12B100BB
13C1150C1C1
14C110C1C1
15C110C1C1
16A12100A3A3
17A130AA
18C14100C2C2
19C150CC
20B160BB
21B160BB
22B160BB
23A17100A4A4
24C180CC
25C180CC
26C19100C3C3
27A20100A5A5
28A210A6A6
29A2150A6A6
30A21100A6A6
31A21150A6A6
32A22100A7A7
33B23100B3B3
cerobit (2)
Cell Formulas
RangeFormula
D2:D33D2=IF(C2=0,IF(COUNTIFS(A$2:A$33,A2,B$2:B$33,B2,C$2:C$33,">0"),XLOOKUP(A2&"|"&B2,A$1:A1&"|"&B$1:B1,D$1:D1,XLOOKUP(A2&"|"&B2,A3:A$34&"|"&B3:B$34,D3:D$34)),A2),A2&ROWS(UNIQUE(FILTER(A$2:B2,(A$2:A2=A2)*(C$2:C2>0)))))
 
Upvote 0
Solution
Hello, this is awesome, it makes exactly what I was hoping for. Thank you!

1) I just needed to make a small correction while in a real application the columns Prefix and Connector are not adjacent, so I replaced the reference A$2:B2 with more general A$2:A2 & B$2:B2.

2) Is there any reason why the references are sometimes one row more longer (A3:A$34, B3:B$34, D3:D$34)?
And similarly why are the reference sometimes one row shorter (A$1:A1, B$1:B1, D$1:D1)?

The whole formula seems to be working fine even for the first and last row when I replace the number 34 with 33 and the number 1 with 2 (except the D$1:D1).

I am curious because in a real application I am turning this formula to structured references style with named range data tables.

→ 3) See my attempt, I looks pretty overcomplicated (in column E), maybe it could be done more clean way...?

Anyway, it works, so I am glad.


order_of_occurrence_of_areas.xlsx
ABCDE
1PrefixConnectorValueResults (ref)Results (tab)
2A1100A1A1
3A20AA
4B3100B1B1
5C40CC
6B5100B2B2
7A6100A2A2
8B70BB
9A80AA
10A80AA
11B90BB
12B100BB
13C1150C1C1
14C110BB
15C110BB
16A12100A3A3
17A130AA
18C14100C2C2
19C150CC
20B160BB
21B160BB
22B160BB
23A17100A4A4
24C180CC
25C180CC
26C19100C3C3
27A20100A5A5
28A210A5A5
29A2150A6A6
30A21100A6A6
31A21150A6A6
32A22100A7A7
33B23100B3B3
example (no zeros) (3)
Cell Formulas
RangeFormula
D2:D32D2=IF( C2=0,IF(COUNTIFS( A$2:A$33, A2, B$2:B$33, B2, C$2:C$33,">0"),XLOOKUP( A2&"|"& B2, A$2:A2&"|"& B$2:B2, D$1:D1,XLOOKUP( A2&"|"& B2, A3:A$33&"|"& B3:B$33, D3:D$33)), A2), A2&ROWS(UNIQUE(FILTER( A$2:A2& B$2:B2,( A$2:A2= A2)*( C$2:C2>0)))))
E2:E33E2=IF( [@Value]=0,IF(COUNTIFS( [Prefix], [@Prefix], [Connector], [@Connector], [Value],">0"),XLOOKUP( [@Prefix]&"|"& [@Connector], OFFSET(t_data[[#Headers],[Prefix]],1,0):[@Prefix]&"|"& OFFSET(t_data[[#Headers],[Connector]],1,0):[@Connector], t_data[[#Headers],[Results (tab)]]:OFFSET([@[Results (tab)]],-1,0),XLOOKUP( [@Prefix]&"|"& [@Connector], OFFSET([@Prefix],1,0):INDEX([Results (tab)],ROWS(t_data))&"|"& OFFSET([@Connector],1,0):INDEX([Connector],ROWS(t_data)), OFFSET([@[Results (tab)]],1,0):INDEX([Results (tab)],ROWS(t_data)))), [@Prefix]), [@Prefix]&ROWS(UNIQUE(FILTER( OFFSET(t_data[[#Headers],[Prefix]],1,0):[@Prefix]& OFFSET(t_data[[#Headers],[Connector]],1,0):[@Connector],( OFFSET(t_data[[#Headers],[Prefix]],1,0):[@Prefix]= [@Prefix])*( OFFSET(t_data[[#Headers],[Value]],1,0):[@Value]>0)))))
D33D33=IF( C33=0,IF(COUNTIFS( A$2:A$33, A33, B$2:B$33, B33, C$2:C$33,">0"),XLOOKUP( A33&"|"& B33, A$2:A33&"|"& B$2:B33, D$1:D32,XLOOKUP( A33&"|"& B33, A$33:A34&"|" &#REF!, E$33:E34)), A33), A33&ROWS(UNIQUE(FILTER( A$2:A33& B$2:B33,( A$2:A33= A33)*( C$2:C33>0)))))
 
Upvote 0
Sorry, my bad, I haven't noticed, that there is a faulty value on line 28 of given result when the boundaries are shifted (34→33 and 1→2) . So your solution is correct.

I've updated even the "structured style", if somebody else would need this piece of work.

order_of_occurrence_of_areas.xlsx
ABCDEF
1PrefixConnectorValueResult (hard)Results (ref)Results (tab)
2A1-100A1A1A1
3A20AAA
4B3100B1B1B1
5C40CCC
6B5-100B2B2B2
7A6100A2A2A2
8B70BBB
9A80AAA
10A80AAA
11B90BBB
12B100BBB
13C11-100C1C1C1
14C110C1C1C1
15C110C1C1C1
16A12100A3A3A3
17A130AAA
18C14100C2C2C2
19C150CCC
20B160BBB
21B160BBB
22B160BBB
23A17-100A4A4A4
24C180CCC
25C180CCC
26C19100C3C3C3
27A20100A5A5A5
28A210A6A6A6
29A2150A6A6A6
30A21-100A6A6A6
31A21150A6A6A6
32A22100A7A7A7
33B23100B3B3B3
example (no zeros) (corr)
Cell Formulas
RangeFormula
E2:E33E2=IF( C2=0,IF(COUNTIFS( A$2:A$33, A2, B$2:B$33, B2, C$2:C$33,"<>0"),XLOOKUP( A2&"|"& B2, A$1:A1&"|"& B$1:B1, E$1:E1,XLOOKUP( A2&"|"& B2, A3:A$34&"|"& B3:B$34, E3:E$34)), A2), A2&ROWS(UNIQUE(FILTER( A$2:A2& B$2:B2,( A$2:A2= A2)*( C$2:C2<>0)))))
F2:F33F2=IF( [@Value]=0,IF(COUNTIFS( [Prefix], [@Prefix], [Connector], [@Connector], [Value],"<>0"),XLOOKUP( [@Prefix]&"|"& [@Connector], t_data[[#Headers],[Prefix]]:OFFSET([@Prefix],-1,0)&"|"& t_data[[#Headers],[Connector]]:OFFSET([@Connector],-1,0), t_data[[#Headers],[Results (tab)]]:OFFSET([@[Results (tab)]],-1,0),XLOOKUP( [@Prefix]&"|"& [@Connector], OFFSET([@Prefix],1,0):OFFSET(t_data[[#Headers],[Prefix]],ROWS(t_data)+1,0)&"|"& OFFSET([@Connector],1,0):OFFSET(t_data[[#Headers],[Connector]],ROWS(t_data)+1,0), OFFSET([@[Results (tab)]],1,0):OFFSET(t_data[[#Headers],[Results (tab)]],ROWS(t_data)+1,0))), [@Prefix]), [@Prefix]&ROWS(UNIQUE(FILTER( OFFSET(t_data[[#Headers],[Prefix]],1,0):[@Prefix]& OFFSET(t_data[[#Headers],[Connector]],1,0):[@Connector],( OFFSET(t_data[[#Headers],[Prefix]],1,0):[@Prefix]= [@Prefix])*( OFFSET(t_data[[#Headers],[Value]],1,0):[@Value]<>0)))))
 
Upvote 0
I've updated even the "structured style", if somebody else would need this piece of work.
What about this col H formula as a simpler (or at least shorter) "structured style" option. (I added a new column between prefix and Connector just to check that it worked fine when they were not adjacent as you mentioned)
This formula also avoids the volatile function OFFSET

cerobit.xlsm
ABCDGH
1PrefixColumn1ConnectorValueResults (tab)Results
2Adata 11-100A1A1
3Adata 220AA
4Bdata 33100B1B1
5Cdata 440CC
6Bdata 55-100B2B2
7Adata 66100A2A2
8Bdata 770BB
9Adata 880AA
10Adata 980AA
11Bdata 1090BB
12Bdata 11100BB
13Cdata 1211-100C1C1
14Cdata 13110C1C1
15Cdata 14110C1C1
16Adata 1512100A3A3
17Adata 16130AA
18Cdata 1714100C2C2
19Cdata 18150CC
20Bdata 19160BB
21Bdata 20160BB
22Bdata 21160BB
23Adata 2217-100A4A4
24Cdata 23180CC
25Cdata 24180CC
26Cdata 2519100C3C3
27Adata 2620100A5A5
28Adata 27210A6A6
29Adata 282150A6A6
30Adata 2921-100A6A6
31Adata 3021150A6A6
32Adata 3122100A7A7
33Bdata 3223100B3B3
cerobit (3)
Cell Formulas
RangeFormula
G2:G33G2=IF([@Value]=0,IF(COUNTIFS([Prefix],[@Prefix],[Connector],[@Connector],[Value],"<>0"),XLOOKUP([@Prefix]&"|"&[@Connector],t_data[[#Headers],[Prefix]]:OFFSET([@Prefix],-1,0)&"|"&t_data[[#Headers],[Connector]]:OFFSET([@Connector],-1,0),t_data[[#Headers],[Results (tab)]]:OFFSET([@[Results (tab)]],-1,0),XLOOKUP([@Prefix]&"|"&[@Connector],OFFSET([@Prefix],1,0):OFFSET(t_data[[#Headers],[Prefix]],ROWS(t_data)+1,0)&"|"&OFFSET([@Connector],1,0):OFFSET(t_data[[#Headers],[Connector]],ROWS(t_data)+1,0),OFFSET([@[Results (tab)]],1,0):OFFSET(t_data[[#Headers],[Results (tab)]],ROWS(t_data)+1,0))),[@Prefix]),[@Prefix]&ROWS(UNIQUE(FILTER(OFFSET(t_data[[#Headers],[Prefix]],1,0):[@Prefix]&OFFSET(t_data[[#Headers],[Connector]],1,0):[@Connector],(OFFSET(t_data[[#Headers],[Prefix]],1,0):[@Prefix]=[@Prefix])*(OFFSET(t_data[[#Headers],[Value]],1,0):[@Value]<>0)))))
H2:H33H2=LET(p,[@Prefix],pc,[Prefix],cc,[Connector],vc,[Value],r,MIN(FILTER(SEQUENCE(ROWS(t_data)),(vc<>0)*(pc=p)*(cc=[@Connector]),NA())),IF([@Value]=0,IFNA(INDEX([Results],r),p),p&ROWS(UNIQUE(FILTER(pc&cc,(pc=p)*(vc<>0)*(ROW(pc)<=ROW()))))))
 
Upvote 0
Very nice modification and optimization of a volatile function, thank you so much.

There is one more related issue, where a similar optimization would be useful.

I am highlighting these consequenting/grouped rows with a top&bottom border using conditional formating. To manage this I have added some helping columns.

In a real application these consequenting/grouped rows are less common, than a single non-connected data rows, so this kind of visual highlighting is clearer than its negative variant (like border on every row and no border for connected data).

In one of a helping columns I am also using the OFFSET function. Could it be replaced with some non-volatile function?

Cell Formulas
RangeFormula
D2:D36D2=LET( p,[@Prefix], pc,[Prefix], cc,[Connector], vc,[Value], r,MIN(FILTER(SEQUENCE(ROWS(t_data_with_border)),(vc<>0)*(pc=p)*(cc=[@Connector]),NA())), IF([@Value]=0, IFNA(INDEX([Results (tab) 2],r),p), p&ROWS(UNIQUE(FILTER(pc&cc,(pc=p)*(vc<>0)*(ROW(pc)<=ROW())))) ) )
E2:E36E2=LET( order_within_connected_data, IF(COUNTIF([Connector], [@Connector]) > 1, COUNTIF(t_data_with_border[[#Headers],[Connector]]:[@Connector], [@Connector]), 0), order_within_connected_data)
F2:F36F2=LET( order_within_connected_data, IF(COUNTIF([Connector], [@Connector]) > 1, COUNTIF(t_data_with_border[[#Headers],[Connector]]:[@Connector], [@Connector]), 0), is_it_connected_data, order_within_connected_data > 0, is_it_connected_data)
G2:G36G2=LET( border_follows, OR([@[Helper 2]] <> OFFSET([@[Helper 2]], 1, 0), AND([@[Helper 1]] > OFFSET([@[Helper 1]], -1, 0), OFFSET([@[Helper 1]], -1, 1) <> 0, [@[Helper 1]] > OFFSET([@[Helper 1]], 1, 0))), border_follows)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:G36Expression=$G2textNO
 
Upvote 0
I'm not sure that I am fully understanding what you are trying to achieve but try this as a replacement for Helper 3
Excel Formula:
=LET(next,IFERROR(INDEX([Helper 1],ROW()-ROW(t_data_with_border[#Headers])+1),0),OR(AND([@[Helper 1]]=0,next=1),[@[Helper 1]]>next))

Note that this does not use helper 2 so unless you are using that for something else, perhaps you don't need it any more?
 
Upvote 0

Forum statistics

Threads
1,225,739
Messages
6,186,738
Members
453,369
Latest member
juliewar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top