How to shorten this syntax in Google Sheet? - google-sheets

Can this (Google Sheet) =IFS syntax be improved?
=IFS(and(E42>E38;E42>E34;E42>E30;E42>E26;E42>E22;E42>E18); "Cattleman";
and(E38>E42;E38>E34;E38>E30;E38>E26;E38>E22;E38>E18); "Naturalist";
and(E34>E42;E34>E38;E34>E30;E34>E26;E34>E22;E34>E18); "Farmer";
and(E30>E42;E30>E38;E30>E34;E30>E26;E30>E22;E30>E18); "Carpenter";
and(E26>E42;E26>E38;E26>E30;E26>E34;E26>E22;E26>E18); "Blacksmith";
and(E22>E42;E22>E38;E22>E30;E22>E34;E22>E26;E22>E18); "Miner";
and(E18>E42;E18>E38;E18>E30;E18>E34;E18>E22;E18>E26); "Builder")
And how can I add a default value so that if this syntax returns FALSE it doesn't say #N/A! in the cell, but "No class" or something similar instead (or empty)?

One obvious way would be to replace the ANDs with a MAX. Why? In the first line, if E42 is greater than all of the other cells, it must be greater than the MAX of them. So the condition in this line
E42 > MAX(E38; E34; E30; E26; E22; E18)
which looks much cleaner. Repeat for the other lines.
Trying to simplify it more, the logic of the formula seems to be that depending on which of the cells is the greatest, you choose a particular literal value. There is a function for that! I'd try this (can't test it though without access to your data)
=CHOOSE(
MATCH(
MAX(E42; E38; E34; E30; E26; E22; E18);
{E42; E38; E34; E30; E26; E22; E18});
"Cattleman"; "Naturalist"; "Farmer"; "Carpenter"; "Blacksmith"; "Miner"; "Builder")

wrap it in IFERROR like this:
=IFERROR(IFS(
AND(E42>E38;E42>E34;E42>E30;E42>E26;E42>E22;E42>E18);"Cattleman";
AND(E38>E42;E38>E34;E38>E30;E38>E26;E38>E22;E38>E18);"Naturalist";
AND(E34>E42;E34>E38;E34>E30;E34>E26;E34>E22;E34>E18);"Farmer";
AND(E30>E42;E30>E38;E30>E34;E30>E26;E30>E22;E30>E18);"Carpenter";
AND(E26>E42;E26>E38;E26>E30;E26>E34;E26>E22;E26>E18);"Blacksmith";
AND(E22>E42;E22>E38;E22>E30;E22>E34;E22>E26;E22>E18);"Miner";
AND(E18>E42;E18>E38;E18>E30;E18>E34;E18>E22;E18>E26);"Builder");
"No class")

Related

Directly return a table entry from a (simplest) function in Lua

I wanted to write the simplest possible function which let me return the desired value in a nameless table and, ideally, it should be something like this:
function RL_MyTool:Version(n)
return {"0.4.0", "20221003-0230", "13.5.5"}[n]
end
But, of course, that's not allowed in Lua...
So, off the top of my head, I can think on these two other possibilities:
1:
function RL_MyTool:Version(n)
local t = {"20221003-0230", "13.5.5"}
return t[n] or "0.4.0"
end
2:
function RL_MyTool:Version(n)
local n, t = n or 1, {"0.4.0", "20221003-0230", "13.5.5"}
return t[n]
end
Both of them slightly different from each other but doing the same, counting with the advantage of returning a default value if no argument is given, which is good. BUT... Do you think I could still have a possibility of writing it like in the very simplest fashion way above? Basically, what I'd like is not even have to use a single variable or table declaration along the function but still let me return the specified table entry when called.
Well, that's all. Of course if it's finally not possible (as I'm afraid) it won't be the end of the world 🙄, but I wanted to be sure I wasn't missing any Lua trick or something that let me do it more like I firstly imagined... Thanks!
P.S. Oh, I don't see how, but of course if it could be achieved without the necessity of even using a table at all, that would be equally valid or even better.
EDIT: BTW, for the record and based in #Piglet (great!) answer, I got to reduce it even more this way:
function RL_MyTool:Version(n)
return ({"0.4.0", "20221003-0230", "13.5.5"})[n or 1]
end
Improving code usability/maintenance a bit at the same time by avoiding duplicated values... Kind of a win-win-win 😁
Just put the table in parenthesis.
function RL_MyTool:Version(n)
return ({"0.4.0", "20221003-0230", "13.5.5"})[n] or "0.4.0"
end
But what is the purpose of this? Code should be easy to read and easy to work on. There is absolutely no reason to not use a local table. You don't have to pay a dollar for each line of code.

Exact match in dget function with an array as the criteria

Example Sheet I'm trying to get an exact match with an array in the criteria section of dget. Maybe there is another way to work around this, but I'm trying to give it a dynamic component in the array.
=dget('Micro Data'!$A$1:J,"PCR Score",{"Micro Type","Stage Type","Tank","ID#";"PCR PAL","Bright",F2,H2})
Sometimes all criteria matches multiple data points except the "Tank". However the tanks won't exactly match. Ex. All the data is the same in two data sets, except the tanks are CT1 and CT18. This then comes up with the #NUM! error. I'm trying to find if there is a way to get an exact match in the array data while still allowing it to reference the cell?
I know there is the option of making it "=XXX" making it a txt string, but this would take away the dynamic function. I would also loose the auto updating aspect when more data is added.
Thanks
Ryan, see my solution using a query, in Retain Log-GK, cell F2. I think it is just as dynamic as the dget, but perhaps not. It will need some error wrapping to avoid errors if no result found.
Formula is basically:
=query('Criteria Source'!A2:J5,
"select J where B = '"&D9&"' and C = '"&D10&"' and E = '"&D11&"' and D ='"& D2 & "' ",0)
I made all of the criteria dynamic, though obviously you can do it whatever way suits you best...
Let me know of any questions. I'll check back later...

Replacing empty cells in a variable with values from another

I have a dataset with a number of columns. Two of them are practically the same however in variable column 1 there are string data that I would like to extract and replace in empty cells of variable column 2.
I tried using the syntax
If
variable_2 = "".
Compute variable_1 = variable_2.
End If
But do not get anything. Please, could someone help with this?
Much appreciated.
This should be either
if var2="" var2=var1.
(no period after the condition, no "end if")
OR
do if var2="".
compute var2=var1.
end if.
(this is a "do if" and not just an "if" - enables you to add commands after the condition, and not needed here).
In any case, if variable_2 is empty you want to run variable_2=variable_1 and not the reverse.

Joining more than two WHERE statements in Query Language

So I am trying to use a simple QUERY function in Google Sheets where I want to select based on TWO parameters. Simple logic, and documentation says use the AND operator. The problem is I am searching for text via Cell Reference.
So here is my function
=QUERY(A1:D6,"select A where C='" &K1&'"" & "and D='" &K2"'")
Unfortunately it throws up an ERROR. I understand that Cell References that are text based need to be in single quotes (which themselves need to be in double quotes), but I am unable to join two WHERE statements.
What is the right syntax for this?
Very close indeed, please try:
=query(A1:D6,"select A where C='"&K1&"' and D='"&K2&"' ")
Welp! I was missing an concatenation symbol (&) at the end of the final cell reference K2.
=QUERY(A1:D6,"select A where C='" &K1&'"" & "and D='" &K2&"'")

Google Spreadsheet long IF statement?

I have this statement:
=if(
F1B!D3="1",50+FLOOR(D2/10,1),
if(F1B!D3="2",40),
if(F1B!D3="3",30),
if(F1B!D3="4",25),
if(F1B!D3="5",20),
if(F1B!D3="6",19),
if(F1B!D3="7",18),
if(F1B!D3="8",17),
if(F1B!D3="9",16),
if(F1B!D3="10",15),
if(F1B!D3="11",14),
if(F1B!D3="12",13),
if(F1B!D3="13",12),
if(F1B!D3="14",11),
if(F1B!D3="15",10),
if(F1B!D3="16",9),
if(F1B!D3="17",8),
if(F1B!D3="18",7),
if(F1B!D3="19",6),
if(F1B!D3="20",5),
if(F1B!D3="21",4),
if(F1B!D3="22",3),
if(F1B!D3="23",2),
if(F1B!D3="24",1));
But GoogleDocs return me "error: Wrong number of arguments to IF"
What I'am doing wrong?
You can't pass infinitely many arguments to IF. There's a single condition, a single "THEN", and a single "ELSE". You need to "nest" your IF statements, where each new IF() in part of the previous IF statement's ELSE. Something like this (abbreviated):
=if(
F1B!D3="1",50+FLOOR(D2/10,1),
if(F1B!D3="2",40,
if(F1B!D3="3",30,
if(F1B!D3="4",25,
if(F1B!D3="5",20,
if(F1B!D3="6",19,
if(F1B!D3="7",18)))))))
Trying to apply too many IFs, far more than necessary:
=IF(F1B!D3=1,50+FLOOR(F1B!D2/10,1),iferror(CHOOSE(F1B!D3-1,40,30,25),25-F1B!D3))
Also do not append ;.

Resources