Specify condition within preset comparison operator? - google-sheets

I am experimenting with Named Functions in Google Sheets and trying to make a function to take start and end date arguments to look between within a custom formula. I cannot figure out how to put the dates in a preset comparison and work properly. (i.e. ">=start_date") will not accept the start_date as an argument of the named function.If the start_date variable is outside the parentheses, a formula parse error occurs.
I want to be able to input a date (i.e."1/1/1969") into my function and have the ">=" to be automatic.
For a simpler experiment I want to have MYFUNC perform a SUMIF after checking if a given value is ">=":
Named Function syntax:
MYFUNC(sum_range,criteria)
Function definition:
=SUMIF(sum_range,">=criteria")
Example:
=MYFUNC(A1:A69,2)
My goal is that this example would sum the range A1:A69 if it was greater than or equal to two.
Named function syntax:
=MYFUNC(sum_range,date_range,start_date,end_date,criteria_range,criteria,placeholder)
Formula definition of my named function:
=IF(SUMIFS(sum_range,date_range,">=start_date",date_range,"<=end_date",criteria_range,criteria)=0,placeholder, SUMIFS(sum_range,date_range,">=start_date",date_range,"<=end_date",criteria_range,criteria))
I attempted populating the MYFUNC start_date and end_date as "1/1/1969" with and without quotes. This does not populate the function with a date and results in zero value while if I use the formula definition in a cell, it functions and sums the values properly.

To specify a condition within a preset comparison operator in a Google Sheets named function, you can use the IF function in the same way as you would in a regular formula.
As an example you use the following ;
function compareValues(val1, val2) {
return IF(val1>val2, "val1 is greater than val2", "val1 is not greater than val2");
}

Related

How can I find the name of the person ranked 1?

What I'm trying to do is find the name of the person who is ranked number 1 in the table shown below. I have tried =LOOKUP and =VLOOKUP but I get an error saying that a result can't be found, even though it's obviously there. I assume that I'm either using the wrong function or just not using it right.
I tried =VLOOKUP(1;D2:H19;1) and =LOOKUP(1;D2:H19;1) but neither seems to work.
Answer
The following formula should produce the behaviour you desire:
=INDEX(D2:D,MATCH(1,H2:H,0))
Explanation
=VLOOKUP can only be used to find values to the right of a search key. To find values to the left of a search key, use a combination of =INDEX and =MATCH.
The =MATCH function searches a specified range for a specified value and returns the relative position of the value in that range. In this case, the value to search for is 1, the range to search through is H2:H, and the 0 at the end specifies that the range is not sorted in any way.
The =INDEX function returns the contents of a cell within a range having a specified row and column offset. In this case, the range is D2:D, and the row is whichever row is returned by =MATCH. =INDEX could take a third argument if it was desired to specify a row offset as well, but that is not necessary here.
Functions used:
=INDEX
=MATCH
You sort your ascending order based on rank then return your desired data using INDEX() function. Try-
=INDEX(SORT(D2:H500,5,1),1,1)
=vlookup(1,{H2:H19, D2:D19},2)
Since vlookup only searches the 1st column of the input range, to use it, you need to flip the columns by composing a local array: {H2:H19, D2:D19}.
{} means concatenation. , in it means horizontal concatenation. With that, the rank column is now the 1st column in the input of vlookup and now vlookup works.
With our local array, the 2nd column are the names and therefore index should be 2.
Also note the use of comma to separate function inputs.
your VLOOKUP formula should look like:
=VLOOKUP(1, {H2:H19, D2:D19}, 2, 0)
also try just:
=FILTER(D:D; H:H=1)
or:
=SORTN(D:D; 1; 1; H:H; 1)
You can use query (usefull in case of ex aequo)
=query(D2:H,"select D where H=1",0)

Last value of a column in Google Sheets

I was trying to use the following function;
=INDEX(D:D,COUNTA(D:D),1),
in order to get the last currency value of a column, but it returns #ERROR!.
The value im trying to extract
As I montly update this spreadsheet, it would make it very convenient if would etract the last value in the column, e.g. the value marked in the image.
Is there a way (in Google Sheets) to find the last non-empty cell in this column, such that when I update the spreadsheet with a new "last value" it would return that value?
The index(counta()) pattern will fail when the data is sparse, i.e., when there are blank values in the column.
The index(match()) pattern will fail when the data contains a value that is not a number.
To find the last non-blank value in column D, regardless of data type, use the +sort(row()) pattern:
=+sort(D1:D; not(isblank(D1:D)) * row(D1:D); false)
The formula uses semicolons as argument separators to make it work in any locale.
If the column has only currency (ie number) values then you can use something like:
=INDEX(D1:D, MATCH(999^99, D1:D))
or try:
=SORTN(D:D; 1;;ROW(D:D)*(D:D<>""); )

Google Sheet: Arrayforumla as sum_range argument in sumif throws error

Screenshot with example
I want to define the sum_range in an sumif-formula by using a range returned by an arrayformula but it gives me an error, not acknowledging it as a range. Using an arrayformula for the conditional range works however.
This works:
=sumif(
ARRAYFORMULA({B1;C3;D5}),TRUE,
A7:A9
)
This doesn't:
=sumif(
ARRAYFORMULA({B1;C3;D5}),TRUE,
ARRAYFORMULA({A1;B3;C5})
)
Am I doing something wrong here that I can't see or is it maybe just not possible? Maybe there's another smart way to do it..?
Why not use
=sumif(B1:B3, "TRUE", A1:A3)
or even
=sumproduct(B1:B3, A1:A3)
instead ?
If you really need to use inline arrays this should work
=sumproduct({B1, B2, B3}, {A1, A2, A3})
Bool to Number Casting
SUMPRODUCT will automatically cast Booleans to Numbers, so TRUE=1 and FALSE=0. If it doesn't, then the formula requires this to be a manual operation. You can leverage the N() formula to cast Booleans to Numbers manually.
Reference
N()
SUMPRODUCT()
you don't need any arrayformula to use the sumif as the sumif accepts array ranges:
=sumif(B:B; true; A:A)
ranges:*** ***
the thing is that you may not have a multidimensional array in your sum range column, only a single array, however, in your initial range you can have any multidimensional array:
=sumif(B1:B3;true;{A1:A3}) - error
=sumif({B1:B3};true;A1:A3) - works well
=sumif({B1:B3; B1:B3}; true; A1:A3) - works well as well

Google Sheets SUMIFS different criteria with checkbox

i've the following formula the problem i've is, this isn't working as it should be.
=SUMIFS(E9:14,$I$16:$I,FALSE(),$H$16:$H,G8)
E9:E14 is the part which should be summed up when the checkbox in I16:I = FALSE and if the name matches in H16:H from G8. My problem is I am getting this error
The array arguments of "SUMIFS" vary in size
My question would be, how do I get this exact formula to work? Exactly these areas have to be covered and cannot be changed, otherwise everything else is broken.
EDIT: Added example spreadsheet
https://docs.google.com/spreadsheets/d/1DdTSZAfGTpoeun3k2qqkDMG1jnaUaSz6wgSf2heIIDI/edit?usp=sharing
You need to adjust your ranges. Here's how =SUMIFS() works and then you'll see why you need to adjust the function.
=SUMIFS() looks for ranges and then applies the logic. So when you are telling the function to summarise E9:E14 it interprets it as:
SUM(E9,E10,E11,E12,E13,E14)
provided the following conditions. The conditions will tell the function whether to include each of the components (i.e. E9,...,E14).
Whether a condition is met or not is decided using a simple boolean (true/false) array. This could for example be I9:I14=FALSE which is interpreted as the array
{IF(I9=FALSE),IF(I10=FALSE),...,IF(I14=FALSE)}
resulting in an array similar to this:
{TRUE,TRUE,FALSE,FALSE,FALSE,TRUE}
(assuming the conditions I9, I10 and I14 are met but not the other three. The same is done for the second condition (the values in column H being equal to the value in G8, resulting in another array similar to this:
{TRUE,FALSE,FALSE,TRUE,FALSE,TRUE}
(assuming that only the values in H9, H12 and H14 are equal to G8.
When the function compares the two condition arrays and returns an aggregate array similar to this:
{TRUE,FALSE,FALSE,FALSE,FALSE,TRUE}
because only for the first and the last value the conditions are met. Therefore the =SUM function becomes like this:
SUM(E9,FALSE,FALSE,FALSE,FALSE,E14)
where FALSE = 0 so it returns
=SUM(E9,E14)
Here's where you get into trouble
You try to pass the function conditional arrays that are of a different size to the sum array (E9:E14), in effect asking it to compare apples and the age of your neighbour. What you need to do is to create the calculation you have in column E in a new column in rows 24 down and use that as the sum range in =SUMIFS().

Google spreadsheet countif non empty cells except certain values

So basically i want to check if certain range of field is not blank or has a certain value
so
=ArrayFormula(COUNTIF(NOT(ISBLANK(H24:I28)), true))
will count if they are blank
How can i edit it to also search for a certain value so far i've tried :
=ArrayFormula(COUNTIF(NOT(ISBLANK(H24:I28)) OR(IFERROR(SEARCH("someValue",H24:I28,1)>0,FALSE)), true))
Here is a link to a sheet as an example:
https://docs.google.com/spreadsheets/d/1sc8xmLf8_EYFoQb3kNQRdxdd-9PemrJ4lDhTfIqCJNg/edit?usp=sharing
Update following OP's details:
Well i want to count every value except anything that starts with the word Empty basically
=COUNTIF(H24:I29,"<>") -
COUNTIF(ArrayFormula(REGEXMATCH(H24:I29,"Empty")),TRUE)
Functions used:
REGEXMATCH
ArrayFormula
COUNTIF
Initial answer
To find whether theValue exists or not, you can use the following formula:
=IF(COUNTIF(H24:I28,"<>")>0,IF(COUNTIF(H24:I28,"someValue")>0,"someValue","no value"),"empty")
To count how many times theValue exists, please use:
=IF(COUNTIF(H24:I28,"<>")>0,IF(COUNTIF(H24:I28,"someValue")>0,COUNTIF(H24:I28,"someValue"),"no value"),"empty")
(Of course you can adjust the "messages" to your liking)
Functions used:
COUNTIF
IF
You could use the OR() operator:
Example:
=ArrayFormula(OR(COUNTIF(NOT(ISBLANK(H24:I28)),true),IFERROR(SEARCH("someValue",H24:I28,1)>0,FALSE)))
References:
- OR() function

Resources