Can I create an array with indirect? - excel

I have some weekly stock return data that looks like this:
I've hidden a lot of rows there, obviously. But basically there are 200ish different stocks and their weekly returns over the course of 10 years. They are paired with a return in column E for the same period for the S&P 500. I want to calculate betas with these Covariance(Stock, S&P 500)/Variance(S&P 500). I'm struggling just to create the Covariance portion right now:
Column A in my new sheet is a unique list of all the ticker symbols and my formula is as follows:
=COVARIANCE.P(INDIRECT("'Weekly Data'!D$"&MATCH(A2,'Weekly Data'!F:F,0)&":'Weekly Data'!D$"&MATCH(A2,'Weekly Data'!G:G,0)),INDIRECT("'Weekly Data'!E$"&MATCH(A2,'Weekly Data'!F:F,0)&":'Weekly Data'!E$"&MATCH(A2,'Weekly Data'!G:G,0)))
Getting a #REF error.

Your concatenation
"'Weekly Data'!D$"&MATCH(A2,'Weekly Data'!F:F,0)&":'Weekly Data'!D$"&MATCH(A2,'Weekly Data'!G:G,0)
leads to the wrong string representation of a cell range address.
It leads to 'Weekly Data'!D$2:'Weekly Data'!D$528 in case of ORCL. But in Excel it would must be 'Weekly Data'!D$2:D$528. Note, the sheet name only once.
So correct formula would be
=COVARIANCE.P(INDIRECT("'Weekly Data'!D$"&MATCH(A2,'Weekly Data'!F:F,0)&":D$"&MATCH(A2,'Weekly Data'!G:G,0)),INDIRECT("'Weekly Data'!E$"&MATCH(A2,'Weekly Data'!F:F,0)&":E$"&MATCH(A2,'Weekly Data'!G:G,0)))
But as often INDIRECT could be replaced by INDEX. This is the better approach because of the volatile behavior of INDIRECT, which gets recalculated on each change in sheet and not only on change of the cells in function parameters.
The INDEX formula would be:
=COVARIANCE.P(INDEX('Weekly Data'!$D:$D,MATCH(A2,'Weekly Data'!$F:$F,0)):INDEX('Weekly Data'!$D:$D,MATCH(A2,'Weekly Data'!$G:$G,0)),INDEX('Weekly Data'!$E:$E,MATCH(A2,'Weekly Data'!$F:$F,0)):INDEX('Weekly Data'!$E:$E,MATCH(A2,'Weekly Data'!$G:$G,0)))

Related

calc/excel: retreive last value of a row after finding value in column

I have a calc spreedsheet, which is mostly similar to excel regarding formulas. In one sheet I have the details of some boughts I've done (comprasDet) and in another the items I can buy (productos).
The first entry in comprasDet looks like this:
I need to retreive from productos the last registered cost from "Shampo y Mascarilla Chocolate....", put it in ctUnit (that stands for unitary cost) and then calculate the entry cost in ctTot.
The last registered costs in the productos sheet are:
In this case, I need the value in the F12 cell - $6.00. Now, I know that with:
MATCH(D2,$productos.D:D,0) (written in comprasDet)
I can get the row number of "Shampo y Mascarilla Chocolate..." in productos (12), and with (formula taken from this this post):
LOOKUP(2,1/($productos.12:12<>""),$productos.12:12)
I get actually the last value in the 12th row ($6.00) - the thing is, I need to be able to look for the last price for any given value in comprasDet, for example, if instead of "Shampo y Mascarilla Chocolate..." I have "Keratina BrasileƱa (1 lt)" I'll would like to get the F4 value in productos ($7.00).
The sheet productos will be getting more columns as the prices vary, for example, in the previos print, there were only two price columns (ct20210918 and ct20211001), here is an example with another price modification:
And, in this scenario, the price I need to get from "Shampo y Mascarilla Chocolate..." with a formula inside the comprasDet sheet will be the value in productos in cell G12, $8.00.
Note: I know that in excel the notation to reference cell/s in another sheet is Sheet_name!First_cell:Last_cell, in libreoffice calc changes a little, Sheet_name.First_cell:Last_cell, but the formulas are pretty much the same.
Thanks in advance.
Edit: With the new requirements, you need to use COLUMN and MAX to find the last used column. Because you are looking for the last used column of a particular row, you need to use INDIRECT to reference only that row. Here is how you do it in excel. You will have to modify it for your program
=INDEX(productos!A:AD,MATCH(D2,productos!D:D,0),MAX(COLUMN(INDIRECT("R"&MATCH(D2,productos!D:D,0)&"C1:"&"R"&MATCH(D2,productos!D:D,0)&"C30"))))
Explanation:
MATCH(D2,productos!D:D,0) gets the correct row in the productos table. I'll call it MyRow.
INDIRECT("R"&MyRow&"C1:"&"R"&MyRow&"C30") creates a reference to the first 30 columns of MyRow. I'll call it MyRange. Note, "30" is an arbitrary number. Make it large enough that you always get the data.
COLUMN(MyRange) creates an array of column indexes for all filled cells in MyRange.
MAX(COLUMN(MyRange)) gets the largest column index, which is the last filled cell. I'll call this MyCol.
`=INDEX(productos!A:AD,MyRow,MyCol)' returns the last used column of the row you are interested in.
Original Answer:
If this was excel you would use XLOOKUP.
=XLOOKUP(D2,'productos'!D:D,'productos'!F:F,"-",0,-1)
If your program has access to the index function, this should work:
=INDEX($productos.F:F, MATCH(D2,$productos.D:D,0))

SumIF Using Table/Named Range Instead of Single Cell Criteria

I have 2 sheets in a workbook (Sheet1, Sheet2).
Sheet 2 contains a table (Named Table1) with 5 columns:
Takeaways
Household
Clothing
Fuel
Groceries
On sheet one, I have 2 columns:
Expense Name
Expense Total
Now, what I am trying to do is:
Set the range for the Expense Name (Range 1)
Set the range for the Expense Total (Range 2)
Compare Range 1 with the respective column in the table and only add up the values for matches
For example, in Range 1 (B6:B16):
BP
Caltex
McDonalds
KFC
In Range 2 (C6:C16):
300
400
200
150
Now, all I want to do is add up the values for the Takeaways (McDonalds, KFC) and exclude anything that DOES NOT match the criteria.
So my sum total will be all occurrences of Takeaways - provided they are listed in my table - 350 in this case.
But I cannot seem to get the formula to work.
I used these sources:
https://exceljet.net/excel-functions/excel-sumifs-function
Selecting a Specific Column of a Named Range for the SUMIF Function
and ended up with this formula:
=SUMIF($B$6:$B$16;Table1[Takeaways];C6:C16)
This source:
https://excelchamps.com/blog/sumif-sumifs-or-logic/
and ended up with this formula:
=SUM(SUMIFS(C6:C16;B6:B16;Table1[Takeaways]))
Both formulae return 0.
BUT, with BOTH of them, if I change Table1[Takeaways] to "McDonalds", then it correctly identifies every occurrence of the word "McDonalds" in Range 1.
EDIT:
I have updated the formulae above to match the images below.
This is the table that contains the references:
This table contains the data:
Formula:
Cell C4 (Next to Takeaways): =SUMIF($B$6:B$16;Table1[Takeaways];C6:C16)
Cell C5 (Next to Fuel): =SUM(SUMIFS(C6:C16;B6:B16;Table1[Fuel]))
It appears that ONLY BP is being detected in the formula.
This is a an output table when I use the formulae with a single cell reference and not a table or used range:
Formula:
Cell F4 (Next to BP): =SUMIF($B$6:B$16;"BP";C6:C16)
Cell F5 (Next to Caltex): =SUM(SUMIFS(C6:C16;B6:B16;"Caltex"))
Cell F6 (Next to McDonalds): =SUMIF($B$6:B$16;"McDonalds";C6:C16)
Cell F7 (Next to KFC): =SUM(SUMIFS(C6:C16;B6:B16;"KFC"))
If I understand correctly what you're trying to achieve, I think your setup is not right conceptually.
It looks like you're trying to track expenses, and each expense (or payee) is allocated to a category ("Takeaways", "Household" etc.). From a relational-model point of view, your second table (which defines the category for each expense/payee) should only have two columns (or variables): Expense Name and Expense Category.
The table you set up ('Sheet 2') uses the categories (i.e., possible values) as different columns (i.e., variables). But there's only variable, namely the "Expense Category", and the categories themselves are the possible values.
If you set it up like that, the problem changes: you can add a dependent column to your first table that shows the category for each payee (or "Expense Name"), using a VLOOKUP() from the second table.
You can then sum the expenses for all payees matching that category.
Note: I've created the illustration using LibreOffice Calc, so there might be some small differences, but the logic is the same.
Without seeing the data in L and K I can't give you a full answer - but likely it's to do with the way you're pulling your Array
Try something similar to this
=SUMPRODUCT(SUMIFS($L$11:$L$43,$K$11:$K$43,CHOOSE({1,2},Takeaways,"anything else you wanted to sum")))
Remember SUMIFS is for multiple criteria, so if you're only calculating one, you'll need =SUMPRODUCT(SUMIF(
The way the above works is with vertical vectors only, but changing your named ranges so the table of 2 columns is 2 named ranges instead should be okay - unless it's part of your requirements
Table 2 would become expense_Name and expense_Total etc
I was about to close this as a duplicate of my own question here but there is a bit of a difference in using a named range I think. However the logic behind this follows more or less the same approach.
Working further on my partial solution below I derived the following formula:
=SUMPRODUCT(COUNTIF(Table1[Takeaways];Range1)*Range2)
The COUNTIF() part counts the number of occurrences of the cell value in your table. Therefore make sure there are no duplicates in your table. If the value is present in the table the result of COUNTIF() will be 0. This way we create a matrix of 1's and 0's. By multiplying and the use of SUMPRODUCT() we force excel to perform matrix calculations and return the correct result.
Partial solution
I used the following formula:
=SUMPRODUCT(ISNUMBER(MATCH(Range1;Table1[Takeaways]))*Range2)
The formula does the following:
The MATCH()checks if the value in Range1 is present in your table and returns the position of the matching value in your table.
The ISNUMBER() checks if a match is found by checking if the MATCH() fucntion returned a number
Multiplying this with Range2 forces matrix calculation, using the SUMPRODUCT() function
EDIT:
This worked for a really limited sample. As soon as I added the fourth row to my data the formula stopped working as intended. See screenshot:
It took the first two values into the sum correctly, the fourth is not taken into account.

Excel - SUMIFS - dynamic sum range for table

In my source data I've got a columns with data for every month and for few years.
In my working spreadsheet I want to sum the source data with 4 criteria. 3 of them are constant and one is dynamic (Month&Year). Depends on the chosen month and year I want to sum correct column in source data.
I've found a similar topic - link below:
SUMIF dynamically change summing column
However, I am getting #Values error if I input a formula. I've check that all data is text and compare with Exact function as well.
Below is my formula:
=SUMIFS(INDEX(IBRACT[#All];0;MATCH(Sheet3!G$1;IBRACT[#Headers];0));IBRACT[Entity];$A$1;IBRACT[SKU];$D14)
IBRACT is the name of table.
Below is the link to screen of evaluation (in this example I wanted to sum 6th column which in the spreadsheet is column "F"). Next step of evaluation shows #Value.
https://imgur.com/JHq80BM
Have anybody any idea how to solve this problem?
Best,
Wiktor
IBRACT[#All] includes the header row which is one row more than the two criteria ranges, hence the #VALUE! error. Just change the sum range to only include the data body range.
=SUMIFS(INDEX(IBRACT; 0; MATCH(Sheet3!G$1; IBRACT[#Headers]; 0)); IBRACT[Entity]; $A$1; IBRACT[SKU]; $D14)

Excel averageifs by looking up column for criteria

I have a spreadsheet that looks like this:
The data is in the coloured section, to the right of that is a table that shows the average of the result column when the data under ABC, XYZ, or GGG is inbetween the Max and Min Value.
This was done with this formula: =AVERAGEIFS($D$4:$D$27,A$4:A$27,"<"&$F5,A$4:A$27,">"&$G5)
What I want to do is have that formula reference the heading and then lookup the appropriate column in the data table to apply the averageif criteria to.
The formula I have now works fine but as I add more columns and the data table that may be out of order it would be much better if I was able to incorporate a lookup or match into the averageifs formula.
The following non-volatile function will do it:
=SUMPRODUCT(($A$1:$C$1=H$1)*($A$2:$C$21>=$G2)*($A$2:$C$21<=$F2)*($A$2:$C$21))/SUMPRODUCT(($A$1:$C$1=H$1)*($A$2:$C$21>=$G2)*($A$2:$C$21<=$F2))
EDIT
To get the average of the results column when the other column is within the constraints. Change the fourth Criterion in the first SUMPRODUCT():
=SUMPRODUCT(($A$1:$C$1=H$1)*($A$2:$C$21>=$G2)*($A$2:$C$21<=$F2)*($D$2:$D$21))/SUMPRODUCT(($A$1:$C$1=H$1)*($A$2:$C$21>=$G2)*($A$2:$C$21<=$F2))
EDIT2
Another option that is both non volatile and non array:
=AVERAGEIFS(D:D,INDEX(A:C,0,MATCH(H$1,1:1,0)),">=" & G4,INDEX(A:C,0,MATCH(H$1,1:1,0)),"<=" &F4)
if you are doing it over a small number of calcs then the Offset function will work for you replace your reference to your current A column with
OFFSET($A$4,0,MATCH(h$2,$a$2:$d$2,0))-1,24,1)
The offset is broken down in to $A$4 being your reference point.
The 0 being how many rows to move from the reference point to start
The MATCH(h$2,$a$2:$d$2,0))-1 being how many columns to move from the reference point
The 24 is the number of rows to return
The 1 is how many columns to return
The match formula looks for the value in H2 in your header road in A2:D2 for an exact match and returns the number for the spot the item was found. We subtract 1 from this result as we only want to move one less spot than where the item was found. So it if was found in the 1st spot we dont want to move over any columns so we need to make the value 0.
You end formula would look something like:
=AVERAGEIFS($D$4:$D$27,OFFSET($A$4,0,MATCH(h$2,$a$2:$d$2,0))-1,24,1),"<"&$F5,OFFSET($A$4,0,MATCH(h$2,$a$2:$d$2,0))-1,24,1),">"&$G5)

Ignoring Hidden Table Rows in Excel Formula

Good Evening.
I am trying to get my formulas to ignore hidden rows in a filtered table. I have attempted some of the tricks shown here, but I haven't been successful.
The CSV I get from BigFix (network management tool) looks something like:
The applications column lists the applications in the same cell. As a result, when I do a count function to identify the issues, I have to use a wildcard (I'm searching for the results in a different tab).
=COUNTIF('Input Data'!C:C,"*"&Results!A2&"*")
I want to be able to filter the table on the first tab, and have the formula results show up accurately on the 2nd tab.
Any ideas?
UPDATE:
I feel more information would help. The reason I want the "results" tab to update automatically is that I plan to build graphs out of the information on that tab. I would love to be able to filter the table in the 'input data' to include only Department A and have the graph autoupdate to Department A's info.
The excel files I'm working with have up to a thousand entries, and I'm trying to get graphs I can copy/paste to put into a presentation as efficiently as possible.
=SUMPRODUCT(SUBTOTAL(3,OFFSET('Input Data'!C:C,ROW('Input Data'!C:C)-MIN(ROW('Input Data'!C:C)),,1))*(ISNUMBER(SEARCH(A2,'Input Data'!C:C))))
In the above formula Restrict the C:C to your actual data range or use a named range to identify the Actual Data Range.
Edit
Above formula should be replaced with 103 instead of 3 as pointed by Jeep
=SUMPRODUCT(SUBTOTAL(103,OFFSET('Input Data'!C:C,ROW('Input Data'!C:C)-MIN(ROW('Input Data'!C:C)),,1))*(ISNUMBER(SEARCH(A2,'Input Data'!C:C))))
In the above formula Restrict the C:C to your actual data range or use a named range to identify the Actual Data Range.
Adding Explanation based on OP's Request.
In the above formula Used Sumproduct() Subtotal() Offset() Min() Row() IsNumber() Search() function combinations for arriving your expected result.
Row() - Will get the Row Number of a given range
Min() - Will get the Minimum Value of a given Numbers
Offset() - Is used to redirect the reference to each cell of a given range.
Subtotal() - Is used to find the (un)hidden state of redirected reference.
Search() - Is used to find the specific text in a given range (Not case sensitive) will result Number or error.
IsNumber() Is Used to check whether search returns Number or error. So Isnumber will return boolean True/False as result.
Column-C Column-D
Data For Filtering
a 1
b 1
a 1
a 2
a 2
The above data starts from 1st Row with headers in the 1st Row. Assume that I filtered D column With 1. Using the below formula will result 2.
=SUMPRODUCT(SUBTOTAL(103,OFFSET('Input Data'!C2:C6,ROW('Input Data'!C2:C6)-MIN(ROW('Input Data'!C2:C6)),,1))*(ISNUMBER(SEARCH("a",'Input Data'!C2:C6))))
ROW('Input Data'!C2:C6) = {2;3;4;5;6}
MIN(ROW('Input Data'!C2:C6)) = 2
ROW('Input Data'!C2:C6)-MIN(ROW('Input Data'!C2:C6)) should be read as
{2;3;4;5;6}-2 = {0;1;2;3;4}
OFFSET('Input Data'!C2:C6,ROW('Input Data'!C2:C6)-MIN(ROW('Input Data'!C2:C6)),,1) = 'Input Data'!C2,'Input Data'!C3,'Input Data'!C4,'Input Data'!C5,'Input Data'!C6
SUBTOTAL(103,OFFSET('Input Data'!C2:C6,ROW('Input Data'!C2:C6)-MIN(ROW('Input Data'!C2:C6)),,1)) should be read as
SUBTOTAL(103,'Input Data'!C2,'Input Data'!C3,'Input Data'!C4,'Input Data'!C5,'Input Data'!C6) = {1;1;1;0;0}
Subtotal() arrived the visible state of each (cell) reference.
SEARCH("a",'Input Data'!C2:C6) = {1;#VALUE!;1;1;1}
ISNUMBER(SEARCH("a",'Input Data'!C2:C6)) should be read as
ISNUMBER({1;#VALUE!;1;1;1}) = {TRUE;FALSE;TRUE;TRUE;TRUE}
SUBTOTAL(103,OFFSET('Input Data'!C2:C6,ROW('Input Data'!C2:C6)-MIN(ROW('Input Data'!C2:C6)),,1)) = {1;1;1;0;0}
(ISNUMBER(SEARCH("a",'Input Data'!C2:C6))) = {TRUE;FALSE;TRUE;TRUE;TRUE}
{1;1;1;0;0}*{TRUE;FALSE;TRUE;TRUE;TRUE} = {1;0;1;0;0}
SUMPRODUCT({1;0;1;0;0}) = 2
Used Sumproduct() to avoid Array Entry and also to do the Array Formula Task (Looping through the range of cells) and Sumproduct() will result the Sum of the passed values.
Instead of filtering the source data, use the Countifs function and add the filter conditions to the Countifs.

Resources