(Excel) Giving the smallest value between 2 values - excel

Excel data table
I am new to here. If I have any mistake in making new post, Please tell me and I am ready to correct my mistake.
For above pic, I want to extract the 2 smallest values in column D respectively between row 77 and row 84, and between row 84 and 97. The resulting values are shown in P77 and P84 respectively.
How should I write the excel formula for it? Or it needs VBA to code it?
Thanks a lot for your sincere help!
(Update)
data set
above pic is another capture of my data set which filtered the day with "Bullish breaking candle/bearish breaking candle" only.
Thanks

There are lots of functions/ways to calculate Minimum in addition to the MIN function and it is worth being familiar with them as you will require different ones according to your data.
So quick rundown of some of the main offerings:
SMALL function:
I would consider also the more versatile SMALL function
=SMALL(D77:D84,1) in cell P77
=SMALL(D84:D97,1) in cell P84
You put the array (the range of cells to compare) then the k-th smallest item in that range that you want to retrieve e.g. put 1 to get the smallest, as above, comparable to MIN function, or 2 to get the second smallest etc.
Official blurb below:
Description
Returns the k-th smallest value in a data set. Use this function to
return values with a particular relative standing in a data set.
Syntax
SMALL(array, k)
The SMALL function syntax has the following arguments:
Array Required. An array or range of numerical data for which you
want to determine the k-th smallest value.
K Required. The position (from the smallest) in the array or range
of data to return.
AGGREGATE Function:
Consider the even more versatile AGGREGATE function which can cope with hidden rows in the range, errors etc. You can specify a host of additional requirements whilst still getting the minimum value
General syntax for first form:
AGGREGATE(function_num, options, ref1, [ref2], …)
Function 5 is Minimum. Options are viewable at link I gave but 7 is ignore errors and hidden rows. So, you could use:
=AGGREGATE(5,7,D77:D84)
The AGGREGATE option above is the only version that will still return the minimum correctly if there is an error in the range D77:D84 e.g. a DIV/0 error.
SUBTOTAL Function:
Similar to the AGGREGATE function is the SUBTOTAL function.
You can use SUBTOTAL(5, D77:D84) where 5 specifies you want the minimum for the range. This will not ignore errors. SUBTOTAL(105,D77:D84) will ignore hidden rows though.

Simply put the formula '=Min(D77:D84)' in cell P77
and '=Min(D84:D97)' in cell P84

Related

Counting excel cells within a time window

I am trying to get a formula that will help count cells that fall within a time window. I came up with a formula but it only works when the time is from AM to PM and not for PM (previous day) to AM (next day). As shown in the image, I want to be able to count the number of Korea trades (from Table 2) within trading window (Table 3). I was trying to use the highlighted formula but apparently it's not working on Table 1.
Formula: =COUNTIFS(D13:D16, "Korea", E13:E16, ">="&H4, E13:E16, "<="&I4)
Is there a way to achieve this?
When the times cross midnight for Korea, you'll need to do an OR condition rather than an and condition. You could do this by making two countifs functions or use Sumproduct as shown below:
=SUMPRODUCT((D13:D16="Korea")*((E13:E16>=H3)+(E13:E16<=I3)))
If you have access to a version of Excel that supports the Lambda function I’d suggest creating a lambda that does the within checking so it’s not done through the formulas on your sheet. Unfortunately I don’t think there are Ifs functions that take a lamdba as a criteria but you might be able to use Map to generate an array of booleans and get the product similar to the other reply.
So I started by creating a lambda for determining if the value is in the range. The formula I used is:
=LAMBDA(s,e,IF(s<e,LAMBDA(x,AND(x>=s,x<e)),LAMBDA(x,OR(x<e,x>s))))
That basically takes 2 parameters - your starting time and the ending time for the range. It will return a lambda that will take the time to compare. And I added that as a namedreference named “IsWithin”
Then I created another lambda that would do the overall count.
=LAMBDA(rangeMatch,textMatch,rangeTime,start,end,SUM(MAP(rangeMatch,LAMBDA(x,ISNUMBER(SEARCH(textMatch,x))))*MAP(rangeTime,IsWithin(start,end))))
The function takes 5 parameters:
the range for which you are searching for the country name
the text to match within the range
the range containing the times to evaluate
the start time for the valid range (e.g. H3)
the end time for the valid range (e.g. I3)
It uses the Map function to evaluate the items in the range using lambdas. The first part of the evaluate creates an array of the items that have the matching text so either TRUE or FALSE. The second part creates an array of items that are within the specified time range - again either TRUE or FALSE. And then it just multiplies those arrays and sums the values essentially getting those where both values are true. Then to use it you just call that named reference:
=CountMatch(D13:D16,"Korea",E13:E16,H4,I4)
You can try this
=SUMPRODUCT(--(LEN(D13:D16)<>LEN(SUBSTITUTE(D13:D16,"Korea","")))*((E13:E16>=H4)+(E13:E16<=I4)))

Get count only all parameters are within there LSL & USL

From below data table, I tried to get sample count which are each value within their specification limits. To get the sample count, all 3 values must fulfilled its specification requirement. I used SUMPRODUCT function for each and every column and checked its relevant specification limits using following formula.
=SUMPRODUCT((B3:B8>=B9)*(B3:B8<=B10),(C3:C8>=C9)*(C3:C8<=C10),(D3:D8>=D9)*(D3:D8<=D10))
But when I am dealing with more columns, this is getting more complex.
My question is, are there any other way to check all column at once? to reduce the formula complexity.
Note:- Highlighted with red color are out of specification limits. Only 2 & 6 rows are counted the returned result.
You can use SUMPRODUCT/MMULT:
=SUMPRODUCT(--(MMULT((B3:D8>=B9:D9)*(B3:D8<=B10:D10),ROW(A1:A3)^0)=3))
Just remember that the second parameter of MMULT must specify the number of rows corresponding to the number of columns of the first parameter, i.e. B3:D8 = 3 columns => A1:A3 = 3 rows. The comparison with 3 also changes accordingly.

Excel formula to sum an array of items from a lookup list

I'm trying to make my monthly transaction spreadsheet less work-intensive but I'm running up against problems outputting my category lookups as an array. Right now I have a table with all my monthly transactions and I want to create another table with monthly running totals. What I've been doing is manually summing each entry from each category, but I'd love to automate the process. Here's what I have:
=SUM(INDEX(Transactions[Out], N(IF(1,MATCH(I12,Transactions[Category],FALSE)))))
I've also tried using AGGREGATE in place of SUM but it still only returns the first value in the category. The N(IF()) was supposed to force INDEX to return all the matches as an array, but it's not working. I found that trick online, with no explanation of why it works, so I really don't know how to fix it. Any ideas?
Just in case anyone ever looks at this thread in the future, I was able to find a simpler solution to my problem once I implemented the Transactions[Category]=I12 method. SUM, itself will take an array as an argument, so all I had to do was form an array of the values I wanted to keep from Transactions[Out] range. I did this by adjusting the method Ron described above, but instead of using 1/(Transactions[Category]=I12 I used 1/IF(Transactions[Category]=I12, 1,1000) and surrounded that by a FLOOR(*resulting array*, .01) which rounded all the thousandth's down to zero and didn't yield any #DIV/0! errors.
Then! I realized that the simplest way to get the actual numbers I wanted, rather than messing with INDEX or AGGREGATE, was to multiply the range Transactions[Out] by the binary array from the IF test. Since the range is a table, I know they will always be the same size. And SUM automatically multiplies element by element and then adds for operations like this.
(The result is a "CSE" formula, which I guess isn't everyone's favorite. I'm still not 100% clear on what it means: just that it outputs data in a single cell, rather than over multiple cells. But in this context, SUM should only output a single number, so I'm not sure why I need CSE... A problem for another day!)
In your IF, the value_if_true clause needs to return an array of the desired row numbers from the array.
MATCH does not return an array of values; it only returns a single value which, with the FALSE parameter, will be the first value. That's why INDEX is only returning the first value.
One way to return an array of values:
Transactions[Category]=I12
will return an array of {TRUE,FALSE,FALSE,TRUE,...} depending on if it matches.
You can then multiply that by the Row number to get the relevant row on the worksheet.
Since you are using a table, to obtain the row number in the data body array, you have to subtract the row number of the Header row.
But now we are going to have an array which includes 0's for the non-matching entries, which is not good for us as a row number argument for the INDEX function.
So we get rid of that by using the AGGREGATE function with the ignore errors argument set after we do change the equality test to 1/(Transactions[Category]=I12) which will create DIV/0 errors for the non-matchers.
Putting it all together
=SUM(INDEX(Transactions[Out],AGGREGATE(15,6,1/(Transactions[Category]=I12)*ROW(Transactions)-ROW(Transactions[#Headers]),ROW(INDIRECT("1:"&COUNTIF(Transactions[Category],$I$12))))))
You may need to enter this with CSE depending on your version of Excel.
Also, if you have a lot of these formulas, you may want to change the k argument for AGGREGATE to use the INDEX function (non-volatile) instead of the volatile INDIRECT function.
=SUM(INDEX(Transactions[Out],AGGREGATE(15,6,1/(Transactions[Category]=I12)*ROW(Transactions)-ROW(Transactions[#Headers]),ROW(INDEX($A:$A,1,1):INDEX($A:$A,COUNTIF(Transactions[Category],$I$12),1)))))
Edit
If you have Excel/O365 with dynamic arrays and the FILTER function, you can greatly simplify the above to the normally entered:
=SUM(FILTER(Transactions[Out],Transactions[Category]=I12))

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

Using MIN function in excel when using mm:ss.00 fields and ignoring 00:00.00

Does anyone know how I can get excel to look at the following fields, all formatted in mm:ss.00 and return the lowest time. I am using this to calculate PB's - personal best times - in a sports club race sheet.
The formula I am using is
=MIN(J5,(U5),(AE5),(AO5),(AY5),(BI5),(BS5),(CC5),(CM5),(CW5),(DG5),(DQ5),(EA5),(EK5),(EU5))
The problem I have at the moment is that it is including 00:00.00 values in the cells and returning a MIN value of 00:00.00.
Any suggestions would be welcomed.
many thanks
Nigel
Use the following:
=SMALL((J5,U5,AE5,AO5,...),COUNTIF((J5,U5,AE5,AO5,...),0)+1)
COUNTIF counts the amounts of 0 (you maybe need to adjust this value based on your formatting, but it should work). SMALL returns the n-smallest number of the given matrix, with n being the counted value + 1.
Therefore if no 0 is in the matrix, you get the 1st-smallest (aka the smallest), with one 0 you get the 2nd-smallest and so on. Maybe you need to add a check if every value is 0, if that could happen, as in that case SMALL would try to retrieve the value on position list_size+1 of the list, which of course isn't present.

Resources