formula to find value from two separate tables and based on values in three tables - excel

I'm looking for a formula for the Party column in Table 3 that will produce its values based on the data contained in Table 1 and Table 2.
NumSelect value in Table 3 determines Party value in Table 3.
Where NumSelect has "p", it refers to data in Table 1. If no "p" in NumSelect, then it refers to Table 2.
Number in NumSelect refers to row number.
If the corresponding ShortName has a value, that value should be returned.
If the corresponding ShortName is blank, then the corresponding Name should be returned.
Uppercase "P" and lowercase "p" in the NumSelect should both point to Table 1.
Each table is an Excel Table and its rows may expand or contract.
Certain rows in Table 1 and Table 2 may be empty.
Formula should not be volatile, not require control+shift+enter to enter the formula, and not require VBA.
Thanks!
Sorry for the bad formatting. I had this question formatted perfectly, but Stack Overflow kept preventing me from posting it because it claimed, "Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon."
Table 1
Name
Gender
ShortName
Occupation
Grace Turner
F
Singer
Cadie Crawford
F
Tiger
Fine Artist
Paige Johnston
F
Archeologist
Dexter Payne
M
Klondike
Veterinarian
Valeria Barnes
F
Chef
Florrie Reed
F
Lawer
Emily Ferguson
F
Scientist
Sam Hawkins
M
Alpha
Biochemist
Savana Ellis
F
Cook
Table 2
Name
Gender
ShortName
Occupation
Vanessa Cooper
F
Producer
Jasmine Morris
F
Beta
Baker
Evelyn Taylor
F
Economist
Adelaide Roberts
F
Historian
Blake Cunningham
M
Lion
Chef
Adelaide Harrison
F
Chemist
Frederick Watson
M
Journalist
Table 3
NumSelect
Party
p2
Tiger
3
Evelyn Taylor
P8
Alpha
2
Beta
7
Frederick Watson
p7
Emily Ferguson

Long Formula
Your formula has 717 characters, this one has 347.
=IF(ISNUMBER(SEARCH("P",[#NumSelect])),
IF(INDEX(Table1[ShortName],VALUE(RIGHT([#NumSelect],1)))="",
INDEX(Table1[Name],VALUE(RIGHT([#NumSelect],1))),
INDEX(Table1[ShortName],VALUE(RIGHT([#NumSelect],1)))),
IF(INDEX(Table2[ShortName],[#NumSelect])="",
INDEX(Table2[Name],[#NumSelect]),
INDEX(Table2[ShortName],[#NumSelect])))
A pseudo-code could look like this:
=IF(ISNUMBER(A),IF(B="",C,B),IF(D="",E,D))
The issue is that B (lines 2 & 4) and D (lines 5 & 7) are repeated expressions.
Hopefully, this will help someone to make a major improvement.
Microsoft 365
Using the LET function, you could use the following:
=LET(iIndex,[#NumSelect],sIndex,VALUE(SUBSTITUTE(LOWER(iIndex),"p","")),
IF(LEN(iIndex)>LEN(sIndex),
LET(nShort,INDEX(Table1[ShortName],sIndex),nLong,INDEX(Table1[Name],sIndex),
IF(nShort="",nLong,nShort)),
LET(nShort,INDEX(Table2[ShortName],sIndex),nLong,INDEX(Table2[Name],sIndex),
IF(nShort="",nLong,nShort))))

Welp, I figured out the formula. But it's very inefficient. I'm sure someone here could make it a lot shorter and more efficient.
Here it is:
=IF(
INDEX(FILTER(CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[[Name]:[ShortName]],Table2[[Name]:[ShortName]]),CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[Name],Table2[Name])<>""),SUBSTITUTE(LOWER([#NumSelect]),"p",""),3)
=0,
INDEX(FILTER(CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[[Name]:[ShortName]],Table2[[Name]:[ShortName]]),CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[Name],Table2[Name])<>""),SUBSTITUTE(LOWER([#NumSelect]),"p",""),1),
INDEX(FILTER(CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[[Name]:[ShortName]],Table2[[Name]:[ShortName]]),CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[Name],Table2[Name])<>""),SUBSTITUTE(LOWER([#NumSelect]),"p",""),3)
)

Related

How to condense column data in excel at a change in the row data

I'm not sure exactly how to explain this, but I want to combine rows in Excel while condensing the column data. I'm looking to take a spreadsheet like this:
Name
Coffee
Bananas
Chocolate Ice Cream
Tom
4
Tom
2
Tom
2
Jerry
1
Jerry
4
Amanda
3
Amanda
2
and turn it into this:
Name
Coffee
Bananas
Chocolate Ice Cream
Tom
4
2
2
Jerry
1
4
Amanda
3
2
Any tips would be super helpful.
You can setup a pivot table for this.
Select your table > Insert Pivot Table, then follow this screenshot:
Drag Name colum in Rows
Drag Coffee, Bananas, Chocolate ice creams in Values and you're set!
If you want the data transformed and cleansed, you can always use PowerQuery.
Go to Data -> Get Data -> From File -> whatever source your data is in.
In there, use the Group By function to suppress the rows.
From there, you'll have a new set of data which can then be loaded into the destination worksheet.
If the usage of PowerQuery is new to you, a quick Google search will help you through this.
Naturally, this assumes you have a version of Excel that has PQ built in.
Here/screenshot(s) refer:
Condensed table comprises 3 sections (colour coded respectively):
Row labels (names, blue) - assuming these are unique at source
Column labels (food product, green) - ditto
Values (sum per row / col label, yellow)
note: included an extra '1' for Tom + Chocolate IC in 1st row for testing...
1] Row labels
=UNIQUE(D7:D13)
(array function - requires Office 365 compatible version of Excel)
2] Col labels
=E6:G6
(array function - enter in single cell if using Office 365 compatible version of Excel, otherwise highlight relevant area and enter function with shift + alt + enter)
3] Values
=SUMIFS(INDEX($E$7:$G$13,0,MATCH(J$6,$E$6:$G$6,0)),$D$7:$D$13,$I7)
(can drag this function across values / yellow section)

Is there a way to distribute data according to a logic in Excel vba?

I have an Excel sheet with the below data.
There are 10,000 Data rows.
9000 are of "USA" & 1000 are of "Other" country.
I want to evenly distribute the data so that when I have 9 "USA" followed by 1 "Other" data distributed throughout.
Name
Country
Alice
USA
Brook
Other
Cathy
USA
David
USA
Esther
Other
Freddy
USA
Galin
USA
Henry
Other
Indigo
USA
Jenny
USA
Kalin
Other
Linda
USA
How do I accomplish this using manual & excel VBA? Appreciate both solutions. Thanks
This can be achieved with a formula if you have the newest version of Excel.
Try something like (adapt ranges and what you are filtering on as necessary):
=LET(x, FILTER($B$1:$C$12, $C$1:$C$12="a"),
y, FILTER($B$1:$C$12, $C$1:$C$12="b"),
z, ROW(D1:D12), myrows, MAX(z),
ratio, MAX((COUNTA(x)/2)/(COUNTA(y)/2), (COUNTA(y)/2)/(COUNTA(x)/2))+1,
IF(MOD(z,ratio)<>0,
INDEX(x, IF(MOD(SEQUENCE(myrows),ratio)=0, 0, SEQUENCE(myrows)-CEILING(ROW(G1:G12)/ratio-1,1)), SEQUENCE(1,2)),
INDEX(y, IF(MOD(SEQUENCE(myrows),ratio)<>0,0,SEQUENCE(myrows)/ratio), SEQUENCE(1,2))))
For example:
The trick is to create the "correct" sequence for each result; for the first array you want to skip every nth row (in your case 10), and having the nth+1 row not default to n+1, but n, while in the second array you want to skip every row that isn't a some multiple of n, and have the nth rows count sequentially.
A caveat-- as is, I don't believe the formula will work with repetition other than 1, i.e. if you want to do something like 8 rows followed by 2 rows, this won't work.
This works even with older Excel versions:
If this is your data:
Add a Sort column with the following formula in C2 and pull it down:
=IF(B2="USA",COUNTIF($B$2:B2,"USA")+INT((COUNTIF($B$2:B2,"USA")-1)/ROUNDUP(COUNTIF(B:B,"USA")/(COUNTA(B:B)-COUNTIF(B:B,"USA")),0)),COUNTIF($B$2:B2,"Other")*(ROUNDUP(COUNTIF(B:B,"USA")/(COUNTA(B:B)-COUNTIF(B:B,"USA")),0)+1))
Then sort by this column C and USA and Other are evenly spread:

Using Google Sheets (or MS Excel) need formula to sum columns to a third column with conditions

I am developing a hockey scoresheet for my son's youth team. I need to figure out how to sum goals by period. I have up to 16 goals per game. Each is identified by period and some other data not relevant to this question (Jersey, assists, etc.). Once the data is entered, I want to show total goals by period. I need help with this part.
Specifically, I need a formula which will look at column A (Period) and if the condition is met add the value in column B (which will typically, but not always be 1) to column G (Period 1 Total). Also, do the same for columns C & D and E & F. Always adding the value to column G.
Columns A,C,D are the period the goal is scored. Columns B,D,F is the point value added. (In reality, my spreadsheet does this with 16 possible goals, but for illustrative purposes, I'm only showing 3 possible goals.)
So, Column G will have a formula that says,
If A = 1 then add B to G
If C = 1 then add D to G
If E = 1 then add F to G
G could have a result of 0 to 3 depending how many goals are scored.
My belief is that additional Columns H and I will represent periods 2 and 3. These two columns will use the same formula as G, except filtering for "2" or "3" in columns A,C,D.
I hope the examples help make sense of the request. Thank you.
Example 1: 3 goals scored in first period
---Goal 1------------Goal 2-----------Goal 3-------Period 1
Period Goal----Period Goal----Period Goal----Goals
--A--------B--------C-------D---------E-------F---------G
--1--------1---------1-------1----------1-------1----------3
Example 1: 2 goals scored in first period
---Goal 1------------Goal 2-----------Goal 3-------Period 1
Period Goal----Period Goal----Period Goal----Goals
--A--------B--------C-------D---------E-------F---------G
--1--------1---------1-------1---------- ------- ----------2
Example 3: 2 goals scored in first period 1 goal scored in period 2
---Goal 1------------Goal 2-----------Goal 3-------Period 1
Period Goal----Period Goal----Period Goal----Goals
--A--------B--------C-------D---------E-------F---------G
--1--------1---------1-------1----------2-------1----------2
You can use SUMIFS()
=SUMIFS(B3:F3,$B$2:$F$2,"Goal",A3:E3,1)
Turns out it was much easier than I thought. I was overthinking things.
=SUM(IF(A1=1,B1),IF(C1=1,D1),IF(E1=1,D1))
Thanks for viewing. I hope this helps someone else out.

Excel --- Tried many time but fail

enter image description here
I want to sum all these data given in right side to my format. I tried many time and many formulas but some things left every time. Please help
You can achieve most of what you need with a mixture of formulae and a pivot table. But I couldn't quite get everything you wanted, as some of it doesn't make sense. For example, you have 2013 - 2012 - <5 as your first row header, but there are multiple years that give an age less than 5, e.g. 2014 - 2013 - <5 would also be valid. I would suggest hardcoding the years somehow, as they aren't entirely relevant anyway?
Breaking it down into separate problems.
How do I get the Roman number version of the Class?
You could have a massive IF statement, but I went for a VLOOKUP. You could have used the ROMAN built in function, but you can't because your numbering is "first", "second", etc. instead of "1", "2", etc.
So I actually went for both solutions, just to show how they both work. First I created a named table (list object) called "Named" that looks like this:
Class Class Number
First 1
Second 2
Third 3
Fourth 4
etc.
I then used this to VLOOKUP your class names and convert them into numbers. Next I used the ROMAN function to make this into roman numeral format. So I ended up with this (columns E, F and G in my worksheet):
Class Class Number Roman
First 1 I
First 1 I
First 1 I
First 1 I
First 1 I
Second 2 II
Third 3 III
Fourth 4 IV
With formulae:
Class Number - =VLOOKUP(E2,Named,2,FALSE);
Roman - =ROMAN(F2).
How do I get from "Male"/ "Female" to "B"/ "G"?
This was trivial, just =IF(A2="Male", "B", "G") where I have Gender in column A.
How do I get the "age text" so it handles "<5" and ">22"?
My age was in column D, so this was also trivial, =IF(D2<5,"<5", IF(D2>22, ">22", D2)).
Now I have enough data to pivot, so I selected my entire table, which was basically your sample data with calculated columns added to the right. Then I inserted a pivot table and dragged in row/ column headers to make it match your format. This doesn't give you the year columns, but it gives you everything else. For example, just using a few random rows of sample data (as I couldn't be bothered to type it all in):
I went from:
Gender Date of Birth Age Class
Male 06-Jan-14 4 First
Female 07-Sep-11 6 First
Male 01-Jan-12 6 First
Male 31-Dec-12 5 First
Female 01-Oct-11 6 First
Female 16-Nov-10 7 Second
Male 31-Oct-09 8 Third
Male 25-Oct-10 7 Fourth
To:
I II III IV
B G G B B
<5 1
5 1
6 1 2
7 1 1
8 1
Total 3 2 1 1 1
From here you could simply hardcode the columns A and B from your desired final format, as these years aren't actually based on the data?

Excel - Transposing unsorted items

So lets say that I have a table like this..
TID Person Type Name
1 Andy F Orange
2 Andy M Beef
3 Andy V Carrot
4 Andy V Spinach
5 Bobby M Ham
6 Bobby F Apple
7 Bobby V Carrot
I want to transpose it so that it will be sorted according to the Type, I want it to look like so
Person F M V
Andy Orange Beef Carrot
Bobby Apple Ham Carrot
How can I manage to do this? Oh, and I'll also point some stuff in case you guys missed it:
The Types have no particular order, if you notice Andy's, the order is F M V V, but Bobby's is M F V.
Multiple instances of Type may occur, just like in Andy's case, notice the double V. But even so, I want it so that the only V that counts is the first one, thats why in the transposed table, the V is Carrot, because the Carrot occurred first (the Spinach is ignored).
I dont know if I ask too much, but even just the gist of the solution would be very helpful for me. The main point of my question is to ask how can I transpose such unsorted items, whilst paying attention to the 1st point. The 2nd point is important too, but I can wait or ask later if you guys dont feel like answering.
Thanks for reading, please share me your knowledge.
The easisest/quickest way is to create a new column before the TID column which has this formula in it.
=[Person]&"_"&[Type]
For instance say your data started in column B, see screen shots (TID), then the first formula would be:
=C2&"_"&D2 and will result in Andy_F being created. Copy this down for all the names you have.
You should have something like this:
NEW TID Person Type Name
Andy_F 1 Andy F Orange
Andy_M 2 Andy M Beef
Andy_V 3 Andy V Carrot
Andy_V 4 Andy V Spinach
Bobby_M 5 Bobby M Ham
Bobby_F 6 Bobby F Apple
Bobby_V 7 Bobby V Carrot
Next, set up a table like this (using copy unique items, if necessary), with unique names on the vertical and Types along the horizontal:
F M V
Andy [form]
Bobby
Where [form] is a vlookup formula as in the screen shots below:
Resulting in the correct table for you, once the formula is copied to all cells in the new table:
Vlookup will grab the first item that matches its search critera, so multiple matches will be ignored.
The formula for Andy F in the table is VLOOKUP($G2&"_"&H$1,$A$2:$E$8,5,0), with the data as in the screen shots.
A better way might be to use VBA, but this should do the trick.

Resources