Arrays, SUM + INDEX/MATCH - arrays

Note: tried in Excel and Google Sheets, but I have a preference for Sheets.
Basically I want to get the sum of a group of data using INDEX and MATCH (because the parameters are going to be drop-down dependent):
The desired result is:
So this will require a few things:
Converting the cell D13(April) to a Month
Converting the "weekof" column to a Month
Using INDEX and MATCH and MATCH again, I'm assuming because it's multiple cell references.
Here's my solution currently below:
=SUM(INDEX(D5:I9, MATCH(MONTH(D13&1),ARRAYFORMULA(MONTH(C5:C9)),0), MATCH(E12,D4:I4,0)))
This returns the NEAREST value:
270
Instead of:
804
Why this value?
270+500+34 = 804

If you are not strict to use INDEX and MATCH, you may use the following solution:
Add extra column name it "Month", this column will extract the month name from the date column using TEXT function as the following:
=IF(C3<>"",TEXT(C3,"mmmm"),"")
The if statements ensures that only filled dates will have a month value, since you have to fill this column with the above formula for a certain amount of cells.
Now you can simply use the SUMIF function in cell E13 or where ever you want:
=SUMIF(B:B,D13,D:D)
If you don't want the Month column to appear within your data table you may put it at the end of your table and hide it.

You could directly use FILTER then SUM the result instead to simplify your formula to this one:
Formula:
=SUM(FILTER(D:D, TEXT(C:C,"MMMM") = E13))
Output:
UPDATE:
The above formula should also update when the value is dropdown. Dropdown is just data that can be changed with predetermined values, aside from that, it should be the same when using a normal cell.
To match columns, use MATCH and INDEX together with the formula above. See modified formula below.
Be careful of the circular dependency. make sure your ranges doesn't interfere with the actual cell where you put your formula.
Column Matching:
=SUM(INDEX(FILTER(D:E, TEXT(C:C, "MMMM") = E13),,MATCH(F12, D4:4, 0)))

You can use pivot table and group dates by year and month.

Related

How do I change my multi criteria Index Match formula in such a way that it sorts results closest to today?

How can do I write an array formula in such a way that both following factors apply:
Results give me the names of sales that have either TRUE OR FALSE next to it in a different column/sheet.
Results are sorted chronologically based on the date that is connected to each sale. Each sale has a different date next to it. This can be found in the same sheet as where the "TRUE OR FALSE" result is displayed. Column with the dates is called "AY:AY". I use an indirect formula to target the correct sheet within the spreadsheet.
I currently only have the first criteria implemented, don't know how to do the 2nd one.
Since the raw data is not ordered I need this to happen when I use the Index Match Array formula. So far I have this formula in my Google Sheets spreadsheet.
=ArrayFormula(iferror(index(indirect("'"&$B$5&" 2023'!c:c");small(if(TRUE=indirect("'"&$B$5&" 2023'!ca:ca");row(indirect("'"&$B$5&" 2023'!ca:ca"))-row(indirect("'"&$B$5&" 2023'!$ca$1"))+1);row(1:1)));""))
I know I could use the Index Array formula below with multiple criteria, but don't know how to implement the date component.
INDEX(indirect("'"&$B$5&" 2023'!c:c");SMALL(IF(COUNTIF(_______)*COUNTIF("true"; indirect("'"&$B$5&" 2023'!CA:CA"); ROW(indirect("'"&$B$5&" 2023'!A:CA"))-MIN(ROW(indirect("'"&$B$5&" 2023'!A:CA"))+1); ROW(indirect("'"&$B$5&" 2023'!A1));COLUMN(indirect("'"&$B$5&" 2023'!A1))
Thanks in advance.
A query like this could help?
=QUERY(INDIRECT("'"&$B$5&" 2023'!C:CA"),"SELECT C,AY WHERE CA = TRUE order by AY")

Google Data Studio, how to get a sum of all Max or Min values

I am working with a data set where i have to get Min or Max for different text fields. My dataset can have thousands of rows so below is a simpler example. So I have 3 categories having multiple values and I can put this dataset in GDS to build a table where I select Category as dimention and Value as Max(Value) in metric.
Now I need to see the sum of all those values too. But like the pivot table in excel, the subtotal in GDS shows the Max out of all the max listed above. So instead of 65, it shows 30 in GDS. Is there a way I can get it to show the sum?
To reach the desired result you will need:
Make a data combination, not being necessary to insert a second base, just so that a current base is defined as a data combination.
In the combination use the Category dimension and define the Max Value metric. The combination is only necessary for the metric to be used in the table as a dimension (this is a property resulting from the combination of data).
Configure the table with the Category dimension and Include the metric with the Value sum option. Remember that now Value is the maximum value (as defined in the data combination).
Finally, display the Summary line. And the desired result is obtained

SumProduct multiple criteria with text

I have a bank register on the left. I want a code on the right to tell me sum all the times the description "Rent" is paid in that month. I've tried index, sumproduct and sumif. I can't find the correct way to have it search by month, year and text.
It isn't clear what format your tables are in.
SUMIFS is the easiest solution but it will only work if the bank register uses the Excel date format. It gets a little complicated if those dates are formatted as text.
Building a SUMIFS formula begins with the SUM RANGE. This is a welcome change away from the backwards construction and wrongly named SUMIF (looks more like an IFSUM if you ask me)
=SUMIFS(SUM RANGE, CRITERIA 1 RANGE, CRITERIA 1, CRITERIA 2 RANGE, CRITERIA 2, CRITERIA 3 RANGE, CRITERIA 3...)
Using your example:
SUM RANGE, the 'Debit' column of the bank register (BankRegisterDebitRange)
CRITERIA 1 RANGE, the 'Description' column of the bank register (BankRegisterDescriptionRange)
Criteria 1, the string "Rent"
Criteria 2 Range, the 'Date' column of the bank register (BankRegisterDateRange)
Criteria 2, this formula string ">="&EOMONTH(RentTableDate,-1)+1
Criteria 3 Range, the 'Date' column of the bank register (BankRegisterDateRange)
Critera 3, this formula string "<="&EOMONTH(RentTableDate,0)
Putting it together:
=SUMIFS(BankRegisterDebitRange, BankRegisterDescriptionRange, "Rent", BankRegisterDateRange, "=>"&EOMONTH(RentTableDate, -1)+1, BankRegisterDateRange, "<="&EOMONTH(RentTableDate, 0))
If your data is fornatted as text then you need to decide if you want to use helper column or not or if you want a single formula. A helper column may be desirable if you want to use SUMIFS or perform additional analysis with simple formulas. A different array based formula may be desirable if you don't want a helper column, for example SUMPRODUCT or SUM.
There's already great answer showing how to use SUMPRODUCT so here is an example of how to build an array formula with SUM when your data is fornatted as text.
=SUM((BankRegisterDebitRange)*(BankRegisterDescriptionRange="Rent")*(DATEVALUE(BankRegisterDateRange)=>(EOMONTH(RentTableDate,-1)+1))*(DATEVALUE(BankRegisterDateRange)<=MONTH(RentTableDate,0)))
This is a true array formula and must be entered with Ctrl + Shift + Enter
I was surprised by some of my results during testing. Mainly how Excel was still treating text as dates in some cases but not others. For example, DATEVALUE was needed on the bank register side but that wasn't the situation with the rent table because EOMONTH worked just fine without it. I believe it is related to another odd behavior: when I create a text formatted cell in A1 and then enter a date, regardless if it is preceded by an apostrophe or not, then if I enter =A1+1 in any other cell, that cell becomes formatted as text and displays Excel's numerical value of the day after the date in A1. What I expected was a #Value! error. I suspect this is what keeps EOMONTH from bonking and the mysterious nature of arrays somehow preventing that behavior from carrying over to SUMIFS... but I really do not know why this is happening.
Suppose you have the following named ranges:
BankDate being the date column in your bank register table;
Desc being the description column in your bank register table;
Dr being the debit column in your bank register table.
If the look up date are text, you can use the following formula:
=SUMPRODUCT((Desc="Rent")*(TEXT(BankDate,"mmm yyyy")=G2)*Dr)
If the look up date are date, you can use the following formula:
=SUMPRODUCT((Desc="Rent")*(TEXT(BankDate,"mmm yyyy")=TEXT(G4,"mmm yyyy"))*Dr)
Change G2 or G4 in the above formulas to suit your actual case.
The logic is to use TEXT function to convert the BankDate into the same format as your look up date, and then use SUMPRODUCT function to return the rent by month.
Ps. using SUMPRODUCT may be an overkill, as SUMIFS suggested by #ProfoundlyOblivious is actually faster in excel calculation.
Ps2. as pointed out by #ProfoundlyOblivious, there is an interesting behavior in Excel in terms of treating text date as 'real' date in certain scenarios. Although it may not be relevant to the question of this post, I'd like to share some of my test results here for anyone interested:

Excel average based on row and column values

I would like to calculate the average (and other summary statistics) of algae counts based on the algal group (Column B) and the month (collection dates are in row 1). The first few columns and rows look like this:
row B---------------------C-------------------D
Algal Group -------6/5/2000 ----------7/5/2000-----etc.
Cyanobacteria-------5---------------------60
Bacillariophyta-------2---------------------40
Xanthophyta-------[blank]---------------30
I've tried using the standard AVERAGEIFS combined with the MONTH forumla, but I think the row of dates is throwing it off. I've also tried inserting a new row below the dates (i.e., row 2) with the Month of the collection date in row 1, and referring to that column in an AVERAGEIFS. That didn't work either. I've also tried entering it as an array formula. The dates are C1:DW1, Algae Groups are B2:B246. Any thoughts are much appreciated!
Use an array formula like this:
=AVERAGE(IF((MONTH($C$1:$E$1)=H8)*($B$2:$B$4=H9)*($C$2:$E$4<>""),$C$2:$E$4))
Being an Array it needs to be confirmed with Ctrl-Shift-Enter. If done correctly Excel will put {} around the formula.

VLOOKUPs to Populate an Excel Table

I'm having trouble writing a VLOOKUP to sort some data.
I have one table that has data that looks like this:
MarkAsOfDate MaturityDate ZeroRate
05-May-15 05-May-15 0.006999933
05-May-15 06-May-15 0.006999933
05-May-15 05-Jun-15 0.008996562
05-May-15 06-Jul-15 0.008993128
... ....
I want to make a table with every instance where the interval between the dates in the first and second columns is exactly one month (such as 05 - May - 15 and 05 - Jun - 15), and with blanks where no such value exists.
So I made a second table which looks like:
MarkAsofDate MaturityDate Zero Rate 1M
5-May-15 5-Jun-15
6-May-15 6-Jun-15
7-May-15 7-Jun-15
8-May-15 8-Jun-15
9-May-15 9-Jun-15
.... ....
I want to populate this table using data from the first table. I've tried to write a VLOOKUP for it but I'm not sure how to do it with two columns instead of one.
Thanks in advance.
Vlookup has some limitations, as you're starting to see. Another suggestion is Index/Match. Use this in your second table.
Note: I assume your top table is in Sheet1. Put this in your C2, in the second table (under the "Zero Rate 1M" header):
=Index(Sheet1!$C:$C,match(A2&B2,Sheet1!$A:$A&Sheet1!$B:$B,0)) and enter by pressing CTRL+SHIFT+ENTER.
There are a lot of ways to do it - if your data is sorted first by MarkAsofDate and then by Maturity date, the simplest method will be to add a helper column on your raw data tab - let's say column E. In column E, starting at E2 and copied down, type [Assuming MarkAsofDate is column A & MaturityDate is column E]:
=IF(MONTH(B1)-MONTH(A1)=1,A1,"")
This column will show the MarkAsOfDate for every item which has a MaturityDate 1 month after the MarkAsOfDate; for all other rows it will show "".
In your special data results tab, use the MATCH function to find the row in column E which matches your current row's MarkAsOfDate. We will also use the INDEX function to return the value from that row in column C. Assuming your first sheet with raw data is sheet1 and your special data results is on sheet2, type this into E2 on sheet2 and drag down:
=INDEX(Sheet1!C:C,MATCH(A2,Sheet1!A:A,0))
Another alternative (apart from BruceWayne's recommended Array Formula) would be to use the OFFSET function. OFFSET creates a new range based on a starting point, moving a number of cells to the right/left/up/down, for a given height and width. In this case, we will first use MATCH to find the first time that the MarkAsOfDate on Sheet1 matches Sheet2. We will use that info and the OFFSET function to create a new range which starts there, and ends at the bottom of your data, like so:
=OFFSET(Sheet1!A1,MATCH(A2,Sheet1!A:A,0),1,COUNT(Sheet1!C:C),2)
Then we just need to use VLOOKUP on the range we created above, like so:
=VLOOKUP(B2,OFFSET(Sheet1!A1,MATCH(A2,Sheet1!A:A,0),1,COUNT(Sheet1!C:C),2),0)
This second alternative avoids needing a helper column, but is more complex and could be prone to errors if your rows/columns change (because we had to hardcode a couple of things in the OFFSET function). Also, OFFSET is volatile, meaning it recalculates whenever any cell calculates, so it can slow down your workbook if you have a lot of rows of it. Based on that, I recommend you either use the helper column method above, or if you are comfortable with Array Formulas, using BruceWayne's answer.

Resources