Excel getting nearest value and finding corresponding column - arrays

I have the following table:
A B
1 | 262 | 22.6 | 454 || 255 | 23.2 | 442 |
2 | 327 | 18.1 | 566 || 320 | 18.5 | 554 |
3 | 452 | 13.1 | 782 || 442 | 13.4 | 765 |
Where the A corresponds with the 3 columns below.
I want to get the nearest value of the 3th column of A or B depending on another cell value. The first and second column in A remain unused
Then get the column (A, B) and the row (1, 2, 3) to use for another function
So lets say I have a value of 570. The nearest value in the 3th column would be 566. My column would be A and row 2
=INDEX(Blad2!D2:D26;MATCH(MIN(ABS(Blad2!D2:D26-D30));ABS(Blad2!D2:D26-D30);0))
This is the formula I currently use for determining the nearest value. It works perfectly but if I try to get it's values from multiple arrays, the automatic ";" sign will make excel see the next array as a new argument for the Index function.
Can both problems be solved within excel functions or do I need a macro?

I don't understand what you are trying to do but:
{="A"&MATCH(INDEX(D2:D26;MATCH(MIN(ABS(D2:D26-D30));ABS(D2:D26-D30);0));D:D;0)-1}
might suit.

Related

How to join values in one cell based on multiple conditions?

My question is very similar to How to concatenate values in multiple cells based on a condition? and I've had a lot of success applying Mourits de Beer's answer in the past; however for my current problem, I need to only join values that meet two sets of criteria instead of one.
I have Sheet_1 with ID numbers, PO numbers, and Text values that looks like this:
ID
PO
CLASS
1
223
A
1
334
B
2
334
A
3
556
B
3
445
A
1
445
A
3
334
B
I have a Sheet_2 with unique IDs where I need to join all unique PO numbers into a single cell based on whether they are Class A or B, like this:
| ID | CLASS = A and ID = A2 | CLASS = B and ID = A2 |
| ----- |---------------------- | ---------------------- |
| 1 | 223, 445 | 334 |
| 2 | 334 | N/A |
| 3 | 445 | 556, 334 |
I tried to nest an AND() function inside of the IF function so the formula only works if BOTH conditions are met, but this isn't returning all true values.
=ARRAYFORMULA(TEXTJOIN(", ",TRUE,(UNIQUE(IF(AND(Sheet_1!$ID$2:$ID=$Sheet_2!ID2,'Sheet_1!$CLASS$2:$CLASS="A"),Sheet_1!$PO$2:$PO,"NA")))))
I need the function to work in Excel/Google Sheets and do not have strong VBA skills, but any help is appreciated! (This is also my first question here, so please be kind and let me know if there is anything I can do to explain/post my questions better next time. Thank you!)

ArrayFormula in google sheets SUM if values match criteria

Ok, so I've been stuck on this for a while now trying to create an array formula that will do the following:
Let's pretend the data I'm working with appears as such:
SHEET 1 SHEET 2
| A | B | C | D | E | F | A | B |
1 | Up | Down | Down | Up | Down | 1 | Red | $50 |
2 | Red | Green| White| Black | Blue | 2 | Green | $100 |
3 | $50 | $200 | $15 | Hidden| | 3 | White | $10 |
4 | | $200 | | Hidden| $100 | 4 | Black | $70 |
5 | $50 | $200 | $15 | Hidden| | 5 | Blue | $100 |
Using that data, I need a formula in column F (that I can copy and paste for subsequent cells in the column).
My actual sheet has about 45 columns across and about 25 rows down with sensitive financial information, so I'm using pretend data above on a smaller scale.
So the formula should look something like this (this would be posted in column F of Sheet 1). It needs to check if $A$1:$E$1="Up" (disregard anything that is not "Up") in addition, IF that array = "up" then check to make sure $A3:$E3>$0 (I intend to exclude blank values and "hidden" values), then IF those 2 criteria are met, then return the corresponding value from $A$2:$E$2 and use that as a search key in a lookup formula (HLookup, Vlookup, offset+match, etc). So for example, the Formula in F3 would match those 2 criteria (which would be A2 [red] and then sum together all of the matching values - in this case, there is only 1 matching value, but usually there will be 3-5).
Attempt 1:
=ArrayFormula(SUM(OFFSET('Sheet 2'!$B$1,MATCH(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),$A$2:$E$2,""),'Sheet 2'!$A$1:$A$5,0),0)))
Attempt 2:
=ArrayFormula(SUM(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),OFFSET('Sheet 2'!$B$1,MATCH($A$2:$E$2,'Sheet 2'!$A$1:$A$5,0),0),"")))
My first attempt attempts to create an array for the Match search key that uses the IF statements to determine the results to be used in the match condition (In the example above, the only result would be "Red", but in the actual sheet, it could be an array like "Red, Blue, Yellow").
My second attempt attempts to first determine which cells we are using (based on IF conditions), then if the cell matches the IF then we will OFFSET the particular cell.
The errors I have noticed is that match doesn't seem to like my array in the search key. The first attempt results with an error saying it cannot find " in the range. (I guess I don't understand why it was searching for that...?). The 2nd attempt seems to fail because it uses the ENTIRE range ($A$2:$E$2), instead of only the cells that match the criteria.
I have tried such things as: VLookUp, HLookUp, LookUp (but it is not a sorted list). I've tried some other weird tactics, but non seem to give me what I need.
So far, the best I have come up with is:
=ArrayFormula(SUM(IF($A$1:$E$1="Up",IF($A3:$E3>"$0",0,OFFSET('Sheet 2'!$B$1,MATCH($A2:$E2,'Sheet 2'!$A$1:$A$5,0),0)),0)))
In my sheet, it found 3 cells that matched the criteria, but instead of adding Cell 1 value, Cell 2 value, and Cell 3 value, it just added Cell 1 value 3 times. So it seems it was only using $A2 (the first option in the range for the match search key) and repeated it for every cell that matched.
I have also tried to include the Filter function (instead of IF) to eliminate cells that do not have "up" as their value ($A$1:$E$1).
Use SUMPRODUCT:
=SUMPRODUCT(($A$1:$E$1="Up")*(VLOOKUP($A$2:$E$2,'Sheet 2'!$A$1:$B$5,2,FALSE))*($A3:$E3>0)*(ISNUMBER($A3:$E3)))

SUMPRODUCT doesn't recognise OFFSET array

I'm working through an Excel exercise in which I need to SUMPRODUCT two tables of related values. Unfortunately, I can't be sure the tables will always be ordered identically.
To get around this, I have looked up their values into a new third table; this works, however I'm now looking for a way to do it in one cell.
My code is currently
SUMPRODUCT(Resources_Used18[In House],OFFSET(Rate_comparison[Role],MATCH(Resources_Used18[Role],Rate_comparison[Role],0)-1,1))
For some reason however this doesn't seem to work and only ever returns zeros. The OFFSET(,MATCH()) is meant to reconstruct the second data array, this appears to work and when I test it with F9 it produces the array I'm expecting.
Additionally when I manually enter the array, it provides the correct answer.
So I think the error must be with SUMPRODUCT failing to recognise it as an array.
Where is the error and how can I correct my formula? Thanks for your help.
EDIT: Actually I need to correct myself, OFFSET produce the correct array when run in a cell of its however after I run the cell if I don't ctrl-shift-enter then produces an array of errors.
EDIT:EDIT: An example of the sort of data I'm looking is this,
Rate_comparison
| Role | Rate |
|------|------|
| 1 | 50 |
| 2 | 100 |
| 3 | 150 |
| 4 | 200 |
| 5 | 250 |
| 6 | 300 |
| 7 | 350 |
| 8 | 400 |
| 9 | 450 |
And
Resources_Used18
| Role | In house |
|------|----------|
| 9 | 23 |
| 8 | 24 |
| 4 | 25 |
| 3 | 26 |
| 1 | 27 |
| 7 | 28 |
| 6 | 29 |
| 5 | 30 |
| 2 | 31 |
I have seen this issue before, and cannot explain it. However, if you enclose the OFFSET function inside the N function, the actual values will be returned.
I did not check to see if the result (59,300) is correct however.
=SUMPRODUCT(Resources_Used18[ [ In house ] ],N(OFFSET(Rate_Comparison[ [ Role ] ],MATCH(Resources_Used18[ [ Role ] ],Rate_Comparison[ [ Role ] ],0)-1,1)))
According to Tushar Metar, The OFFSET function, when used with an array as the 2nd or 3rd argument, returns an undocumented data structure that is understood only by Excel.  The N function converts the data from the internal structure into one that can be displayed or used for further analysis.  Laurent Longre deserves the credit for discovering this capability of the N function.

Django QuerySet - Multiple Order By when there is a single field per record

I am accessing an API where the number of fields can change at any time, but I must store and display the data as a table. Therefore, each record from the API is stored as a single record per field. My problem is that I am having trouble working out how I would order by multiple columns at a time. Putting all of the data into a 2D array (list of lists) before sorting is not a viable option as the number of records could be too large to feasibly hold in memory.
I've put together a simple example to explain. If anyone has an idea on how to overcome the problem, or how I could redesign my approach, I'd be very grateful.
| record_id | field | data |
| 1 | x | 2 |
| 1 | y | 1 |
| 1 | z | 3 |
| 2 | x | 30 |
| 2 | y | 42 |
| 2 | z | 7 |
| 3 | x | 53 |
| 3 | y | 2 |
| 3 | z | 7 |
If ordering by fields 'z' then 'x' (both ascending), the record order would be 1,2,3
If ordering by fields 'z' then 'y' (both ascending), the record order would be 1,3,2
I am using models in django to store and I am using QuerySets to retrieve the data. I don't have any control over the API or database from which I am originally accessing the data.
After a fair amount of research I realised I was going about this all wrong - I am now using an hstore field in postgres and django-hstore to utilise it, for a schema-less approach. I now have a single row per original record and I can order_by after casting the required field in an 'extra' method.

Conditional SUM using multiple tables in EXCEL

I have a table that I'm trying to populate based on the values of two reference tables.
I have various different projects 'Type 1', 'Type 2' etc. that each run for 4 months and cost different amounts depending on when in their life cycle they are. These costings are shown in Ref Table 1.
Ref Table 1
Month | a | b | c | d
---------------------------------
Type 1 | 1 | 2 | 3 | 4
Type 2 | 10 | 20 | 30 | 40
Type 3 | 100 | 200 | 300 | 400
Ref Table 2 shows my schedule of projects for the next 3 months. With 2 new ones starting in Jan, one being a Type 1 and the other being a Type 2. In Feb, I'll have 4 projects, the first two entering their second month and two new ones start, but this time a Type 1 and a Type 3.
Ref table 2
Date | Jan | Feb | Mar
--------------------------
Type 1 | a | b | c
Type 1 | | a | b
Type 2 | a | b | c
Type 2 | | | a
Type 3 | | a | b
I'd like to create a table which calculates the total costs spent per project type each month. Example results are shown below in Results table.
Results
Date | Jan | Feb | Mar
-------------------------------
Type 1 | 1 | 3 | 5
Type 2 | 10 | 20 | 40
Type 3 | 0 | 100 | 200
I tried doing it with an array formula:
Res!b2 = {sum(if((Res!A2 = Ref2!A2:A6) * (Res!A2 = Ref1!A2:A4) * (Ref2!B2:D6 = Ref1!B1:D1), Ref!B2:E4))}
However it doesn't work and I believe that it's because of the third condition trying to compare a vector with another vector rather than a single value.
Does anyone have any idea how I can do this? Happy to use arrays, index, match, vector, lookups but NOT VBA.
Thanks
Assuming that months in results table headers are in the same order as Ref table 2 (as per your example) then try this formula in Res!B2
=SUM(SUMIF(Ref1!$B$1:$E$1,IF(Ref2!$A$2:$A$6=Res!$A2,Ref2!B$2:B$6),INDEX(Ref1!$B$2:$E$4,MATCH(Res!$A2,Ref1!$A$2:$A$4,0),0)))
confirm with CTRL+SHIFT+ENTER and copy down and across
That gives me the same results as you get in your results table
If the months might be in different orders then you can add something to check that too - I assumed that the types in results table row labels might be in a different order to Ref table 1, but if they are always in the same order too (as per your example) then the INDEX/MATCH part at the end can be simplified to a single range

Resources