Home » excel » Excel Generating a formula based sequence based on levels

# Excel Generating a formula based sequence based on levels

Questions:

What I am trying to achieve is the following

`````` ID            Name       Level
1             ABC           1
11            ABC           2
11001         ABC           3
1100101       ABC           4
110010101     ABC           5
11001010101   ABC           6
11002         ABC           3
1100201       ABC           4
110020101     ABC           5
11002010101   ABC           6
11002010102   ABC           6
110020102     ABC           5
11002010201   ABC           6
11002010202   ABC           6
11002010203   ABC           6
2             ABC           1
21            ABC           2
21001         ABC           3
2100101       ABC           4
210010101     ABC           5
210010102     ABC           6
21002         ABC           3
``````

I have the following code in excel to generate this code but I m not able to achieve the above, can anyone help me with this, please find below the formula I am using:

``````=CONCATENATE(COUNTIF(\$D\$9:D18,1),IF(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=1,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,2),COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=1,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,2),""),IF(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=2,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,3),TEXT(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=2,ROW(D9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,3),"000"),""),IF(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=3,ROW(D9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,4),TEXT(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=3,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,4),"00"),""),IF(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=4,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,5),TEXT(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=4,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,5),"00"),""),IF(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=5,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,6),TEXT(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=5,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,6),"00"),""),IF(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=6,ROW(D9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,7),TEXT(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=6,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,7),"00"),""),IF(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=7,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,8),TEXT(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=7,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,8),"00"),""),IF(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=8,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,9),TEXT(COUNTIF(INDIRECT("D"&MAX(IF(INDIRECT(ADDRESS(9,4)):INDIRECT(ADDRESS(ROW(),4))=8,ROW(\$D\$9:INDIRECT(ADDRESS(ROW(),4))),0))):D18,9),"000"),""))
``````

Basically I am trying to generate the code in the ID field automatically, let me explain how it works: ID are set using the Levels field for Level 1 its the first digit in the ID field it represents, for Level 2 its the 2nd digit for level 3 its the 3 digits after 2 digits , and afterwards 2 digits for each levels. Furthermore once a level 1 has been assigned it cannot to assigned to another level 1 therefore the second level 1 will be give the next number, its first level 2 will have 1 as number and 2nd level 2 within will give it 2 and the same goes for the following code.

The problem with my code is that it calculates the value fine till the first level 1 and its below code but however its start behaving incorrectly as I go down the codes, it should not print the level code for 4,5,6 and more if I am generating a level 3. these are some of the problems I am facing

I can do it although I use a helper column. It could all go in one cell but it gets way longer then a formula I would like to see

If you have levels in column A you will calculate a decimal ID in column B and an actual ID in column C

cell B2=1 (since the formula doesn’t work on the first of the series)

other cells in column B use the formula

`=INDIRECT("B"&MAX(ROW(\$A\$2:A2)*(\$A\$2:A2<=A3)))+INDEX({1,0.1,0.0001,0.000001,0.00000001,0.0000000001},MATCH(A3,{1,2,3,4,5,6},0))`

Entered with `Ctrl` + `Shift` + `Enter`

This formula finds the value of the decimal ID from the last row that has a level less then or equal to itself (indirect part)

the index looks up the Level and adds the right amount to the value found in the first part of the formula.

Column C uses the formula

`=B2*10^IF(B2=INT(B2),0,LEN(B2)-FIND(".",B2))`

which just rounds the number up to a integer by finding the number of decimal points (if statement) and then raising 10 to that power and multiplying by the decimal ID.

If you really need to do it in one column you could paste the whole first column everywhere you see `B2` in the second formula but that would make the formula totally illegible.

Gordon

PART 2

As per the request for more levels I’ve had to use a different technique which deals mainly with text.

Instead of using the arrays built right into the formulas I created a seperate table which makes it a little easier to handle, you could reference this with named ranges, a seperate chart or just put the numbers right in the formula if you want.

For the cells you enter the Level and sigfigs.

Digits kept is sigfigs from the level above minus 1 (`M3=L2-1`)

Digit Add start is the sigfigs from level above (`N3=L2`)

Digits add length = sigfigs from level – sigfigs from level above +1 (`O3=L3-L2+1)

For the actual index formula the first row has the value 1 again and then in cell B3 use

`=LEFT(B2,INDEX(\$M\$2:\$M\$12,MATCH(A3,\$K\$2:\$K\$12,0))) & MID(B2&"00000",INDEX(\$N\$2:\$N\$12,MATCH(A3,\$K\$2:\$K\$12,0)),INDEX(\$O\$2:\$O\$12,MATCH(A3,\$K\$2:\$K\$12,0)))+1`

To break this down

`=LEFT(B2,INDEX(\$M\$2:\$M\$12,MATCH(A3,\$K\$2:\$K\$12,0)))`

means keep the left part of the index above. Find the length to keep from the digits keep column

`MID(B2&"00000",INDEX(\$N\$2:\$N\$12,MATCH(A3,\$K\$2:\$K\$12,0)),INDEX(\$O\$2:\$O\$12,MATCH(A3,\$K\$2:\$K\$12,0)))+1`

means add a bunch of zeros to the end so you can use a level higher then the current one. Find the digit you need to start with, and the number of digits you want to use. Add 1 to this value.

The & sign between them will concatenate them together.

Place `1`in `A2`.
In cell `A3` and below, place this formula:
``````=CHOOSE(C3,