Is it possible to stack values from 4 different columns from two different sheets into one column in third sheet using formula? [duplicate] - excel

I have a FLATTEN LAMBDA function that flattens data in an array. This works well, but I want to integrate another array argument so I can use non-contiguous ranges.
In my example, the range A1:B6 is housed in array and returns the flattened data.
How can I include an array2 argument that accepts D1:D6 as an additional range?
Formula:
FLATTEN =
LAMBDA(array,
LET(
rows,ROWS(array),
columns,COLUMNS(array),
sequence,SEQUENCE(rows*columns),
quotient,QUOTIENT(sequence-1,columns)+1,
mod,MOD(sequence-1,columns)+1,
INDEX(IF(array="","",array),quotient,mod)
)
)

Edit 7/4/22:
ms365 now has introduced a function called VSTACK() and TOCOL() which allows for the the functionality that we were missing from GS's FLATTEN() (and works even smoother)
In your case the formula could become:
=TOCOL(A1:D6,1)
And that small formula (where the 2nd parameter tells the function to ignore empty cells) would replace everything else from below here. If C1:C6 would hold values you don't want to incorporate you can try things like:
=VSTACK(TOCOL(A1:B6),D1:D6)
Previous Answer:
You can't really create a LAMBDA() with an unknown number (beforehand) of arrays to include in flatten. The fact that you have arrays of multiple columns will contribute to the "trickyness". One way to 'flatten' multiple columns in this specific way would be:
Formula in G1:
=LET(X,CHOOSE({1,2,3},A1:A6,B1:B6,D1:D6),Y,COLUMNS(X),Z,SEQUENCE(COUNTA(X)),INDEX(X,CEILING(Z/Y,1),MOD(Z-1,Y)+1))
EDIT: As per your comment, you can extend this as such:
=LET(X,CHOOSE({1,2,3},IF(A1:A6="","",A1:A6),IF(B1:B6="","",B1:B6),IF(D1:D6="","",D1:D6)),Y,COLUMNS(X),Z,SEQUENCE(ROWS(X)*Y),FLAT,INDEX(X,CEILING(Z/Y,1),MOD(Z-1,Y)+1),FILTER(FLAT,FLAT<>""))

It's a cheat, but:
FLATTEN =
LAMBDA(array,
LET(
rows,ROWS(array),
columns,COLUMNS(array),
sequence,SEQUENCE(rows*columns),
quotient,QUOTIENT(sequence-1,columns)+1,
mod,MOD(sequence-1,columns)+1,
unpiv, INDEX(array,quotient,mod),
FILTER(unpiv, unpiv<>"")
)
)
Where your array has been extended to A1:D6 as the input.
I think JvdV's answer will be the best depending on the input format
you want, but I had already written this out, so here goes...
You could do:
=LET( array1, A1:B6, array2, D1:D6,
rows1,ROWS(array1), rows2,ROWS(array2),
columns1,COLUMNS(array1), columns2,COLUMNS(array2),
rows, MIN(rows1, rows2),
columns, columns1 + columns2,
sequence,SEQUENCE(rows*columns),
quotient,QUOTIENT(sequence-1,columns)+1,
mod,MOD(sequence-1,columns)+1,
IFERROR(INDEX( IF( ISBLANK(array1),"",array1),quotient,mod),
INDEX(IF( ISBLANK(array2),"",array2),quotient,MOD(sequence-1,columns2)+1) )
)
It will take multi-column/row inputs to both arrays.

Starting from the article here and updating based upon observations about empty values in the arrays and allowing varying sized arrays we can get two formulae which you should be able to translate to Named LAMBDA functions for 'stacking' and 'shelving' arrays.
Stack Arrays
=LET(rngA, A1:C5, rngB, A9:D11,
rowsA, ROWS(rngA), rowsB, ROWS(rngB),
NumCols, MAX(COLUMNS(rngA), COLUMNS(rngB)),
SeqRow, SEQUENCE(rowsA + rowsB), SeqCol, SEQUENCE(1, NumCols),
Result, IF(SeqRow <= rowsA, INDEX(IF(rngA="","",rngA), SeqRow, SeqCol),
INDEX(IF(rngB="","",rngB), SeqRow-rowsA, SeqCol)),
arr, IFERROR(Result,""), arr)
Shelve Arrays
=LET(rngA, A1:C5, rngB, B8:D12,
colsA, COLUMNS(rngA), colsB, COLUMNS(rngB),
NumRows, MAX(ROWS(rngA), ROWS(rngB)),
SeqRow, SEQUENCE(NumRows), SeqCol, SEQUENCE(1, colsA + colsB),
Result, IF(SeqCol <= colsA, INDEX(IF(rngA="","",rngA), SeqRow, SeqCol),
INDEX(IF(rngB="","",rngB), SeqRow, SeqCol-colsA ) ),
arr, IFERROR(Result,""), arr)
Once you have a contiguous array, you can apply the formula you already have:
Updated to use a spill range for ease of testing...
=LET(data, A1#,
rows, ROWS(data), cols, COLUMNS(data),
seq, SEQUENCE(rows*cols,,0),
list, INDEX(IF(data="", "", data), QUOTIENT(seq, cols)+1, MOD(seq, cols)+1),
FILTER(list, LEN(list)>0))
This approach is really geared towards the named LAMBDA functions because otherwise you will end up with monstrous formulae and the other approaches may well be better in that case.

Related

How to use Calculate with single condition on pivot?

In my pivot table I added a custom column like this:
=CALCULATE(MAX([Column1]),FILTER(table,expr))
I want to add a new condition and after a few tries, this is working as I expected
=IF([Column2]>5, CALCULATE(MAX([Column1]),FILTER(table,expr)) + 10,
CALCULATE(MAX([Column1]),FILTER(table,expr))
)
but this function is quite long (because expr pretty much the condition), I wonder if there is a LET function or some way that can shorten the above function
=CALCULATE( IF (
[Column2]>5,MAX([Column1])+10, MAX([Column1])
)
,FILTER(table,expr))
However this formula doesn't work,
Is there a better solution for this?

selecting all cells between two string in a column

I posted question previously as "using “.between” for string values not working in python" and I was not clear enough, but I could not edit, so I am reposting with clarity here.
I have a Data Frame. In [0,61] I have string. In [0,69] I have a string. I want to slice all the data in cells [0,62:68] between these two and merge them, and paste the result into [1,61]. Subsequently, [0,62:68] will be blank, but that is not important.
However, I have several hundred documents, and I want to write a script that executes on all of them. The strings in [0,61] and [0,69] are always present in all the documents, but along different locations in that column. So I tried using:
For_Paste = df[0][df[0].between('DESCRIPTION OF WORK / STATEMENT OF WORK', 'ADDITIONAL REQUIREMENTS / SUPPORTING DOCUMENTATION', inclusive = False)]
But the output I get is: Series([], Name: 0, dtype: object)
I was expecting a list or array with the desired data that I could merge and paste. Thanks.
enter image description here
If you want to select the rows between two indices (say idx_start and idx_end), excluding these two rows) on column col of the dataframe df, you will want to use
df.loc[idx_start + 1 : idx_end, col]
To find the first index matching a string s, use
idx = df.index[df[col] == s][0]
So for your case, to return a Series of the rows between these two indices, try the following:
start_string = 'DESCRIPTION OF WORK / STATEMENT OF WORK'
end_string = 'ADDITIONAL REQUIREMENTS / SUPPORTING DOCUMENTATION'
idx_start = df.index[df[0] == start_string][0]
idx_end = df.index[df[0] == end_string][0]
For_Paste = df.loc[idx_start + 1 : idx_end, 0]

How do I sum results of two if statements in the same cell?

I'm looking to have the results of two If statements calculated and added in the same cell. I'm getting #VALUE! error.
=IF(ISERROR(GETPIVOTDATA("Sum of CHARGES",'Ship City'!$A$3,"ship_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29)),"",GETPIVOTDATA("Sum of CHARGES",'Ship City'!$A$3,"ship_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29))+IF(ISERROR(GETPIVOTDATA("Sum of CHARGES",'Recipient City'!$A$4,"recipient_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29)),"",GETPIVOTDATA("Sum of CHARGES",'Recipient City'!$A$4,"recipient_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29))
=IF(ISERROR(GETPIVOTDATA("Sum of CHARGES",'Ship City'!$A$3,"ship_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29)),"",GETPIVOTDATA("Sum of CHARGES",'Ship City'!$A$3,"ship_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29))
+
IF(ISERROR(GETPIVOTDATA("Sum of CHARGES",'Recipient City'!$A$4,"recipient_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29)),"",GETPIVOTDATA("Sum of CHARGES",'Recipient City'!$A$4,"recipient_city",$B$7,"carrier_type",$A$27,"INV_month_id",D$6,"INV_year_id",$D$5,"Company Name",$B29))
Your both IF functions return an empty string "". Using + operator with strings returns #VALUE!. There are different methods to fix it:
use 0 instead of ""
use SUM instead of + (it ignores strings).
And as #John Bustos mentioned in his comment, you can simplify your formula with
IFERROR(value,value_if_error)
IFERROR description

IF function for specific text strings

I am trying to write a formula that takes a word and process it through a IF function in excel, The Values are list in the formula. My issue right now is the fact that I have Large, X-Large and 1X-Large text. The X-Large and 1X-Large are unique strings and need the IF function to be able to differentiate the two.
Here is what i have so far.
=if(or(isnumber(search("Small",af2)),ISNUMBER(SEARCH("Medium",AF2)),ISNUMBER(SEARCH("Large",AF2)),,ISNUMBER(SEARCH("X-Large",AF2))),"Small",or(isnumber(search("1X-Large",af2)),isnumber(search("2X-Large",af2)),isnumber(search("3X-Large",af2)),isnumber(search("4X-Large",af2))),"1X-Large")
I cant understand why it's showing an error and only displays small when it works.
All help is appreciated
Your current formula shouldn't work, it should be giving you an error about having too many arguments. A breakdown of your function:
=if(
or(isnumber(search("Small",af2)),ISNUMBER(SEARCH("Medium",AF2)),ISNUMBER(SEARCH("Large",AF2)),,ISNUMBER(SEARCH("X-Large",AF2))),
"Small",
or(isnumber(search("1X-Large",af2)),isnumber(search("2X-Large",af2)),isnumber(search("3X-Large",af2)),isnumber(search("4X-Large",af2))),
"1X-Large"
)
You cannot use 4 parameters in an IF. You need to have a maximum of 3. Maybe what you meant was:
=if(
or(isnumber(search("Small",af2)),ISNUMBER(SEARCH("Medium",AF2)),ISNUMBER(SEARCH("Large",AF2)),,ISNUMBER(SEARCH("X-Large",AF2))),
"Small",
IF(
or(isnumber(search("1X-Large",af2)),isnumber(search("2X-Large",af2)),isnumber(search("3X-Large",af2)),isnumber(search("4X-Large",af2))),
"1X-Large"
)
)
But that doesn't solve your issue about the X-Large part. To cater for that, you can check whether the X-Large series exist first, then the others.
=IF(
OR(ISNUMBER(SEARCH("1X-Large",AF2)),ISNUMBER(SEARCH("2X-Large",AF2)),ISNUMBER(SEARCH("3X-Large",AF2)),ISNUMBER(SEARCH("4X-Large",AF2))),
"1X-Large",
IF(
OR(ISNUMBER(SEARCH("Small",AF2)),ISNUMBER(SEARCH("Medium",AF2)),ISNUMBER(SEARCH("Large",AF2)),ISNUMBER(SEARCH("X-Large",AF2))),
"Small"
)
)
Although you can make it shorter with this:
=IF(
OR(ISNUMBER(SEARCH({"1X-Large","2X-Large","3X-Large","4X-Large"},AF2))),
"1X-Large",
IF(
OR(ISNUMBER(SEARCH({"Small","Medium","Large","X-Large"},AF2))),
"Small"
)
)
In one line...
=IF(OR(ISNUMBER(SEARCH({"1X-Large","2X-Large","3X-Large","4X-Large"},AF2))),"1X-Large",IF(OR(ISNUMBER(SEARCH({"Small","Medium","Large","X-Large"},AF2))),"Small"))

Converting Excel functions into R

I have two excel functions that I am trying to convert into R:
numberShares
=IF(AND(N213="BOH",N212="BOH")=TRUE,P212,IF(AND(N213="BOH",N212="Sell")=TRUE,ROUNDDOWN(Q212/C213,0),0))
marketValue
=IF(AND(N212="BOH",N213="BOH")=TRUE,C213*P212,IF(AND(N212="Sell",N213="Sell")=TRUE,Q212,IF(AND(N212="BOH",N213="Sell")=TRUE,P212*C213,IF(AND(N212="Sell",N213="BOH")=TRUE,Q212))))
The cells that are referenced include:
c = closing price of a stock
n = position values of either "buy or hold" or "sell"
p = number of Shares
q = market value, assuming $10,000 initial equity (number of shares*closing price)
and the tops of the two output columns that i am trying to recreate look like this:
output
So far, in R I have constructed a dataframe with the necessary four columns:
data.frame
I just don't know how to write the functions that will populate the number of shares and market value columns. For loops? ifelse?
Again, thank you!!
Covert the AND()'s to infix "&"; the "=" to "=="; and the IF's to ifelse() and you are halfway there. The problem will be in converting your cell references to array or matrix references, and for that task we would have needed a better description of the data layout:
numberShares <-
ifelse( N213=="BOH" & N212=="BOH",
#Perhaps PosVal[213] == "BOH" & PosVal[212] == "BOH"
# ... and very possibly the 213 should be 213:240 and the 212 should be 212:239
P212,
ifelse( N213=="BOH" & N212=="Sell" ,
round(Q212/C213, digits=0),
0))
(You seem to be returning incommensurate values which seems preeety questionable.) Assuming this is correct code despite my misgivings the next translation involves apply the same substitutions in this structure (although you seem to be missing an else-consequent in the last IF function:
marketValue <-
IF( AND(N212="BOH", N213="BOH")=TRUE,
C213*P212,
IF(AND(N212="Sell",N213="Sell")=TRUE,
Q212,
IF( AND(N212="BOH",N213="Sell")=TRUE,
P212*C213,
IF(AND(N212="Sell",N213="BOH")=TRUE,
Q212))))
(Your testing for AND( .,.)=TRUE is I believe unnecessary in Excel and certainly unnecessary in R.)

Resources