how to sequence a set with reseting condition only if previous state got terminated - arrays

given the specific set of columns, I'm looking for a way to turn red cells into blank cells either within the COUNTIF process or afterward.
formula I use to get (partially) correct answer is:
=ARRAYFORMULA(IF(A2:A="",,IF(NOT(REGEXMATCH(TO_TEXT(B2:B), "\-")),
COUNTIFS(A2:A&D2:D, A2:A&D2:D, ROW(A2:A), "<="&ROW(A2:A)), )))
here is a copy of my sheet: - CC -
consider yellow cells as RESET/TERMINATION for ID
after each reset, ID gets upgraded (E column)
E column is just a helper for better visualization - it's not part of the dataset
A column is unsorted and it may be even ungrouped

When I make a copy of your sheet (the only option provided), the negative numbers in Col B are actually all text. So I didn't backtrack into the best way of doing this from the original data in Col A. Instead, I just worked with Cols A:D as you currently have them (though I suspect that you don't need the helpers) and with your original "close" formula as you wrote it.
That said, this should work to deliver your desired result as shown:
=ArrayFormula({"DESIRED OUTPUT";IF(A2:A="",,IF(VLOOKUP(A2:A&"~"&COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))-1,{SPLIT(UNIQUE(A2:A)&"~0|0","|");A2:A&"~"&COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A)),C2:C},2,FALSE)<>0,,IF(NOT(REGEXMATCH(TO_TEXT(B2:B), "\-")), COUNTIFS(A2:A&D2:D, A2:A&D2:D, ROW(A2:A), "<="&ROW(A2:A)), )))})
My contribution is this part:
IF(VLOOKUP(A2:A&"~"&COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))-1,{SPLIT(UNIQUE(A2:A)&"~0|0","|");A2:A&"~"&COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A)),C2:C},2,FALSE)<>0,, [most of your previous formula])
In plain English, this looks up the last occurrence of each ID. Only if that last occurrence had a 0 value in Col C will the results of your main formula be shown.
I stacked SPLIT(UNIQUE(A2:A)&"~0|0","|") on top of the actual ID~Row, Col C values so that the first occurrence of any ID will still find a 0 and will not result in an error. (Adding IFERROR would have unnecessarily lengthened the formula.)
NOTE 1: I assumed here that you original "close" formula works as you expect. I did not test it under close scrutiny. I just plugged it into my extended formula that determines where to place results.
NOTE 2: Normally, I don't get into complex formulas on these forums, for the sheer sake of time and the fact that I stay very busy. But you help out a ton on this forum, so I was happy to invest back into your own rare inquiry here.

Related

Extract Data From Table Based on Multiple Criteria within ranges

I have a problem that I would like someone to help me with.
I need to cross data from a table, the table is the following below:
Literally I want to put in the "Yellow" line the amount I want, it can be any one from 0 to 3000. If in "Yellow" put for example 190, which is up to 200, then it will select column F. If you put 1000, then will already select column H.
Then I need to cross data with the lines, which will be up to x m2. That is, if in the line where it says "Green" select for example 0.3. Then it will select line 15. The Result of the 2 questions would be 1000 in this example.
However, I've already made a few attempts, and there I arrived at a formula killer:
=IFS(AND($E$20<=$F$14;$E$21<=$E$15);$F$15;AND($E$20<=$F$14;$E$21<=$E$16);$F$16;AND($E$20<=$F$14;$E$21<=$E$17);$F$17;AND($E$20<=$F$14;$E$21<=$E$18);$F$18;AND($E$20<=$G$14;$E$21<=$E$15);$G$15)
And this formula continues until the end. It's effective, it does its job, but in addition to being huge, it also makes it difficult to edit one day. I would like to try to improve it.
Any idea?
I apologize to everyone who was confused by my earlier attempt to explain my problem. Thank you all.
Per my understanding you are looking for two match criteria. For yellow criteria you look for exact match and for green criteria, the exact match or the next upper value.
You can use INDEX/XMATCH for that as follow using LET function in cell J3:
=LET(rng, B2:G5, upper, 1*TEXTAFTER(TEXTBEFORE(A2:A5, " m2"), " ", -1),
INDEX(rng, XMATCH(J2, upper, 1), XMATCH(J1, B1:G1))
)
or without LET function:
=INDEX(B2:G5, XMATCH(J2, 1*TEXTAFTER(TEXTBEFORE(A2:A5, " m2"), " ", -1), 1),
XMATCH(J1, B1:G1))
Note: The above approach doesn't require a helper column with the upper values, if such information is provided like in the updated version of the question (column E), then use the corresponding range instead.
Here is the output:
It assumes there is a space between m2 and the number in the green column. You need to standardize it in your input. For example the last green row doesn't have a space. If that is not the case you need to cleanup it first, via SUBSTITUTE function for example or manually it seems to be a typo.
The name upper contains the number associated to m2 in the green column using TEXTBEFORE and TEXTAFTER. The first XMATCH uses the third input argument (1) to ensure if the value doesn't exist, then it finds the next upper value. The second XMATCH look for an exact match for the yellow column.
This is a well known use case: Two dimensional lookup or two way lookup. For example you can check: INDEX XMATCH XMATCH to perform 2-dimentional lookup and just to adapt it to your specific case. You can also use XLOOKUP function for similar situations.
I'm trying to do it the way you showed me. However, I don't use excel and therefore I don't have this "LET" formula.
I had to improvise.
Followed this way:
I made a new column with the intended values.
All these values would be approximate, for example, if I put 150 in quantity and say that they are 5m2, it would give me the result of 1400. This is because it is below 200 units and below 5m2. Another example, if you put 499 units and put 13m2, then the result would be 360,
Currently, with this formula, I have already achieved approximations. However, the values are not matching up and when I pass 1500 units then it gives me this error: The value of Parameter 2 of the INDEX function, 5, is out of range.
Have a good year David.

How can I drag down to multiple rows a formula which has more than 1 row in its result?

I have an extension I am getting the data from, and I am referring to that extension in a formula with result of pre determined rows but I want more than 1 row in its result and that's when I have this problem, because I want to drag the formula to multiple rows but they overlap each other, for example if I wanted 3 rows in the result of the formula starting in row 1 and then drag it down from row 1 to row 3 the formula in row 1 and 2 will show an error because they're overlapped in each other I will put a picture in how it looks...
Is there a way to specify amount of rows as a space between each formula in a way that when I drag the formula down to more rows it will adjust to the "space" I specified?
This is the formula I am using, I am also referring to another sheet as you can see so it'll be great if you can use this formula to answer my question, if I can specify the "space" using another formula that is (also it's probably obvious but the pre determined rows in the formula is the "2d").
=CRYPTOFINANCE("KRAKEN:"&'crypto-track'!C4&"/USD", "price_history", "2d")
this is usually solved by constructing an array of formulae where you stack them up in the line like:
={CRYPTOFINANCE("KRAKEN:"&'crypto-track'!C4&"/USD", "price_history", "2d");
CRYPTOFINANCE("KRAKEN:"&'crypto-track'!C5&"/USD", "price_history", "2d");
CRYPTOFINANCE("KRAKEN:"&'crypto-track'!C6&"/USD", "price_history", "2d")}
this way the 2nd fx will pick up right after 1 fx ends
you can ease your pain of a "hand job" from constructing such an array - especially if that array needs to span over the larger range - by building a formula to generate a formula. for example: https://stackoverflow.com/a/68278101/5632629
also, make sure you obey the law of array constructs and successfully avoid all array errors - https://stackoverflow.com/a/58042211/5632629

Google Sheets: Average of every other column

I’ve looked at similar questions and I think I’m close to a working solution, but it’s giving me the wrong answer. I have a spreadsheet in Google Sheets with data in all columns, but every other cell contains a dollar value and I need only the average of those cells. They start (in this version) on cell G3 and continue through most of row 3, then I intend to copy the formula to other rows with the same cells in those rows needing to be averaged as well, so if it’ll adjust as I copy that’ll be best. Here’s what I’ve worked up so far:
=AVERAGEIF(ArrayFormula(mod(column(G3:3),2)),”>0”)
It’s returning 1 as the result, when it should be about 1500. If I change the 2 to another number, the result increases with it, so I think something in mod or column is being done wrong, but I don’t have enough practice to know where I messed up.
avg of every 2nd column it's done like:
=AVERAGE(FILTER(G3:3, MOD(COLUMN(G3:3)-1, 2)=0))
TIL about the FILTER function. Thanks guys.
There is a way with ArrayFormula. I think you almost got it. I would prefer to add one more argument for AVERAGEIF to specify the range to average. I don't know if it defaults to the range in the condition.
And I would do the modulo on the difference between a column and the first column. I guess for your question it isn't needed as all column numbers are either odd or even. But using the difference is a general purpose way to apply the concept to say every nth column.
The modulo 2 of any column number with be 0 or 1. So instead of using an inequality just use 0 or 1. From your formula it looks like your dollar values must be in odd columns so the result of the modulo should be 1. But I think if you are starting at G3 then taking the column difference before applying Mod 2 changes the desired result to 0. But of course to switch to the other column change the 0 to a 1.
=AVERAGEIF(ArrayFormula(mod((COLUMN(G3:3)-COLUMN(G3)),2)),0,G3:3)

How to arrange table data differently by using if, match, and index?

As shown in the attached image, I need to convert A2:D10 to the format of A12:E17. The 4 tables from F1 to AB12 are my experiments using if, match, and index. Same formula gets different results and it seems to be dependent on the row position of the tables. In My previous question, I was trying to pinpoint the problem to the if function.
What am I doing wrong here?
Thanks,
Lu
enter image description here
Again, as I said in your last question: The formula has not been array entered. Array formulas need to be confirmed with Ctrl-Shift-Enter.
Without that, the first array in the IF statement does not get resolved and the Match does not return the correct result.
Make use of the Evaluate Formula tool and step through the formula.
The merged cells don't help with the cell referencing. Unmerge the cells and fill in all the labels in row 1, then use this slightly amended formula and confirm it with Ctrl-Shift-Enter. Then copy across and down.
I hope that I get the English names of the functions right:
In D14 and following cells:
=INDEX($C$1:$C$5;MATCH(1;MMULT(($B$3:$B$10=$A14)*($A$3:$A$10=B$13);1);0))
The MATCH function tells which value (by number counted from the top) matches both conditions. The INDEX function returns this value from C1:C5.

Optimization of array function that calculates products

I have the following array formula that calculates the returns on a particular stock in a particular year:
=IF(AND(NOT(E2=E3),H2=H3),PRODUCT(IF($E$2:E2=E1,$O$2:O2,""))-1,"")
But since I have 500,000 row entries as soon as I hit row 50,000 I get an error from Excel stating that my machine does not have enough resources to compute the values.
How shall I optimize the function so that it actually works?
E column refers to a counter to check the years and ticker values of stocks. If year is different from the previous value the function will output 1. It will also output 1 when the name of stock has changed. So for example you may have values for year 1993 and the next value is 1993 too but the name of stock is different, so clearly the return should be calculated anew, and I use 1 as an indication for that.
Then I have another column that runs a cumulative sum of those 1s. When a new 1 in that previous column is encountered I add 1 to the running total and keep printing same number until I observe a new one. This makes possible use of the array function, if the column that contains running total values (E column) has a next value that is different from previous I use my twist on SUMIF but with PRODUCT IF. This will return the product of all the corresponding running total E column values.
The source of the inefficiency, I believe, is in the steady increase with row number of the number of cells that must be examined in order to evaluate each successive array formula. In row 50,000, for example, your formula must examine cells in all the rows above it.
I'm a big fan of array formulas, so it pains me to say this, but I wouldn't do it this way. Instead, use additional columns to compute, in each row, the pieces of your formula that are needed to return the desired result. By taking that approach, you're exploiting Excel's very efficient recalculation engine to compute only what's needed.
As for the final product, compute that from a cumulative running product in an auxiliary column, and that resets to the value now in column O when column P in the row above contains a number. This approach is much more "local" and avoids formulas that depend on large numbers of cells.
I realize that text is not the best language for describing this, and my poor writing skills might be adding to the challenge, so please let me know if more detail is needed.
Interesting problem, thanks.
Could I suggest a really quick and [very] dirty vba? Something like the below. Obviously, have a backup of your file before running this. This assumes you want to start calculating from row 13.
Sub calculateP()
'start on row 13, column P:
Cells(13, 16).Select
'loop through every row as long as column A is populated:
Do
If ActiveCell(1, -14).Value = "" Then Exit Do 'column A not populated so exit loop
'enter formula:
Selection.FormulaR1C1 = _
"=IF(AND(NOT(RC[-11]=R[1]C[-11]),RC[-8]=R[1]C[-8]),PRODUCT(IF(R[-11]C5:RC[-11]=R[-1]C[-11],R2C15:RC[-1],""""))-1,"""")"
'convert cell value to value only (remove formula):
ActiveCell.Value = ActiveCell.Value
'select next row:
ActiveCell(2, 1).Select
Loop
End Sub
Sorry, this is definitely not a great answer for you... in fact, even this method could be achieved more elegantly using range... but, the quick and dirty approach may help you in the interim ??

Resources