Rank Arrayformula does not work with COUNTIF - sorting

I'm using this formula in Google Sheets to RANK rows based on TOT column. It works well if I put the formula in E2 and drag over the column, but I want to put this formula inside an arrayformula and it's not working. How could I arrayformulize this?
=RANK.EQ(I3;$I$3:$I;0)+COUNTIF($I$3:I3;I3)-1
=ARRAYFORMULA(RANK.EQ(I3;$I$3:$I;0)+COUNTIF($I$3:I3;I3)-1) This doesn't work
https://docs.google.com/spreadsheets/d/1D0ralOL6nv5Cv_L5La4x8q5XrlX56oVFJHJqgz-aj94/edit
Note: the CountIf part is important because I don't want duplicated ranking numbers if there's a draw between two rows. Without COUNTIF the arrayformula works well but I don't want to remove it.

use:
=INDEX(SORT({SORT(ROW(D2:D); D2:D; )\ROW(D2:D)-1});;2)
update
=SORT(ROW(D2:D)-1; SORT(ROW(D2:D); D2:D;;C2:C;); 1)
or:
=SORT(ROW(D2:D)-1; SORT(ROW(D2:D); D2:D;;C2:C; 1); 1)

You can try this approach. It sorts the sequence of numbers (1 to 14 in this case) according to the ascending order of D2:D; and then finds out with MATCH in which order they were left:
=ARRAYFORMULA(MATCH(SEQUENCE(COUNTA(D2:D));SORT(SEQUENCE(COUNTA(D2:D));FILTER(D2:D;D2:D<>"");0);0))

The sheet you've shared doesn't match the formula you have.
Here's a formula that works with the sheet you've shared:
=ARRAYFORMULA(RANK.EQ(D2:D;D2:D;0)+COUNTIFS(D2:D;D2:D;ROW(D2:D);"<="&ROW(D2:D))-1)
When using ArrayFormula you have to provide ranges to the functions parameters (and you don't need absolute references since you aren't going to drag the formula).
In this case it was slightly more complex as that COUNTIF you have must be done with a COUNTIFS.

Related

Power Query - conditional replace/clear entire cell in multiple columns

I'm trying to clear the entire cell if it doesn't contain a given keyword.
I've managed to do this for one column:
Table.ReplaceValue(#"PrevStep",each [#"My Column"], each if Text.PositionOf([#"My Column"],"keyword")>-1 then [#"My Column"] else null,Replacer.ReplaceValue,{"My Column"})
The problem is I need to iterate/repeat that step for a number of columns... the number of columns may vary and column names also may be different every time. I can have all those column names put into a list but I'm not able to use it.
The solution I'm looking for may look like this
for each ColNam in MyColumnsList
Table.ReplaceValue(#"PrevStep",each [#"ColNam"], each if Text.PositionOf([#"ColNam"],"keyword")>-1 then [#"ColNam"] else null,Replacer.ReplaceValue,MyColumnsList)
next
but this is not the VBA code but Power Query M - and of course the problem is with #PrevStep as I would see it like a recursions... again... do not know how to process.
Is the path I follow correct or should it be done some other way
Thanks
Andrew
Unpivot your columns to turn all the columns into two columns. Apply your replacement to the single value column then pivot it back into the original format

Google Sheets vlookup based on multiple criteria with wildcards

I'm trying to extend a gs vlookup formula with a 2nd criteria.
This formula works fine with 1 criteria in column A.
=ArrayFormula(IFNA(VLOOKUP(""&A2:A&"",sheet2!C:D,2,FALSE), ""))
But I would like to extend the formula with a 2nd (OR) criteria .
That sould somehow look like this:
=ArrayFormula(IFNA(VLOOKUP(""&A2:A&"" OR ""&B2:B&"",sheet2!C:D,2,FALSE), ""))
(The fomula shoud check if one of the 2 criterias (in column A or B) matches with column C on sheet2 and return the value from D on sheet2)
Is this even possible with a fomula?
This formula seems to do what you're looking for, placed in C2:
=ArrayFormula(
IFNA( VLOOKUP(A2:A,Sheet2!C:D,2,0),
IFNA(VLOOKUP(B2:B,Sheet2!C:D,2,0),"no match")))
I think there are several other ways of doing this, but I went with what you'd started with.
I haven't added error checking yet, for blank rows, etc., to keep it straightforward.

Automatically update formula after adding columns

My issue has been dealt with in here but I can't think thru and apply it to my formula
How to automatically update formula when inserting columns.
I would really appreciate some help here guys. Thank you!!!
This is the formula I got:
=IF($A$14=NAMES!$W2;$D15;
IF($A$14=NAMES!$W3;$G15;
IF($A$14=NAMES!$W4;$J15;
IF($A$14=NAMES!$W5;$M15;
IF($A$14=NAMES!$W6;$P15;
0)))))
After inserting three more columns it has to go to:
=IF($A$14=NAMES!$W2;$D15;
IF($A$14=NAMES!$W3;$G15;
IF($A$14=NAMES!$W4;$J15;
IF($A$14=NAMES!$W5;$M15;
IF($A$14=NAMES!$W6;$P15;
IF($A$14=NAMES!$W7;$S15;
0))))))
Upon adding 3 more columns to the right, it looks up for the value in $A$14 and if that matches the one in NAMES!$W + i where i is the row incrementing by 1, then it returns a specific column in the same row# 15, be it $D15, $G15, $J15, always jumping three columns.
The 3 columns are automatically inserted via a trigger based on date/time in GAS but I was not able to make the formula automatically update via GAS.
I'm not even sure if this is possible.
Please help!
Thank you!
I write here again because the comment is too short:
You're right!
Here's the link to the Sheet with editor access.
https://docs.google.com/spreadsheets/d/1Qkb96SgZ4dLRBebgxEiNkYiZ4sTtps-ZbBtBtE4J5os/edit?usp=sharing
So basically, whenever a new year is created or 3 more columns are added the formulas in C15, C16 and C17 be updated automatically from
C15 =IF($A$14=Dates!$A2;$D15;IF($A$14=Dates!$A3;$G15;0))
to =IF($A$14=Dates!$A2;$D15;IF($A$14=Dates!$A3;$G15;IF($A$14=Dates!$A3;$J15;0;0)))
C16 =IF($A14=Dates!$A2;$D16+$E15+$B13;IF($A14=Dates!$A3;$G16+$H15;0))
to =IF($A14=Dates!$A2;$D16+$E15+$B13;IF($A14=Dates!$A3;$G16+$H15;IF($A14=Dates!$A3;$J16+$K15;0)))
C17=IF($A14=Dates!$A2;$D17;IF($A14=Dates!$A3;$D17+$G17;0))
to =IF($A14=Dates!$A2;$D17;IF($A14=Dates!$A3;$D17+$G17;IF($A14=Dates!$A3;$D17+$G17+J17;0)))
and so on...
Daniel, I've had a try, and have found a way to get part of your answer. I believe the rest of your problem could be solved in the same way. Look at tab Test-GK in your sample sheet. Your original formula is in C15; mine is in C19.
Both update as you change the value in B14.
I've used a formula to calculate which cell is needed to be used, instead of lots of IF statements. The formula is as follows.
=INDIRECT(ADDRESS(15;(MATCH(A14;Dates!$A$2:$A$15;0))*3+1;4))
This uses MATCH to obtain an 'offset' value for your date range from in your Date values.
Then it multiplies this by 3 to jump to the right column.
So for the first date range in your list, "a 31 de diciembre 2018", MATCH returns "1" (first date range in the list) times 3 plus 1 equals "4", which ADDRESS converts to column "D". The row is always 15 using your sheet.
So ADDRESS(15,1,4) returns D15.
And INDIRECT gets the value of D15.
I think the same principle could be used for the formula in C16. But I'm not as clear what that formula is doing. Does your script put a value into B13?
Let me know if this looks useful.
Update#1. I've got the formula (I think) to replace your formula in C16 as well now. Except for the third term, adding B$13, in only the first case? If necessary, this could be handled with one IF statement, to check if the data range is "a 31 de diciembre 2018".
Let me know how the B13 value is used...
Update #2. Also added a formula to replace your IFs in C17. Effectively the same, but used a CHOOSE function to select which terms get added together, instead of an IF. Maybe a bit clearer to maintain if you add more years to the date range.
Try this formula in C17 - double check the logic.
=CHOOSE(MATCH(A14;Dates!$A$2:$A$15;0); D17; D17+G17; D17+G17+J17; D17+G17+J17+M17; D17+G17+J17+M17+P17; D17+G17+J17+M17+P17+S17; D17+G17+J17+M17+P17+S17+V17; D17+G17+J17+M17+P17+S17+V17+Y17)
It's good to 2025, but if you need another five years or so, you can just make it longer. There is perhaps a way to build it dynamically, but I didn't have the time now for that.

ArrayFormula column disappears when sorting in a filter view in Google Sheets

I'd like to use ArrayFormula to populate a column in spreadsheet, but when I Sort A->Z in a filter view, the ArrayFormula column vanishes. In some cases, the column includes a #REF! error about the range, and in some cases the column is just blank after the Sort. The following is a simplified version of what I'm trying to do (in my actual application, I'm doing a Vlookup to another sheet):
https://docs.google.com/spreadsheets/d/1XbqqedOjuSKuE-ZLIHNw59-r01EsNMpx7YVqOoxSOR4/edit?usp=sharing
The column 3 header uses an ArrayFormula to copy from column 1. If you go to the Filter 1 filter view, you'll note that column 3 is blank except for an error. This happens after I try to Sort Z->A on column 2. In my more complicated use-case, involving a Vlookup, after a Sort the column disappears entirely (leaving no #REF! error). Before sorting in both cases, everything is fine.
How do I make ArrayFormula values persist in filter views after sorting?
Thanks for your help!
I'm guessing that, because your references are normal (relative, not anchored/absolute), the range A2:A10 after sorting down turns into something absurd, like A7:A4, depending on actual sorted values.
Also, if you hover with your mouse on the #REF error, what does it tell you?
Anyway, try using absolute references in your formula:
=arrayformula({"Column 3"; A$2:A$10})
Edit
Fascinating. It's the first time I see this type of error. Taking it at face value, it seems that it's a limitation of Google Spreadsheets - you cannot use ARRAYFORMULAS spanning multiple rows inside sorted filter views, because, like I sort of guessed, it messes up the ARRAYFORMULA's range (as indicated by the fact that the formula is now in C4 instead of C1).
But that gives you also the solution: do not include the cell with the arrayformula in the filter view. Instead of making your filter view's target range A1:C20, make it A1:B20. Then the arrayformula in C1 will be untouched by the filter and will indeed continue to work.
I have found a solution for my usecase, in your case, it could be:
=arrayformula(if(row(C:C)=1;"Column 3";A:A))
But you'll need to consider the whole columns in your formulas.
Example
Have you tried A2:A?
If you don't put an ending row, means the end of the column.
It worked for me.
Cheers

How to filter entries that are not duplicates of entries from others columns in Google Sheets?

I have a column called "Masterlist" which contains values from Lists 1, 2 and 3. It also contains values which are present only in Masterlist.
How can I filter them, like shown at the attached image in Google Sheets?
EDIT: The lists will have more than one entries.
Solution 1
In E2, type in
=filter(A2:A,arrayformula(iserror(match(A2:A,B2:D2,0))))
Check the documentation of filter or match for how to use them. With match, be sure to include the third argument. That is an easy one to forget. arrayformula iterates a formula over a range. The output can be a range, in which case it will print over any un-written cells. When arrayformula interacts with match, it only iterates over the first argument, which is why this solution works.
EDIT: If you have a two-dimensional range to match to, you need to collapse them into a one-dimensional range using the concatenation operators such as
=filter(A2:A,arrayformula(iserror(match(A2:A,{B2:B4;C2:C4;D2:C4},0))))
You can experiment with endings without row indices and let Google Sheets select an ending index for you.
Solution 2
Use the native Filter View feature. Good for the scenarios where you don't need to separately print a list of the unique values in "masterlist".
Go to Data -> Create Filter View
Use the relevant help pages to navigate yourself. I can see a few ways to implement what you desire, including
filter by value on the same column (selecting the actual values manually);
filter by value on a "helper column" where you include a formula in the cells to check whether the content in "masterlist" belongs to the list you want to check against. You can use the match and iserror combo here;
custom formula using a similar formula as above.
If your column A, ie. the "masterlist", is something a user would add to, then Data Validation can be used to good effect in conjunction with Filter View.

Resources