Adding values to list using vlookup - excel

I have a big list like:
Actual:(sheet "A") Expected:(Sheet "B")
A Values A Values
1 5 1 5
3 12 2 0
5 11 3 12
4 0
5 11
I used formula =vlookup(A2, A!A2:A!B3, 2, FALSE) in B2 of sheet B and it returns "#N/A"

In Sheet B, A2, use this: =IfError(vlookup(A2,A!$A:$B,2,0),"No Match")

Related

Excel Lag Two Group

STUDENT
TIME
CLASS
SCORE
WANT
1
1
A
13
NULL
1
1
B
4
NULL
1
2
A
11
-2
1
2
B
9
5
1
3
A
8
-3
2
2
B
16
NULL
2
3
B
6
-10
2
4
A
7
NULL
2
4
B
6
0
I have XLSX file with STUDENT, TIME, CLASS, SCORE. I wish to calculate WANT which does this:
For every STUDENT and CLASS, calculate the difference in SCORE from TIME(X) TO TIME(X-1).
for STUDENT=1, TIME=2,CLASS=B equals to 5 because it is (9-4)
I try this with no success:
=IF(A3=A2 & C3=C2, OFFSET(D3, -1, 0), "")
I think you can try:
Formula in E2:
=IF(COUNTIFS(A$2:A2,A2,C$2:C2,C2)>1,D2-SUMIFS(D:D,A:A,A2,B:B,B2-1,C:C,C2),"Null")
It is far from the best approach, but it works.
If using helper column is not a problem, you can make additional column for VLOOKUP (see column "Helper1") with formula =TEXTJOIN("-",,A2:C2).
Now use VLOOKUP to find value TEXTJOIN("-",,A2,B2-1,C2) in that column. Formula in "WANT" column: IFNA(E2-VLOOKUP(TEXTJOIN("-",,A2,B2-1,C2),$D$2:$E$10,2,FALSE),"NULL")
Result:

Grouped Sum on complicated calculated fields in other column

I have an excel sheet with data (Sheet1). First number is a secuencial number representing a number of month.
Sheet1 <month, year, data1, data2>
[first row: titles]
1 1 data11 data12
2 1 data21 data22
3 1 data31 data32
4 1 data41 data42
5 1 data51 data52
6 1 data61 data62
7 1 data71 data72
8 1 data81 data82
9 1 data91 data92
10 1 data101 data102
11 1 data111 data112
12 1 data121 data122
13 2 data131 data132
14 2 data141 data142
Sheet2
[month, year, formule]
1 1 sheet1!C2-3*sheet1!B1
2 1 sheet1!C3-3*sheet1!B2
3 1 sheet1!C4-3*sheet1!B3
4 1 sheet1!C5-3*sheet1!B4
5 1 sheet1!C6-3*sheet1!B5
6 1 sheet1!C7-3*sheet1!B6
7 1 sheet1!C8-3*sheet1!B7
8 1 sheet1!C9-3*sheet1!B8
9 1 sheet1!C10-3*sheet1!B9
10 1 sheet1!C11-3*sheet1!B10
11 1 sheet1!C12-3*sheet1!B11
12 1 sheet1!C13-3*sheet1!B12
13 2 sheet1!C14-3*sheet1!B13
14 2 sheet1!C15-3*sheet1!B114
Sheet3
[year, Sum of column C in sheet2 grouped by year]
Firts row <year,formule>
1 =SUMIF(sheet2!B$2:B$15, A2, sheet!C$2:C$15)
2 =SUMIF(sheet2!B$2:B$15, A3, sheet!C$2:C$15)
My question, Can I remove and do the calculation in Sheet3
I can if the column C of sheet2 is moved to sheet1 but I don't want to put many columns in sheet1 because Sheet2 has many columns. If we can remove Sheet2, we removing a lot of formula (in this example 14 + 2 formules -> only 2 formules)
Thanks
Solved: The year is in column 2 then
=SUMPRODUCT((Sheet1!B$2:B$424=Sheet3!B2)*(Formula using $2:$424 in each column of the mensual formula))

how to sort a pandas dataframe according to elements of list [duplicate]

I have the following example of dataframe.
c1 c2
0 1 a
1 2 b
2 3 c
3 4 d
4 5 e
Given a template c1 = [3, 2, 5, 4, 1], I want to change the order of the rows based on the new order of column c1, so it will look like:
c1 c2
0 3 c
1 2 b
2 5 e
3 4 d
4 1 a
I found the following thread, but the shuffle is random. Cmmiw.
Shuffle DataFrame rows
If values are unique in list and also in c1 column use reindex:
df = df.set_index('c1').reindex(c1).reset_index()
print (df)
c1 c2
0 3 c
1 2 b
2 5 e
3 4 d
4 1 a
General solution working with duplicates in list and also in column:
c1 = [3, 2, 5, 4, 1, 3, 2, 3]
#create df from list
list_df = pd.DataFrame({'c1':c1})
print (list_df)
c1
0 3
1 2
2 5
3 4
4 1
5 3
6 2
7 3
#helper column for count duplicates values
df['g'] = df.groupby('c1').cumcount()
list_df['g'] = list_df.groupby('c1').cumcount()
#merge together, create index from column and remove g column
df = list_df.merge(df).drop('g', axis=1)
print (df)
c1 c2
0 3 c
1 2 b
2 5 e
3 4 d
4 1 a
5 3 c
merge
You can create a dataframe with the column specified in the wanted order then merge.
One advantage of this approach is that it gracefully handles duplicates in either df.c1 or the list c1. If duplicates not wanted then care must be taken to handle them prior to reordering.
d1 = pd.DataFrame({'c1': c1})
d1.merge(df)
c1 c2
0 3 c
1 2 b
2 5 e
3 4 d
4 1 a
searchsorted
This is less robust but will work if df.c1 is:
already sorted
one-to-one mapping
df.iloc[df.c1.searchsorted(c1)]
c1 c2
2 3 c
1 2 b
4 5 e
3 4 d
0 1 a

Conditionally highlighting totals that don't match a dynamically summed range

Objective:
I am looking to use conditional formatting to highlight cells in rows that are not equal to the sum of a dynamic range.
Problem:
While the formula I have created seems to work when pasted into cells, it does not give the same results when entered as a conditional formula.
Example:
Here is a space delimited example to be pasted into "A1":
Allo d1 d2 d3 d4 d5
Total 10 10 10 10 10
A 9 9 10 10 9
B 0 0 0 0 0
C 0 1 0 0 0
Total 12 12 12 12 12
B 0 5 0 3 4
C 12 7 8 8 8
Total 12 12 12 12 12
A 0 0 0 0 0
B 0 0 0 0 0
C 0 5 0 3 4
D 12 7 8 8 8
I wrote this formula which shows TRUEs and FALSEs correctly when pasted into "H2" and dragged to the right and down to "L13." When I apply this formula to the data range "B2:F13" it does not mimic what I'd expect.
=IF($A2="TOTAL", B2 <> SUM(INDIRECT(ADDRESS(ROW(B3),COLUMN(B3),4)&":"&ADDRESS(ROW(B2)+IFERROR(MATCH("TOTAL",$A3:$A$13,0)-1,ROW($A$13)-ROW($A2)),COLUMN(B2),4))))
Below you can see the formula broken out in a more easy to read way. Is my formula flawed/ How can I accomplish what I am trying to do? I appreciate your thoughts.
=IF($A2="TOTAL",
B2 <> SUM(
INDIRECT( ADDRESS( ROW(B3),
COLUMN(B3),
4) &":"&
ADDRESS( ROW(B2) + IFERROR(
MATCH( "TOTAL", $A3:$A$13, 0)-1,
ROW($A$13)-ROW($A2)),
COLUMN(B2),4))))
Use OFFSET instead:
=IF($A2="TOTAL",B2<> SUM(OFFSET(B3,0,0,IFERROR(MATCH("TOTAL",$A3:$A$13,0)-1,ROWS($A3:$A$13)),1)))

excel:compare 2 columns and copy data on other columns

need help.. i trying to compare 2 columns and copy data in other columns..
Columns:
A B C D
1 3 10
2 4 20
3 1 30
4 2 40
5 0 50
i want to compare column A to B to find its duplicate and copy data from column C if column A has a duplicate at column B...
Result must be:
A B C D
1 3 10 0
2 4 20 40
3 6 30 10
4 2 40 20
5 0 50 0
thanks in advance...
An answer as I understand the question (assuming the change in col B is just a typo):
Input
A B C D
1 3 10
2 4 20
3 6 30
4 2 40
5 0 50
Output
A B C D
1 3 10 0
2 4 20 40
3 6 30 10
4 2 40 20
5 0 50 0
Formula in D2 (filled down): =IF(COUNTIF(B$2:B$6, $A2)>0, VLOOKUP($A2,$B$2:$C$6, 2, FALSE), 0).
COUNTIF(B$2:B$6, $A2) returns the number of times the value in A2 appears in the array B2:B6. If this value is greater than 0 (meaning that A2 is in B2:B6), the IF() function looks looks up A2 in col B and returns the value in the 2nd row (col C); if A2 is not in B2:B6, the formula returns 0.

Resources