Excel find multiple values and copy them - excel

I'm trying to make Up-Sells list for my personal web shop but cannot figure it out.
I have a lot of items in my database and I'm trying to sort them. I have unique ID's for categories and I need to paste every SKU which is under that category.
Input/Output
Input
SKU Category
123 1
124 1
234 2
235 2
Output SKU Category Up-Sell
123 1 123, 124
124 1 123, 124
234 2 234, 235
235 2 234, 235

Related

Excel RANGE VLOOKUP

STUDENT TIME SCORE WANT
JOHN 1 68 146
JOHN 2 78 146
JOHN 3 77 146
JOHN 4 91 146
JOHN 5 96 146
JAMES 1 66 119
JAMES 2 53 119
JAMES 3 80 119
JAMES 4 96 119
JAMES 5 50 119
JAMES 6 94 119
I have data COLUMNS 'STUDENT' AND 'TIME' AND 'SCORE' and wish to create 'WANT' and the rule which for I will need VLOOKUP is this: WANT = the sum of the SCORE values at TIMES 1 and 2, so I WISH TO USE VLOOKUP to find the 'SCORE' values for each 'STUDENT' at TIMES 1 and 2 and take the sum.
You can try SUMIFS() in this way.
=SUM(SUMIFS($C$2:$C$12,$B$2:$B$12,{1,2},$A$2:$A$12,A2))
It may need to array entry for older versions of excel. Array entry by CTRL+SHIFT+ENTER.
Assuming your dataset is ordered by "student name" (with unique student names), then "time", you could use :
Classical way, in F2 :
=IF(AND(B2=1,B3=2,A2=A3),C2+C3,IF(AND(B2=2,B1=1,A2=A1),C2+C1,OFFSET($F$1,MATCH(A2,A$2:A2,0),0)))
Greedy way (Office365 needed), in E2 :
=SUM(FILTER($A$2:$C$12;($B$2:$B$12<=2)*($A$2:$A$12=A2)))-3
Reference :

Pandas: Better way to combine rows for 'wide' dataset?

I'm trying to make a 'wide' dataset, with one record per game, rather than one record per team, per game. Here's a small example of what I have, first, and then what I'd like to have.
GAME-ID TEAM SCORE
0 123 Cleveland 95
1 123 Orlando 101
2 124 New York 104
3 124 Detroit 98
GAME-ID TEAM1 TEAM2 SCORE1 SCORE2
0 123 Cleveland Orlando 95 101
1 124 New York Detroit 104 98
I can set a flag for game id count (see below), then later use a for loop to iterate through and set values conditionally, but thought there may be an easier way.
import pandas as pd
dict1 = {'GAME-ID':[123, 123, 124, 124],
'TEAM':['Cleveland', 'Orlando', 'New York', 'Detroit'],
'SCORE':[95, 101, 104, 98]}
df = pd.DataFrame(dict1)
df['GAME_ID_CT'] = df.groupby('GAME-ID').cumcount() + 1
print(df)
Result from code above:
GAME-ID TEAM SCORE GAME_ID_CT
0 123 Cleveland 95 1
1 123 Orlando 101 2
2 124 New York 104 1
3 124 Detroit 98 2
If there's a way to do this by column rather than a bunch of loops, it would be great.
You can try pivot:
new_df = df.pivot(index='GAME-ID',columns='GAME_ID_CT')
# rename
new_df.columns = [f'{a}{b}' for a,b in new_df.columns]
Output:
TEAM1 TEAM2 SCORE1 SCORE2
GAME-ID
123 Cleveland Orlando 95 101
124 New York Detroit 104 98
I think this actually worked best for me. It's simple and accommodates lots more variables.
df1 = df[df['GAME_ID_CT'] == 1]
df2 = df[df['GAME_ID_CT'] == 2]
new_df = pd.merge(df1, df2, on='GAME-ID', suffixes=['1', '2'])
print(new_df)
GAME-ID TEAM1 SCORE1 GAME_ID_CT1 TEAM2 SCORE2 GAME_ID_CT2
0 123 Cleveland 95 1 Orlando 101 2
1 124 New York 104 1 Detroit 98 2

Cumulatively Reduce Values in Column by Results of Another Column

I am dealing with a dataset that shows duplicate stock per part and location. Orders from multiple customers are coming in and the stock was just added via a vlookup. I need help writing some sort of looping function in python that cumulatively decreases the stock quantity by the order quantity.
Currently data looks like this:
SKU Plant Order Stock
0 5455 989 2 90
1 5455 989 15 90
2 5455 990 10 80
3 5455 990 20 80
I want to accomplish this:
SKU Plant Order Stock
0 5455 989 2 88
1 5455 989 15 73
2 5455 990 10 70
3 5455 990 20 50
Try:
df.Stock -= df.groupby(['SKU','Plant'])['Order'].cumsum()

Excel auto increment id by 01 when cell changes value

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.

How to use COUNTX to find number of occurences of variable

This question pertains to PowerPivot.
I am currently designing an invoices table to include a column called "Hits", which counts the amount of times a single customer has purchased one item.
I currently have a table like so:
INVOICES
INVOICE ID CUSTOMER ID PRODUCT ID
321 1 444
322 2 411
323 3 221
324 4 321
325 5 444
326 5 444
This is what I would like it do look like:
INVOICES
INVOICE ID CUSTOMER ID PRODUCT ID HITS
321 1 444 1
322 2 411 1
323 3 221 1
324 4 321 1
325 5 444 2
326 5 444 2
As you can see, the row "hits" calculates how many times that particular customer has ordered that particular product.
Thanks for any help!
Assuming data in columns A, B and C try this formula in D2 copied down
=COUNTIFS(B:B,B2,C:C,C2)
That gives you exactly what you want - a count of rows where customer and product both match the current row

Resources