Excel VLOOKUP gives wrong value - excel

I have a VLookup cell which gives me the wrong value:
This is the table:
PID Product Price User User name Deal On Amount After
in 1001 table 1001 1 Milly No 1000
in 1001 table 100 13 Vernetta Yes 900
out 1001 table 50 14 Mireya No 900
out 1001 table 100 15 Rosanne Yes 1000
out 1001 table 101 16 Belinda No 1000
in 1001 table 200 1 Milly Yes 800
in 1234 chair 300 2 Charlena Yes 500
in 1234 chair 100 3 Mina Yes 400
in 1234 chair 50 4 Sabina Yes 350
in 8231 couch 20 5 Joni Yes 330
in 1001 table 150 6 Armando Yes 180
in 1001 table 100 7 Noemi Yes 80
in 8231 couch 40 8 Ashlie Yes 40
in 8231 couch 30 9 Ann Yes 10
out 1001 table 201 10 Angelina Yes 211
out 1234 chair 300 11 Melvina Yes 511
out 8231 couch 21 12 Mattie Yes 532
The product column is a VLOOKUP with the following formula
VLOOKUP(B6,$L$2:$M$10, 2)
B is the PID column. The table in L2:M10 is the following:
PID Prodcut
1001 table
1234 chair
8231 desk
2311 closet
9182 book_shelf
1822 bed
1938 coffee_book_table
2229 couch
Now as you can see. PID 8231 is a desk, but it appears as a couch. Can you see what the problem is?

The crux of your problem is the way you've written the formula, you forgot the last parameter, "FALSE" or 0 which means you want an EXACT MATCH. So your formula should look like this:
VLOOKUP(B6, $L$2:$M$10, 2, FALSE)
OR
VLOOKUP(B6, $L$2:$M$10, 2, 0)
Both do the same thing.
The default setting for it is TRUE, which looks for the closest match, which you only want if the index you're looking up off of is sorted.

Related

Unable to establish relationship for two fact tables with multiple many-to-many dimensions in Excel Data Model

I have 3 tables (FACT1, FACT2, DIM1, DIM2)
FACT1:
Code Month Value
058 1 500
059 1 600
061 1 700
058 2 1000
059 2 1000
061 2 1000
FACT2:
Service Month Status Value
058-buy 1 OK 700
059-purchase 1 Missing 800
061-trade 1 OK 900
058-buy 2 OK 300
059-purchase 2 Missing 400
061-trade 2 OK 500
DIM1:
Code Service
058 058-buy
059 059-purchase
061 061-trade
DIM2:
Month Name
1 January
2 February
3 March
I have all 4 tables loaded into Data Model in Excel and created a new measure in FACT1
Value Total:=sum([Value])+sumx(FILTER('FACT2','FACT2'[Status]="OK"),'FACT2'[VALUE])
I have also created relationship between tables:
FACT1(Code) to DIM1(Code)
FACT2(Service) to DIM1(Service)
FACT1(Month) to DIM2(Month)
FACT2(Month) to DIM2(Month)
However, when I insert a new pivot using the data model by having
Code from DIM1 on "ROW"
Month from FACT1 on "COLUMN"
New Measure Value Total on "Values"
I get something like this:
Code 1 2 Grand Total
058 1500 2000 2500
059 600 1000 1600
061 2100 2400 3100
Grand Total 4200 5400 7200
Somehow it will get sliced by Month properly, but if you pay attention to the last column of the pivot table Grand Total, they are correct! 2500 is indeed 500 + 700 + 1000 + 300, 1600 is indeed 600 + 1000, 3100 is indeed 700 + 900 + 1000 + 500
The final grand total for the above 3 is correct at 7200 too.
Now the question is why the middle part of the pivot tables acts oddly?
For your Pivot Table, use Month from the DIM2 table, not the FACT1 table. With your current set-up, FACT1 cannot filter the FACT2 table, though DIM2 can.

how to pass pandas series element to another dataframe

I want to check if an error occurred.
I have this two dataframes, from excel files:
Log_frame is a dataframe of log files, reporting data recording and error:
Time Voltage[V] Freq[Hz] Speed Motor_Stt: ErrNo
0 10:00 220 50 30 1 0
1 10:10 220 50 30 1 0
2 10:20 220 50 0 2 3601
3 10:30 220 47 0 1 1500
4 10:40 250 50 0 1 7707
5 10:50 220 50 0 2 3601
6 11:00 220 50 0 2 3601
7 11:10 220 47 0 1 1500
8 11:20 220 50 30 1 0
9 11:30 220 50 30 1 0
Dev_frame is the dataframe of error description:
Fehler-Nr. Descr Cause
0 1500 Chk_Voltage Voltage out of range
1 7707 Chk_Freq. Freq. out of range
2 3601 Chk_Motor_Stt Motor_defec
3 7704 switch_trip chk_over_curr
from Log_frame I can check if, which and how many errors occurred during a day by:
Err_log = Log_frame['ErrNo']
p = Err_log[Err_log != 0].drop_duplicates('first').reset_index(drop=True)
and this result is a pandas series:
<class 'pandas.core.series.Series'>
0 3601
1 1500
2 7707
I can "pass" first error (or second and all the other) by this:
Dev_Err = Dev_frame['Fehler-Nr.']
n = Dev_Err[Dev_Err == p.iloc[0]] #or 1, 2 and so on
I was wondering how to loop trough p.iloc[i].
Should I use a for loop or can be done by any pandas function
EDIT: e.g. if I put 1 in p.iloc[] I can get:
0 1500
if 2:
1 7707
No need to create a loop to check each value, you can use isin method that pandas.DataFrame has as following:
n = dev_frame[dev_frame['Fehler-Nr.'].isin(p)]['Fehler-Nr.']
which is going to return:
0 1500
1 7707
2 3601
Name: Fehler-Nr., dtype: int64
Ref: pandas.DataFrame.isin
If you're using pandas and going for for loops you are wrong. Use pandas vectorised operations. These are done using (simple exaple)
df.apply(some function, axis)
I'm not 100% convinced I understood what you're trying to achieve, but I believe you just want to merge/join number of errors for a given error. If so, pandas.join() and pandas.merge() are to help. Check the docs.

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()

SUMPRODUCT, CRITERIA and Blanks

I would like to multiply each fund monthly returns * monthly assets in the following snippet of data
FUND Jan_returns Feb_returns Jan_Assets Feb_Assets
1 -2 3 200 300
1 2 7 250 500
1 5 2 3000
2 6 5 500 600
2 8 900
2 9 1500 1500
3 -6 3 100 1000
3 -7 4 660 520
For example, FUND 1 Jan_returns * Jan_assets = 15100
The currently formula is:
=SUMPRODUCT(($B$1:$B$8)($B$1:$B$10=A2),($D$1:$D$8)($B$1:$B$8=A2))
Where A2 is a reference to the FUND.
This is working for January. However, When I do this for February I am getting #Value! for all 3 funds. I think it is because of the blanks and tried <>"" but just got strange numbers.
The results should be
FUND Jan Feb
1 15100 4400
2 16500 3000
3 -5220 5080
Any help in solving this problem is appreciated?
Like this, modify ranges as needed. Note that repeating the $B$1:$B$8=A2 is redundant* — you only need one instance.
=SUMPRODUCT(($A$2:$A$9=$G2)*B$2:B$9*D$2:D$9)
*I'm assuming the 10 in $B$1:$B$10=A2 is a typo and should be 8.

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.

Resources