Excel auto increment id by 01 when cell changes value - excel

I have a file that has three columns. ID which is blank, Name which has some names and PARENT_ID which stores the parent ID of the Name.
What I want to do is at the column ID to take the Parent id and add a two digit number which will increment by 01. For example we have 10 cats with parent id 1. I want at the column ID to take the parent id "1" and then add "01" for the first cat, "02" for the second cat and so on. So at the column ID I will have foreach cat an auto incrementing value 101,102,...110.
Then the dogs start, so it will take the parent id which is "2" and start again foreach dog do add incrementig values 201,202... etc.
Then the fish 301,302
Here is an example of what I am trying to do.
ID NAME PARENT_ID
101 cat 1
102 cat1 1
103 cat2 1
104 cat3 1
105 cat4 1
106 cat5 1
107 cat6 1
108 cat7 1
109 cat8 1
110 cat9 1
111 cat10 1
201 dog 2
202 dog1 2
203 dog2 2
204 dog3 2
205 dog4 2
206 dog5 2
301 fish 3
302 fish 3
The column name is not of concern, I just placed it for you to understand better.
I am not familiar with visual basic and I tried to accomplish this with formulas but with no luck.
Thank you for any help.

Put this in A2 and copy/drag down:
=IF(C2<>C1,C2*100+1,A1+1)

Paste the below formula in "A2" =C2&RIGHT("00"&COUNTIF($C$1:C2,C2),2)
and drag the formula to down. if your data has more than 10 Unique Records then make it like =C2&RIGHT("00"&COUNTIF($C$1:C2,C2),3)

Not a VBA approach, but a formula approach -- If this is something like what you're looking for:
Row A B C D E F
1 ID NAME PARENT_ID RunningName RunningID NewID
2 101 cat 1 cat 0 cat
3 102 cat1 1 cat 1 cat01
4 103 cat2 1 cat 2 cat02
5 104 cat3 1 cat 3 cat03
6 105 cat4 1 cat 4 cat04
7 106 cat5 1 cat 5 cat05
8 107 cat6 1 cat 6 cat06
9 108 cat7 1 cat 7 cat07
10 109 cat8 1 cat 8 cat08
11 110 cat9 1 cat 9 cat09
12 111 cat10 1 cat 10 cat10
13 201 dog 2 dog 0 dog
14 202 dog1 2 dog 1 dog01
15 203 dog2 2 dog 2 dog02
16 204 dog3 2 dog 3 dog03
17 205 dog4 2 dog 4 dog04
18 206 dog5 2 dog 5 dog05
19 301 fish 3 fish 0 fish
20 302 fish 3 fish 1 fish01
...then I used the following formulas:
D2: =if(a2="","",if(sum(C2)<>sum(C1),trim(B2),trim(D1)))
E2: =if(a2="","",if(sum(C2)<>sum(C1),0,sum(E1)+1))
F2: =if(a2="","",trim(D2)&if(sum(E2)=0,"",text(E2,"00")))
I then replicated those cells down the column as far as I cared to go. You can make the "Running" columns a very light grey text color so as to render them non-distracting to the user.
Hopefully this can help inspire you to craft a solution that works for you.

Related

LISTAGG Partition for Webi

I would like to do something similar to oracle LISTAGG in Webi. Below are my Queries.
Query 1
Id M1 ; columns
1 10
2 20
3 30
4 40
5 50
Query 2
Id D1 ; column
1 A11
1 A12
1 A13
2 A21
2 A22
2 A23
2 A24
3 A31
wanted outcome by merging Query 1 and Query 2 By Id
Id M1 New Column
1 10 A11;A12;A13
2 20 A21;A22;A23;A24
3 30 A31
4 40
5 50
I can get to the point below. Then, use NoFilter to keep values intact when applying a filter. However, the column F2 has the values "#MULTIVALUE". I can get NoFilter to work with one query. But, with two queries like this, NoFilter doesn't work. Any suggestion to address the issue.
Id M1 F1 (Measure) F2
1 10 A11;A12;A13 =NoFilter([F1])
1 10 A11;A12;A13
1 10 A11;A12;A13
2 20 A21;A22;A23;A24
2 20 A21;A22;A23;A24
2 20 A21;A22;A23;A24
2 20 A21;A22;A23;A24
3 30 A31
4 40
5 50
I wonder if anyone could show me how to achieve this.
Many thanks for your help,
Andre

Distinct Count Duplicate Value

I need help for distinct count for duplicate value and output required as follows.
If Column A's value is 28 the result should be unique1(Column E) Else if Column A's value is 29 then the result should be Column F(Unique2)
Code Product Quantity Weight Unique1 Unique2
28 Apple 9 100 1 0
28 Orange 9 100 1 0
28 Apple 9 100 0 0
29 Apple 9 200 0 1
29 Apple 10 100 0 1
29 Apple 10 100 0 0
28 Orange 9 200 1 0
I tried Mr.tigeravatar code and it is working fine, but I don't know how to add an if ... else condition for checking and output in a different column.
For validation try this code: paste it in cell G2 or whatever preceding columns in row 2.
=IF(OR(AND(A2=28,E2=1,F2<>1),AND(A2=29,E2<>1,F2=1)),"Correct","Incorrect")

Sum of next n rows in python

I have a dataframe which is grouped at product store day_id level Say it looks like the below and I need to create a column with rolling sum
prod store day_id visits
111 123 1 2
111 123 2 3
111 123 3 1
111 123 4 0
111 123 5 1
111 123 6 0
111 123 7 1
111 123 8 1
111 123 9 2
need to create a dataframe as below
prod store day_id visits rolling_4_sum cond
111 123 1 2 6 1
111 123 2 3 5 1
111 123 3 1 2 1
111 123 4 0 2 1
111 123 5 1 4 0
111 123 6 0 4 0
111 123 7 1 NA 0
111 123 8 1 NA 0
111 123 9 2 NA 0
i am looking for create a
cond column: that recursively checks a condition , say if rolling_4_sum is greater than 5 then make the next 4 rows as 1 else do nothing ,i.e. even if the condition is not met retain what was already filled before , do this check for each row until 7 th row.
How can i achieve this using python ? i am trying
d1['rolling_4_sum'] = d1.groupby(['prod', 'store']).visits.rolling(4).sum()
but getting an error.
The formation of rolling sums can be done with rolling method, using boxcar window:
df['rolling_4_sum'] = df.visits.rolling(4, win_type='boxcar', center=True).sum().shift(-2)
The shift by -2 is because you apparently want the sums to be placed at the left edge of the window.
Next, the condition about rolling sums being less than 4:
df['cond'] = 0
for k in range(1, 4):
df.loc[df.rolling_4_sum.shift(k) < 7, 'cond'] = 1
A new column is inserted and filled with 0; then for each k=1,2,3,4, look k steps back; if the sum then less than 7, then set the condition to 1.

Find total no of links to and from node based on data in csv

I have a csv with the following info
Src Rx LinkId Weight
===================================
2 1 4000 10
2 1 4056 15
3 1 4100 10
3 1 4156 15
28 1 10650 8
113 2 15051 205
113 3 15058 205
1 4 3952 9
1 4 3951 5
1 4 3950 34
2 4 4052 9
47 4 18672 44
47 4 18670 38
69 4 4701 11
69 4 4700 21
70 4 4801 11
`
The linkId is unique. Each row represents the link between two devices. For example, source 2 and rx 1 means that a link goes from 2 to 1.
I intend to compute the total weight of all the links originating from each device and coming into each device like so:
Device Out weight In weight
=============================
2 25 205
1 48 58
and so on.
I would like to know if doing this is possible in excel. If yes, how.
Using a pivot table may be the best solution here and I think that if you select this table and click pivot-table it will give you your answer.
Alternatively, you can make a column for each in and out and use =sumif(Src, 1, weight ) and then use the totals at the bottom of each column.

Excel SUMIF based on array using text string

Is there a way to substitute the cell address containing a text string as the array criteria in the following formula?
=SUM(SUMIF(A5:A10,{1,22,3},E5:E10))
So instead of {1,22,3}, "1, 22, 3" is entered in cell A2 the formula becomes
=SUM(SUMIF(A5:A10,A2,E5:E10))
I have tried but get 0 as a result (refer C16)
A B C D E F G H
1 Tree
2 {1,22,3} 1
3 22
4 Tree Profit 3
5 1 105
6 2 96
7 1 105
8 1 75
9 2 76.8
10 1 45
11
12 330 =SUM(SUMIF(A5:A10,{1,22,3},B5:B10))
13
14 330 =SUMPRODUCT(SUMIF(A5:A10,E2:E3,B5:B10))
15
16 0 =SUM(SUMIF(A5:A10,A2,B5:B10))
17 NB: Custom Format "{"#"}" on Cell A2 I enter 1,22,3 so it displays {1,22,3}
Ok so after some further searching (see Excel string to criteria) and trial and error I have come up with the following solution.
Using Name Manager I created UDF called GetList which Refers to:
=EVALUATE(Sheet1!$A$3) NB: Cell A3 has this formula in it =TEXT(A2,"{#}")
I then used the following formula:
=SUMPRODUCT(SUMIF($A$5:$A$12,GetList,$B$5:$B$12))
which gives the desired result of 321 as per the other two formulas (see D12 below).
If anyone can suggest a better solution then feel free to do so.
Thanks to Dennis to my original post regarding table
A B C D E
1 Tree
2 1,22,3 1
3 {1,22,3} =TEXT(A2,"{#}") 22
4 Tree Profit 3
5 11 105
6 22 96
7 1 105
8 3 75
9 2 76.8
10 1 45
11
12 321 =SUMPRODUCT(SUMIF($A$5:$A$12,GetList,$B$5:$B$12))
13
14 321 =SUM(SUMIF(A5:A10,{1,22,3},B5:B10))
15
16 321 =SUMPRODUCT(SUMIF(A5:A10,E2:E3,B5:B10))
17
18 0 =SUM(SUMIF(A5:A10,A2,B5:B10))
19 NB: Custom Format "{"#"}" on Cell A2 I enter 1,22,3 so it displays {1,22,3}

Resources