switch function Excel - excel

I am working on an Excel switch funciton but something is not working.
I have 2 tables. This is my table 1 -
And this is my Table 2 -
Now, what is want is - when the table 1 "Pick value" Column has a value of A it should enter code herepick the value from Table 2 column value A Value when the value is B, it should pick B Value, else C value from the corresponding field.
That IS WHY I am using this switch query in Excel -
=SWITCH([Pick Value];"A";Table2[A Value];"B";Table2[B Value];Table2[C Value];)
But it is not working -
Anyone knows what I am doing wrong here!

Here is a SWITCH version also using XLOOKUP:
=XLOOKUP([#ID];Table2[ID];SWITCH([#[Pick Value]];"A";Table2[A Value];"B";Table2[B Value];"C";Table2[C Value]))

Use FILTER:
=#FILTER(CHOOSE(MATCH([#Pick Value];{"A";"B";"C"};0);Table2[A Value];Table2[B Value];Table2[C Value]);Table2[ID]=[#ID])

INDEX/MATCH
If you don't have 365, you can use one of the following formulas.
Comma
Array formula (CRTL+SHIFT+ENTER):
=IFERROR(INDEX(Table2[#[A Value]:[C Value]],MATCH([#[Pick Value]],LEFT(Table2[[#Headers],[A Value]:[C Value]],1),0)),"")
or if using "A","B","C" explicitly (ENTER):
=IFERROR(INDEX(Table2[#[A Value]:[C Value]],MATCH([#[Pick Value]],{"A","B","C"},0)),"")
Semi Colon
Array formula (CRTL+SHIFT+ENTER):
=IFERROR(INDEX(Table2[#[A Value]:[C Value]];MATCH([#[Pick Value]];LEFT(Table2[[#Headers];[A Value]:[C Value]];1);0));"")
or if using "A","B","C" explicitly (ENTER):
=IFERROR(INDEX(Table2[#[A Value]:[C Value]];MATCH([#[Pick Value]];{"A";"B";"C"};0));"")

Related

How to look at a cell's text and assign different values depending on what the text is

This is a classic IF ELSE statement problem but I deciding to just use a LOOKUP so it's easier to understand:
I want to look at the value of cell D1230 and have the following instructions:
If text is either: "CE BSW", "Datalinks", "CE SAC, Infra" -> Assign "1"
If text is either: "PFT","CFT" -> Assign "2"
If text is either: "AHCC","SIC (Spark Igntion Controls)","Fuel Systems" –> Assign "3"
This is my formula:
=LOOKUP(D1230,{"CE BSW";"Datalinks";"CE SAC, Infra";"PFT";"CFT";"AHCC";"SIC"},{"1";"1";"1";"2";"2";"3";"3"})
Unfortunately it's not working and I'm getting a #N/A error, any help would be greatly appreciated.
Use IFS:
=IFS(
OR(D1230={"CE BSW", "Datalinks", "CE SAC, Infra"}),1,
OR(D1230={"PFT","CFT"}),2,
OR(D1230={"AHCC","SIC (Spark Igntion Controls)","Fuel Systems"}),3,
TRUE,"")
Another method:
=INDEX({1;1;1;2;2;3;3},
MATCH(D1230,{"CE BSW";"Datalinks";"CE SAC, Infra";"PFT";"CFT";"AHCC";"SIC"},0))
The difference between this and your LOOKUP is that LOOKUP requires the lookup range be sorted ascending. MATCH with 0 as the third criterion will look for an exact match and does not require them to be sorted.

Excel AVERAGEIFS looking up ONE of the criteria columns

I have built a large data set and I need to see the average results given many different criteria. I've done this with the AVERAGEIFS function and it works just fine, however the more and more I add its getting really time intensive.
I'm wondering if there is a way to nest a vlookup or index match or anything like that in the AVERAGEIFS that read the criteria column heading and criteria in a cell (or 2 if they need to be separated) to be added to the AVERAGEIFS.
Here is an example of my spreadsheet:
The first 3 sets of criteria I want to stay locked.
I want it to read what the 4th criteria column and criteria should be by referencing the I11 cell. The highlighted portion in the formula bar is the part that I want to reference I11 so it reads it and knows that the 4th criteria is the 'code' column and the criteria is '>7'. I can separate this into 2 separate cells if need be.
I've tried a few combinations of VLOOKUP and INDEX MATCH but cannot get it to work.
Data as Text:
Price,Type,sub cat,Time,code,amount,Result,,
,,,,,,,,
9.95,t2,d,ac,2.18," 22,780,893 ",0.73,,T2 and D and AC
118.94,u2,d,bo,2.78," 172,110,893 ",4.07,,
57.63,t1,u,ac,7.09," 128,419,877 ",-2.16,,code
8.88,t2,d,ac,1.50," 62,634,868 ",12.72,,amount < 100 000 000
11.61,u1,u,ac,2.14," 146,982,736 ",1.07,,price >10
13.46,u3,u,ac,0.93," 17,513,672 ",-13.93,,
31.53,t1,u,ac,0.89," 47,170,877 ",1.39,,
16.34,t3,d,bo,1.07," 1,914,767,076 ",-1.42,,
111.59,u1,d,bo,0.62," 2,283,546,000 ",0.67,,
72.4,u3,d,bo,10.37," 951,541,514 ",1.13,,
34.55,u3,d,bo,0.77," 951,541,514 ",-2.52,,
42.25,t1,d,bo,1.05," 63,748,352 ",8.88,,
17.18,u3,u,ac,2.64," 140,217,257 ",4.35,,
97.66,t1,d,bo,3.45," 1,070,383,954 ",1.33,,
58.49,t2,u,bo,8.64," 151,876,559 ",-0.92,,
64.48,t2,d,ac,2.35," 291,967,334 ",3.03,,
38.4,t1,u,ac,17.05," 83,478,472 ",-4.31,,
20.87,u3,d,ac,28.92," 214,080,937 ",-2.16,,
36.53,t1,d,ac,1.43," 73,438,589 ",-2.07,,
89.16,t3,u,ac,1.41," 26,786,958 ",-1.75,,
15.84,t1,u,bo,2.90," 133,560,818 ",1.76,,
3.2,u3,u,bo,2.95," 215,677,667 ",-1.06,,
25.46,t1,d,bo,3.92," 57,148,431 ",1.89,,
40,t2,d,ac,8.00," 65,274,903 ",0.61,,
27.72,t1,u,ac,2.50," 381,400,886 ",6.46,,
29.07,u3,u,ac,2.32," 52,632,107 ",-0.78,,
173.31,t1,d,ac,3.58," 31,547,380 ",-4.92,,
18.22,u3,d,ac,0.58," 292,669,493 ",4.06,,
9.59,t1,d,bo,3.60," 266,883,020 ",3.16,,
115.22,t2,d,bo,4.51," 132,376,476 ",0.78,,
64.48,u3,d,ac,3.03," 338,360,104 ",-0.95,,
41.74,t1,u,bo,25.65," 245,766,436 ",-3.42,,
5.99,t3,u,bo,2.15," 175,054,713 ",-4.37,,
Use INDEX/MATCH to return the correct column. This will require that you separate the column name and the criteria:
=AVERAGEIFS(G:G,B:B,"T2",C:C,"D",D:D,"AC",INDEX(A:F,0,MATCH(I11,$A$7:$G$7,0)),J11)
An idea:
I10 - "Write down the limitation. (You have to use <,>,=,<> AND the value, for e.g.: <5)"
I11 - The user can use relations and values.
In J11, you can reference to I11 ;) It works for me.

Using tbl.Lookup to match just part of a column value

This question relates to the Schematiq add-in for Microsoft Excel.
Using =tbl.Lookup(table, columnsToSearch, valuesToFind, resultColumn, [defaultValue]) the values in the valuesToFind column have a consistent 3 characters to the left and then varying characters after (e.g. 908-123456 or 908-321654 - i.e. 908 is always consistent)
How can I tell the function to lookup the value based on the first 3 characters only? The expected answer should be the sum of the results of the above, i.e. 500 + 300 = 800
tbl.Lookup() works by looking for an exact match - this helps ensure it's fast but in this case it means you need an extra step to calculate a column of lookup values, something like this:
A2: =tbl.CalculateColumn(A1, "code", "x => LEFT(x, 3)", "startOfCode")
This will give you a new column that you can use for the columnsToSearch argument, however tbl.Lookup() also looks for just one match - it doesn't know how to combine values together if there is more than one matching row in the table, so I think you also need one more step to group your table by the first 3 chars of the code, like this:
A3: =tbl.Group(A2, "startOfCode", "amount")
Because tbl.Group() adds values together by default, this will give you a table with a row for each distinct value of startOfCode and the subtotal of amount for each of those values. Finally, you can do the lookup exactly as you requested, which for your input table will return 800:
A4: =tbl.Lookup(A3, "startOfCode", "908", "amount")

Excel - 2 tables - If 2 cells in a single row match, return another cell of same row

Working with 2 separate data sets (with duplicates)
Dataset is unique identified by an ID.
There may not be an entry for the timestamp I require.
Datasets are quite large, and due to duplicates, can't use vlookup.
Samples:
Table 1:
Device Name|Time Bracket| On/Off?
ID1 |06:20:00 |
ID2 |06:20:00 |
ID3 |06:30:00 |
Table 2:
Device Name |Timestamp |On/Off?
ID1 |06:20:00 |On
ID2 |06:50:00 |Off
ID3 |07:20:00 |Off
What I want to achieve:
I want an if statement to check if:
1) device ID matches AND
2) timestamp matches
If so, return the value of On/Off from Table 2.
If not, then I want it to return the value of the cell above it IF it's the same device, otherwise just put "absent" into the cell.
I thought I could do this with some IF statements like so:
=if(HOUR([#[Time Bracket]]) = HOUR(Table13[#[Timestamp Rounded (GMT)]]) and
minute([#[Time Bracket]]) = minute(Table13[#[Timestamp Rounded (GMT)]]) and
[#[Device Name]]=Table13[#[Device Name]], Table13[#[On/Off?]],
IF([#[Device Name]]=Table13[#[Device Name]], INDIRECT("B" and Rows()-1), "absent"))
(I put some newlines in there for readability)
However, this doesn't seem to resolve at all... what am I doing wrong?
Is this even the correct way of achieving this?
I've also tried something similar with a VLookUp, but that failed horribly.
Thanks all!
To not deal with array formulas or merging strings which, (not in your case) can still be wrong at the end, I suggest the use of COUNTIFS due to the fact, you have a very small amount of outcomes (just on or off)...
for the first table (starting at A1, so the formula is at C2):
=IFERROR(CHOOSE(
OR(COUNTIFS(Table13[Device Name],[#[Device Name]],Table13[Timestamp],[#[Time Bracket]],Table13[On/Off?],"On"))+
OR(COUNTIFS(Table13[Device Name],[#[Device Name]],Table13[Timestamp],[#[Time Bracket]],Table13[On/Off?],"Off"))*2
,"On","Off","Error"),IF(A1=[#[Device Name]],C1,"Absent"))
this will also show "Error" of a match for "On" and "Off" is shown... to skip that and increase the speed, you also could use:
=IF(COUNTIFS(Table13[Device Name],[#[Device Name]],Table13[Timestamp],[#[Time Bracket]],Table13[On/Off?],"On"),"On",
IF(COUNTIFS(Table13[Device Name],[#[Device Name]],Table13[Timestamp],[#[Time Bracket]],Table13[On/Off?],"Off"),"Off",
IF(A1=[#[Device Name]],C1,"Absent")))
For both the "Device Name" is at column A, "Time Bracket" at column B and "On/Off?" at column C while the table starts at row 1... If that is not the case for you, then change A1 and C1 so they match
(Also inserted line-breaks for better reading)
Picture to show the layout:
I picked the second formula to show how it works... also, this formula should not be able to return 0's... I'm confused
Couple of good suggestions, however using the helper column as suggested in the topic by Scott Craner above worked.
Created a helper column of concat'd device ID and timestamp for both tables, then did a simple VlookUp.
Another lesson learned: Think outside of the box, and go with simple solutions, rather than try + be too clever like I was doing... :)

Find value in another table, return row index

I'm inexperienced in Excel functions and need some help with the following scenario:
Table I has 2 columns:
X Y
3.2 result
4.7 result
1.2 result
Table II has these columns:
A B C D
1 1.2 0.0 2.3
2 4.1 3.2 0.0
3 0.0 4.7 0.0
I will try to illustrate what I want to do in C++/C#/Java programming syntax:
In Y[1] I want to store:
foreach (int value in TableII) //look for X[1] in Table 2
if( value == X[1] ){ //when you find it,
return A[value.rowNumber]; //return the corresponding value from A
//break;
}
In other words, I want to find X[1] in table II (it will always be there, and always just once), and return the corresponding value from column A[of the same index as the match].
I've looked at HLOOKUP but there's more to it that just one command. Please help.
[EDIT]
I've tried this =INDEX(Column1,IFERROR(MATCH(I2,Column2,0),IFERROR(MATCH(I2,Column3,0),IFERROR(MATCH(I2,Column4,0),IFERROR(MATCH(I2,Column5,0),IFERROR(MATCH(I2,Column6,0),IFERROR(MATCH(I2,Column7,0),IFERROR(MATCH(I2,Column8,0),MATCH(I2,Column9,0)))))))))
But I get the "more levels of nesting than are allowed in the current file format" error. If I remove just one IFERROR(MATCH(I2,Column8,0), then it works. But I need Columns 2 to 9, with Column1 holding the index.
I thought Excel 2007 and 2010 do not have nesting limits any more. What gives?
(For anyone new to Excel reading this, "Column1" etc. are made by selecting the column, right clicking on the selection, and "Define Name".)
[EDIT2]
So I "solved" my nesting problem by dividing the formula into 2 parts, with a +, having the ifs return 0 in case there's no match in either side. This way you either have something + 0 or 0 + something, so it works.
Here's the formula I'm using right now:
=INDEX(Column1, IFERROR(MATCH(I3,Column2,0),IFERROR(MATCH(I3,Column3,0),IFERROR(MATCH(I3,Column4,0),IFERROR(MATCH(I3,Column5,0),IFERROR(MATCH(I3,Column6,0),IFERROR(MATCH(I3,Column7,0),0))))))
+
IFERROR(MATCH(I3,Column8,0),IFERROR(MATCH(I3,Column9,0),0))
)
I've accepted Chris' answer, as he did most of the work, but I would still like to know if my workaround is good practice and if there isn't a shorter/better way to do this.
Thank you.
Here's a possible formula for Y (assumes table I is located in columns G:H)
=INDEX($A:$A,IFERROR(MATCH(G2,$B:$B,0),IFERROR(MATCH(G2,$C:$C,0),MATCH(G2,$D:$D,0))))
Note the IsError function was introduced in Ecel 2007
If your tables are actually defined as excel tables you can use
=INDEX(Table2[Column1],IFERROR(MATCH([#X],Table2[Column2],0),IFERROR(MATCH([#X],Table2[Column3],0),MATCH([#X],Table2[Column4],0))))
(substitute your own table and column names)
If your values are unique, you can use the following formula without having to do any nesting:
=INDEX(column1,SUMPRODUCT((Table2=G24)*ROW(Table2)))

Resources