Dynamic value lookup if input is between two set values - excel-formula

A table contains a list of number and a list of starting values for when the numbers are valid. A separate table has a list of inputs for which the output must be calculated.
Calculation rules:
If the input is greater than or equal to the lower of two sequential start values and less than the higher of the same two start values, the number corresponding with the lower start value is valid.
If the input is lower than the first start value, the output is 0.
If the input is greater than or equal to the highest start value, the output is the number corresponding to the highest start value.
The values in the Number and Start fields are dynamic. In other words, the values can't be hard coded.
Start values will always be in ascending order.
For example:
The following formula works, but I'm trying to increase my Excel skills by considering options which would shorten the overall number of characters. I suppose it's a little bit of a Code Golf situation.
=IF(D2<B$2, 0,
IF(D2<=B$3, A$2,
IF(D2<=B$4, A$3,
IF(D2<=B$5, A$4,
IF(D2<=B$6, A$5, A$6)
)
)
)
)
My current solution doesn't scale well because of the nature of embedded if statements. The goal is to reduce the number of characters necessary to compute the right output values with no concern for readability. It's okay to create reference cells if necessary, but I've been trying to avoid that.
This a dive into nitty-gritty algorithms more than anything else. Any tips would be greatly appreciated.

I found a more succinct and readable answer after considerable trial and error. Unfortunately, I had to add another field (Stop), but the resulting formula is much more readable and idiomatic. See below.
This is the formula in F2: =SUM(J4>=C$11:C$15*(J4<E$11:E$15)*F$11:F$15)

Related

Excel IF statement Not returning the appropriate Value

I'm trying to grade students by giving them A or B depending on their score. If someone is having absent instead of a score, I return a value of the cell.
However, it does not return the value of the cell. The reference records are in a separate sheet called raw. I think it may be because I'm trying to return a string data.
I am using Excel 2007. Here's the formula:
=IF(raw!E6>=75,"AA",IF(raw!E6>=70,"AB",IF(raw!E6>=60,"B",IF(raw!E6>=50,"C",IF(raw!E6>=40,"D",IF(raw!E6<40,"RT",raw!E6))))))
Don't use Nested IFs if you can avoid it. Instead, use a banded VLOOKUP: it's many times more efficient, and a heck of a lot simpler to troubleshoot. Something like this:
=IF(ISNUMBER([#Score]),VLOOKUP([#Score],Table1,2,TRUE),"Absent")
Notes:
The above uses Tables and the associated Table Notation. I always use
Tables when I can, because they reduce spreadsheet administration and
the Structured Table References have intrinsic meaning.
The VLOOKUP must have TRUE as the forth argument, and the lookup
table must be sorted in ascending order.
The lowest score must be zero, so that anything below 40 gets a "Retake" grade.
Depending on whether or not the students' scores are in whole percentage figures (i.e. 75, 63, etc), you could use the INT-function to force interpretation of the input field as an integer (not that it always round down a score, which I suppose seem to be ok with the nested if-structure you're using here. Your function would then be:
=IF(INT(raw!E6>=75),"AA",IF(INT(raw!E6)>=70,"AB",IF(INT(raw!E6)>=60,"B",IF(INT(raw!E6)>=50,"C",IF(INT(raw!E6)>=40,"D",IF(INT(raw!E6)<40,"RT",raw!E6))))))
you just need to change you last IF condition (raw!E6<40,"RT) because excel will give RT to all the score which is below 40 so add a and condition like if raw!E6>0 , this should resolve your work
if(and(raw!E6<40mraw!E6>0),"RT,raw!E6)
Hope this helps

Using ROUND after Nested IF Statements in Excel

I am working on a spreadsheet for a client that calculates values and shows them as fractions after a good amount of deductions. We were running some test after I had it all set and ready and they ran a certain number through the spread sheet. I'll try to beak it down as easily as possible (any value with brackets is user input. So Width is [94.5(94 1/2)] that number get subtracted by 7.5312(7 17/32 that equals 86.9688(86 31/32) that number is then divided by [3] which then equals 28.9896(28 95/96) This is where the problem is though. 95/96 is not a "real" fraction is there a way I can round numbers like this down to 64ths? They were expecting 63/64ths Even though the math is correct they need it to round down in those cases.
The If statement is this:
=IF(E4=1,(K4-F19)/1,IF(E4=2,(K4-G19)/2,IF(E4=3,(K4-H19)/3,IF(E4=4,(K4-I19)/4,IF(E4=5,(K4-J19)/5,IF(E4=6,(K4-K19)/6,IF(E4=7,(K4-L19)/7,IF(E4=8,(K4-M19)/8,IF(E4=9,(K4-N19)/9,IF(E4=10,(K4-O19)/10,0))))))))))
This is a single part of the IF statement:
=IF(E4=1,(K4-F19)/1
Is there a way around this or are they SOL(Sorta Outta Luck)?
Thanks for any insight.
Assuming in A1, please try:
=ROUNDDOWN(A1*64,0)/64
with suitable formatting.
This ensures that all results are multiples of 1/64.

Match 2 columns based on a % difference within the value

I am looking for a method to match two excel tables.
I basically have two Systems, where the values do not exactly match only some IDs. The values in system 2 are usually 10-20% different from system 1.
Here is how the sheet looks like:
I tried to use vlookup on the IDs and then going hand-by-hand through the values if they match, by using the filter with the ID. However, this takes extremely long and is very cumbersome.
Any recommendation how to match these two tables, much more easily?
I really appreciate your replies!
If you look at a formula for G3 you would be involving D3:E3 and A:B (where A10:B10 are the matching values).
When someone states that they are looking for a percentage, it is helpful to know "a percentage of what...?". You receive a different result if the calculation is ABS(12 - 15)/15 instead of ABS(12 - 15)/12. One may within tolerance and the other may not.
In any event, the formula for G3 would be something like,
=ABS(E3-VLOOKUP(D3,A:B, 2, FALSE))/E3
... or,
=ABS(E3-VLOOKUP(D3,A:B, 2, FALSE))/VLOOKUP(D3,A:B, 2, FALSE)
That produces a result of 0.25% or 0.20% depending on how you calculate the percentage. You could wrap that in an IF statement for a YES/NO text result or use a custom number format like [Color3][>0.2]\NO;;[Color10]\Y\E\S;# which will show a red NO for values greater than 20% and a green YES for values between 0 and 20%. Negative values do not have to be accounted for as the ABS removes them from consideration.
       
I've only reproduced a minimum of your sample data for demonstration purposes but perhaps you can get an idea on how to proceed from that.

Excel array function for checking monthly values

I have an array equation to tell me the number of unique values in a column (D) based on whether the date field in another column (B) is in a particular month.
My equation is:
=SUM(IF(MONTH($B$2:$B$63)=10,(IF(FREQUENCY(IF(LEN(D2:D63)>0,MATCH(D2:D63,D2:D63,0),""), IF(LEN(D2:D63)>0,MATCH(D2:D63,D2:D63,0),""))>0,1))),0)
This works great for October and when I change the 10 value to be another number it works for all months except january. So you can see if I have done a copying error here is the cell relating to January:
=SUM(IF(MONTH($B$2:$B$63)=1,(IF(FREQUENCY(IF(LEN(D2:D63)>0,MATCH(D2:D63,D2:D63,0),""), IF(LEN(D2:D63)>0,MATCH(D2:D63,D2:D63,0),""))>0,1))),0)
This always returns "N/A"
Any ideas why?
There are a few things wrong with your construction.
Firstly, the array you are using for the bins_array parameter, which is derived from your MATCH construction combined with an IF statement, is forcing FREQUENCY to return an array containing less than 62 elements.
When this array is then compared with the initial IF clause, i.e. IF(MONTH($B$2:$B$63)=1, which does contain 62 elements, you have an issue, and, where possible, the way in which Excel resolves a comparison between two arrays of differing sizes is to artificially increase the smaller of the two so that it is of a dimension equal to that of the larger.
Of course, in doing this, it fills in the missing values with #N/As (what else could it do?). Hence your result.
In any case, repetition of the MATCH construction is not necessary for the bins_array parameter, and forces unnecessary extra calculation. As such, I am always surprised to see how many sources still recommend this set-up.
Finally, any IF clauses should appear within the FREQUENCY construction, not without.
Overall:
=SUM(IF(FREQUENCY(IF(LEN(D2:D63)>0,IF(MONTH($B$2:$B$63)=1,MATCH(D2:D63,D2:D63,0))),ROW(D2:D63)-MIN(ROW(D2:D63))+1),1))
is what you should be using.
Regards

nested excel functions with conditional logic

Just getting started in Excel and I was working with a database extract where I need to count values only if items in another column are unique.
So- below is my starting point:
=SUMPRODUCT(COUNTIF(C3:C94735,{"Sharable Content Object Reference Model 1.2","Authored SCORM/AICC content","Authored External Web Content"}))
what i'd like to figure out is the syntax to do something like this-
=sumproduct (Countif range1 criteria..., where range2 criteria="is unique value")
Am I getting this right? The syntax is a bit confusing, and I'm not sure I've chosen the right functions for the task.
I just had to solve this same problem a week ago.
This method works even when you can't always sort on the grouping column (J in your case). If you can keep the data sorted, #MikeD 's solution will scale better.
Firstly, do you know the FREQUENCY trick for counting unique numbers? FREQUENCY is designed to create histograms. It takes two arrays, 'data' and 'bins'. It sorts 'bins', then creates an output array that's one longer than 'bins'. Then it takes each value in 'data' and determines which bin it belongs in, incrementing the output array accordingly. It returns the array. Here's the important part: If a value appears in 'bins' more than once, any 'data' value meant for that bin goes in the first occurrence. The trick is to use the same array for both 'data' and 'bins'. Think it through, and you'll see that there's one non-zero value in the output for each unique number in the input. Note that it only counts numbers.
In short, I use this:
=SUM(SIGN(FREQUENCY(<array>,<array>)))
to count unique numeric values in <array>
From this, we just need to construct arrays containing numbers where appropriate and text elsewhere.
In the example below, I'm counting unique days when the color is red and the fruit is citrus:
This is my conditional array, returning 1 or true for the rows I'm interested in:
($A$2:$A$10="red")*ISNUMBER(MATCH($B$2:$B$10,{"orange","grapefruit","lemon","lime"},0))
Note that this requires ctrl-shift-enter to be used as an array formula.
Since the value I'm grouping by for uniqueness is text (as is yours), I need to convert it to numeric. I use:
MATCH($C$2:$C$10,$C$2:$C$10,0)
Note that this also requires ctrl-shift-enter
So, this is the array of numeric values within which I'm looking for uniqueness:
IF(($A$2:$A$10="red")*ISNUMBER(MATCH($B$2:$B$10,{"orange","grapefruit","lemon","lime"},0)),MATCH($C$2:$C$10,$C$2:$C$10,0),"")
Now I plug that into my uniqueness counter:
=SUM(SIGN(FREQUENCY(<array>,<array>)))
to get:
=SUM(SIGN(FREQUENCY(
IF(($A$2:$A$10="red")*ISNUMBER(MATCH($B$2:$B$10,{"orange","grapefruit","lemon","lime"},0)),MATCH($C$2:$C$10,$C$2:$C$10,0),""),
IF(($A$2:$A$10="red")*ISNUMBER(MATCH($B$2:$B$10,{"orange","grapefruit","lemon","lime"},0)),MATCH($C$2:$C$10,$C$2:$C$10,0),"")
)))
Again, this must be entered as an array formula using ctrl-shift-enter. Replacing SUM with SUMPRODUCT will not cut it.
In your example, you'd use something like:
=SUM(SIGN(FREQUENCY(
IF(ISNUMBER(MATCH($C$3:$C$94735,{"Sharable Content Object Reference Model 1.2","Authored SCORM/AICC content","Authored External Web Content"},0)),MATCH($J$3:$J$94735,$J$3:$J$94735,0),""),
IF(ISNUMBER(MATCH($C$3:$C$94735,{"Sharable Content Object Reference Model 1.2","Authored SCORM/AICC content","Authored External Web Content"},0)),MATCH($J$3:$J$94735,$J$3:$J$94735,0),"")
)))
I'll note, though, that scaling might be a problem on data sets as large as yours. I tested it on larger data sets, and it was fairly fast on the order of 10k rows, but really slow on the order of 100k rows, such as yours. The internal arrays are plenty fast, but the FREQUENCY function slows down. I'm not sure, but I'd guess it's between O(n log n) and O(n^2) depending on how the sort is implemented.
Maybe this doesn't matter - none of this is volatile, so it'll just need to calculate once upon refreshing the data. If the column data is changing, though, this could be painful.
Asuming the source data is sorted by the key value [A], start with determining the occurence of the key column
B2: =IF(A2=A1;B1+1;1)
Next determine a group sum
C2: =SUMIF($A$2:$A$9;A2;$B$2:$B$9)
A key is unique if its group sum is exactly 1
D2: =(C2=1)
To count records which match a certain criterium AND are unique, include column D in a =IF(AND(D2, [yourcondition];1;0) and sum this column
Another option is to asume a key unique within a sorted list if it is unequal to both its predecessor and successor, so you could find the unique records like
E2: =AND(A2<>A1;A2<>A3)
G2: =IF(AND(E2;F2="this");1;0)
E and G can of course be combined into one single formula (not sure though if that helps ...)
G2(2): =IF(AND(AND(A2<>A1;A2<>A3);F2="this");1;0)
resolving unnecessarily nested AND's:
G2(3): =IF(AND(A2<>A1;A2<>A3;F2="this");1;0)
all formulas in row 2 should be copied down to the end of the list

Resources