VLOOKUP Value ... not found - google-sheets

I have a problem using VLOOKUP in spreadsheet.
In sheetA i have column A filled with a dropdown menu using datavalidation references on column A from sheetB.
Column B from sheetA contains values type of time.
The point is to sum() all values from sheetA which have the same category (column A) as in the actual row in sheetB.
My formula is in B2 of sheetB and looks as follows:
=sum(vlookup($A2;namedarea;2))
Where 'namedarea' references on sheetA!A2:B6 and looks like:
║ ║A ║B ║
╠══╬═════════╬═════════╣
║ 1║Category ║Value ║
╟──╫─────────╫─────────╢
║ 2║A ║10:00:00 ║
║ 3║ ║ 5:00:00 ║
║ 4║A ║10:00:00 ║
║ 5║ ║ ║
║ 6║B ║12:00:00 ║
╚══╩═════════╩═════════╝
And sheetB looks like:
║ ║A ║B ║
╠══╬═════════╬═════════╣
║ 1║Category ║Sum ║
╟──╫─────────╫─────────╢
║ 2║A ║20:00:00 ║
║ 3║B ║#N/A ║
║ 4║C ║#N/A ║
╚══╩═════════╩═════════╝
For some reason the vlookup for row 2 works, but same formula for row 3 results in error "Value B could not be found". Where is the bug?
Thank you guys for your help :)

VLOOKUP only returns one value. If you want to SUM the range with a condition, use SUMIF() function.
In your case, put the following into sheetB!B2 and drag down:
=SUMIF(sheetA!A2:A6,$A2,sheetA!B2:B6)

Related

Is there a way to make a model method in active record?

I was thinking to make a method that will give me the specific object from Active::Record::Associations::CollectionProxy.
For example, I have a user model that has many user_checklists and user_checklists belongs_to checklist model. So I want to make a method that will check the value in the checklist model and return the value from user_checklist model.
For example.
checklists table has the following row
╔═════════════════════╦══════════════════╗
║ id ║ name ║
╠═════════════════════╬══════════════════╣
║ 1 ║ Complete MBA ║
╠═════════════════════╬══════════════════╣
║ 2 ║ Painting class ║
╚═════════════════════╩══════════════════╝
and the users table has the following
╔═════════════════════╦══════════════════╗
║ id ║ name ║
╠═════════════════════╬══════════════════╣
║ 1 ║ name ║
╚═════════════════════╩══════════════════╝
and user_checklist has the following row
╔════════════╦════════════╦═══════════════╦═══════════════╗
║ id ║ user_id ║ checklist_id ║ completed ║
╠════════════╬════════════╬═══════════════╬═══════════════╣
║ 1 ║ 1 ║ 1 ║ true ║
╠════════════╬════════════╬═══════════════╬═══════════════╣
║ 2 ║ 1 ║ 2 ║ false ║
╚════════════╩════════════╩═══════════════╩═══════════════╝
So I was thinking to make a method called MBA in user_checklist that will give me the MBA completed value.
this is how I want to call
user.first.user_checklists.mba.completed
I don't know how to achieve this. Or I am thinking totally wrong here?
I would suggest selecting only the id of the checklist.
scope :mba, -> { where(checklist_id: Checklist.where(name: "Completed MBA").pluck(:id) }
Will query only desired column(id) from the database(with a better performance),
SELECT "checklists"."id" FROM "checklists" WHERE "checklists"."id" = $1
and you can get the same as what you did
User.first.user_checklists.mba.first&.completed
You can update your scope to avoid N+1 Query:
scope :mba, -> { joins(:checklists).where('checklists.name = "Completed MBA"')
You don't need to select the first and the id in your scope because I guess you call first method on your next request :
User.first.user_checklists.mba.first&.completed
So I have figured out a way. On make a scope on user_checklists that look for mba_id and returns only that value form user_checkilust
scope :mba, -> { where(checklist_id: Checklist.where(name: "Completed MBA").first&.id) }
After that I was able to do
User.first.user_checklists.mba.first&.completed
So that works for me for now.

One to many lookup, converted to a one to one dimension

I am struggling figuring out how to transform a one to many table structure from a transact database into a one to one dimension for a data warehouse.
In the transaction database the data structure is as:
Root Record (Parent_ID)
Lookup Table "Many Parent_IDs" (Patent_ID, Child_ID)
Reference Table (Child_ID, Name)
Joining to my reference table brings back a data set that looks like such:
╔═════════════╦═══════════╦══════════╦════════════╗
║ Root_ID ║ Root_Code ║ Child_ID ║ Child_Name ║
╠═════════════╬═══════════╬══════════╬════════════╣
║ 1 ║ 1000 ║ 22 ║ Name 1 ║
║ 1 ║ 1000 ║ 23 ║ Name 2 ║
║ 1 ║ 1000 ║ 24 ║ Name 3 ║
║ 1 ║ 1000 ║ 25 ║ Name 4 ║
║ 2 ║ 1150 ║ 67 ║ Name 5 ║
║ 2 ║ 1150 ║ 68 ║ Name 6 ║
║ 2 ║ 1150 ║ 69 ║ Name 7 ║
║ 3 ║ 2019 ║ 11 ║ Name 8 ║
║ 3 ║ 2019 ║ 12 ║ Name 9 ║
║ 5 ║ 2010 ║ 91 ║ Name 10 ║
║ 5 ║ 2010 ║ 92 ║ Name 11 ║
║ 5 ║ 2010 ║ 93 ║ Name 12 ║
║ 5 ║ 2010 ║ 94 ║ Name 13 ║
║ 5 ║ 2010 ║ 95 ║ Name 14 ║
║ 5 ║ 2010 ║ 96 ║ Name 15 ║
║ 6 ║ 5010 ║ 33 ║ Name 16 ║
╚═════════════╩═══════════╩══════════╩════════════╝
What I am after in my dimension is to know, Root_ID (1) has the values of Child_ID(22,23,24,25) and be able to reference that using the generated key on the dimension.
Keep in mind I can never predict how many child records there will be,
so creating X about of columns in my dimension isn't an option.
I am sure a wise data warehouse mind than me knows how this is normally handled.
To illustrate the desired result:
Fact Record (Dimension_Root_Key = 1)
Dimension (Key 1, Name 1, Name 2, Name 3, Name 4)
Turns out I should have studied a little more. I have learnt the nomenclature of "Bridging" tables.
Knowing that the kimball group has some great demos on how to achieve what I wanted to. For further reading on Bridging tables

How do I group similar cells and get the maximum value among those cells?

╔══════╦════════════════════╦══════════╗
║ Name ║ Date ║ Stage ║
╠══════╬════════════════════╬══════════╣
║ Dave ║ 4/29/2016 12:15:36 ║ Booking ║
║ Dave ║ 4/29/2016 17:58:24 ║ Booking ║
║ Dave ║ 4/29/2016 11:32:59 ║ Costing ║
║ Dave ║ 4/29/2016 16:52:47 ║ Costing ║
║ Dave ║ 4/27/2016 16:59:27 ║ Lead ║
║ Mark ║ 4/22/2016 12:55:22 ║ Booking ║
║ Mark ║ 4/24/2016 8:00:45 ║ Booking ║
║ Mark ║ 4/24/2016 10:07:01 ║ Booking ║
║ Mark ║ 4/24/2016 14:15:30 ║ Booking ║
║ Mark ║ 4/24/2016 18:55:22 ║ Booking ║
║ Mark ║ 4/24/2016 21:33:12 ║ Booking ║
║ Mark ║ 4/20/2016 12:55:22 ║ New ║
║ Mark ║ 4/23/2016 16:32:22 ║ Planning ║
║ Mark ║ 4/24/2016 9:11:36 ║ Won ║
║ Mark ║ 4/24/2016 12:55:22 ║ Won ║
║ Mark ║ 4/24/2016 17:04:02 ║ Won ║
║ Mark ║ 4/24/2016 19:55:22 ║ Won ║
║ Mark ║ 4/25/2016 10:19:13 ║ Won ║
╚══════╩════════════════════╩══════════╝
I have the above table on my Google Spreadsheet. What I'm trying to do is group duplicate stages and return the maximum date for each group of stage.
I know how to get the maximum date for all the cells by doing this:
=max(arrayformula((B$2:B$19=B2)*(C$2:C$19)))
But, I'd like to return the maximum date of each stage. I would like my table to look like below:
╔══════╦════════════════════╦══════════╦════════════════════╗
║ Name ║ Date ║ Stage ║ Max Date ║
╠══════╬════════════════════╬══════════╬════════════════════╣
║ Dave ║ 4/29/2016 12:15:36 ║ Booking ║ 4/29/2016 17:58:24 ║
║ Dave ║ 4/29/2016 17:58:24 ║ Booking ║ 4/29/2016 17:58:24 ║
║ Dave ║ 4/29/2016 11:32:59 ║ Costing ║ 4/29/2016 16:52:47 ║
║ Dave ║ 4/29/2016 16:52:47 ║ Costing ║ 4/29/2016 16:52:47 ║
║ Dave ║ 4/27/2016 16:59:27 ║ Lead ║ 4/27/2016 16:59:27 ║
║ Mark ║ 4/22/2016 12:55:22 ║ Booking ║ 4/24/2016 21:33:12 ║
║ Mark ║ 4/24/2016 8:00:45 ║ Booking ║ 4/24/2016 21:33:12 ║
║ Mark ║ 4/24/2016 10:07:01 ║ Booking ║ 4/24/2016 21:33:12 ║
║ Mark ║ 4/24/2016 14:15:30 ║ Booking ║ 4/24/2016 21:33:12 ║
║ Mark ║ 4/24/2016 18:55:22 ║ Booking ║ 4/24/2016 21:33:12 ║
║ Mark ║ 4/24/2016 21:33:12 ║ Booking ║ 4/24/2016 21:33:12 ║
║ Mark ║ 4/20/2016 12:55:22 ║ New ║ 4/20/2016 12:55:22 ║
║ Mark ║ 4/23/2016 16:32:22 ║ Planning ║ 4/23/2016 16:32:22 ║
║ Mark ║ 4/24/2016 9:11:36 ║ Won ║ 4/25/2016 10:19:13 ║
║ Mark ║ 4/24/2016 12:55:22 ║ Won ║ 4/25/2016 10:19:13 ║
║ Mark ║ 4/24/2016 17:04:02 ║ Won ║ 4/25/2016 10:19:13 ║
║ Mark ║ 4/24/2016 19:55:22 ║ Won ║ 4/25/2016 10:19:13 ║
║ Mark ║ 4/25/2016 10:19:13 ║ Won ║ 4/25/2016 10:19:13 ║
╚══════╩════════════════════╩══════════╩════════════════════╝
Try using query. Enter in D2 and copy the formula down:
=query($A2:$C19,"select B where A='" & A2 &"' and C='" & C2 &"' order by B desc limit 1 ")
Select B gets the dates. Where A = '" & A2 &"' is limiting to the value in cell A2 (the name). C2= is doing the same thing to the cell value in C2. It orders by date. In this case decending order. Limit 1 effectively gets the max date since it only returns one value. I hope that explain the query.

How to conditionally format based on neighbour cell text?

I'm trying to use conditional formatting on a table like this:
╔════════╦════════╗
║ Editor ║ Type ║
╠════════╬════════╣
║ john ║ Fun ║
║ doe ║ Travel ║
║ mark ║ Movies ║
╚════════╩════════╝
where I need to color both columns according to Editor rows content.
For example, if editor is john both Editor and Type need to be green, if doe red, etc., etc.
Of course in Editor column using simple "text is" conditional works flawlessly but in Type I tried indirect, address and several other formulas in the "Custom formula is" field.
What do I need to do?
Additionally, the columns repeat horizontally, like this:
╔════════╦════════╦════════╦════════╗
║ Editor ║ Type ║ Editor ║ Type ║
╠════════╬════════╬════════╬════════╣
║ john ║ Fun ║ john ║ Fun ║
║ doe ║ Travel ║ doe ║ Travel ║
║ mark ║ Movies ║ mark ║ Movies ║
╚════════╩════════╩════════╩════════╝
So I need a solution compatible with this.
Any ideas?
Select ColumnsA:D and Format - Conditional formatting..., Custom formula is and:
=$A1="john"
Choose green for Formatting style and Done.
Repeat with a new rule for each separate colour, so for red:
=$A1="doe"
If required, adjust A1 and Apply to range to suit.
BTW Strictly speaking (despite much precedent) your Q is probably off topic here (though not necessarily at Web Applications).

Unexpected values when used unsigned int with %u and %x

Today I'm working on a sample iOS application in which there is a code like:
unsigned int uCount = 0;
int iJoke = -7;
uCount = uCount + iJoke;
But when I printed it like:
╔══════════════════╦══════════════════════╦════════════╗
║ Format Specifier ║ Print Statement ║ Output ║
╠══════════════════╬══════════════════════╬════════════╣
║ %d ║ NSLog(#"%d",uCount); ║ -7 ║
║ %u ║ NSLog(#"%u",uCount); ║ 4294967289 ║
║ %x ║ NSLog(#"%x",uCount); ║ fffffff9 ║
╚══════════════════╩══════════════════════╩════════════╝
I expected the output of %u as 7.
Then I used like:
unsigned int i = 0;
int j = -7;
i = i + abs(j);
And output is like:
╔══════════════════╦══════════════════════╦════════╗
║ Format Specifier ║ Print Statement ║ Output ║
╠══════════════════╬══════════════════════╬════════╣
║ %d ║ NSLog(#"%d",uCount); ║ 7 ║
║ %u ║ NSLog(#"%u",uCount); ║ 7 ║
║ %x ║ NSLog(#"%x",uCount); ║ 7 ║
╚══════════════════╩══════════════════════╩════════╝
Although my issue is fixed with abs(), I'm curious to know why the %u gave 4294967289 as result in my first case.
Please help, Thanks in advance.
This assignment will assign in pattern representing -7 (in 2's complement) to the unsigned int. Which will be very large unsigned value.
For 32 bit int this will be 2^32 - 7 = 4294967289
And standard says it like below
"If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2^n where n is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). —end note ]
ARM stores negative numbers in Two's Complement therefore -7 is represented as 0xfffffff9 when read as an unsigned number.
The main advantage of representing negative numbers like this is that the instructions for adding signed integers is the same as adding unsigned integers.
ijoke is promoted to an unsigned integer. On most compilers the signed bit is warped around to give you a large positive number.
Conversion rules are more complicated when unsigned operands are involoved.
For example, suppose that int is 16 bits and long is 32 bits. Then -1L < 1U, because 1U, which is an int, is promoted to to a signed long.
As you seem to have a 32bit int:
4294967296 - 7 = 4294967289
In general, most of the implicit conversions are those that convert a 'narrower' operand into a 'wider' one without losing information.

Resources