I found this amazing formula:
=SUM(INDEX(data,N(IF(1,{1,3,5})))) by Jeff Weir.
But I need to reference the numbers inside an array with some formula. Here is an example:
=SUM(INDEX(data,N(IF(1,{Rows(A4:A7),3,5}))))
Excel does not want to execute this. What can I do?
My problem is more complicated in reality, but this info will help me a lot (I think).
//EDIT
My goal is for Excel to return me the "Price" of a certain "Name" (let's say Oil), within a certain "Date" range (let's say within March 2020). Also, it has to be the latest date in this range. So, in this case, I want Excel to return me the price "80" (the price of Oil), as 20.3.2020 is later than 2.3.2020. Imagine I have a lot of data, with a lot of names, dates, and prices.
Use CHOOSE to return the array:
=SUM(INDEX(data,N(IF(1,CHOOSE({1,2,3},ROWS(A4:A7),3,5)))))
Depending on one's version this formula may require the use do Ctrl-Shift-Enter instead of Enter when exiting edit mode.
Related
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")
I'm trying to write a formula in Excel where if value in B is “Eligible/Previously Eligible” and value in C is more than 365 days before today and value in D contains either 10 or 20, then return the value in A. I’ve been searching around and have written this:
=IFERROR(INDEX($A:$A,SMALL(IF((COUNTIFS($B:$B,"Eligible/Previously Eligible",$C:$C,"<"&TODAY()-365,$D:$D,{"*10*","*40*"})),ROW($A:$D)-MIN(ROW($A:$D))+1),ROW(A1)),COLUMN(A1)),"")
And have activated with the CTRL+Shift+Enter combo, but it just pulls in everything from A regardless of what is in B, C, or D:
#Solar Mike and #Scott Craner, thanks! This has gotten me closer but not quite there. I have a formula now that works to return the ID numbers that meet the criteria:
=IF(AND(B2="Eligible/Previously Eligible",D2<TODAY()-365,D2<>"",OR(SUM(COUNTIF(C2,{"*10*","*20*"})))),A2,"")
But I still can't get it to give me a list without white space. So, I can get what's in the "ID Numbers with Problems" column, but what do I need to write to get it to show the way I've done it manually in the "What I want" column?
image of what
Add a helper column:
=and(find("*elig",B2,1)>=1,now()-c2>=365,D2/10<=2)
Drag down to test each row. Then a results table based on those that give true.
Or use sumproduct() with column A to give the IDs.
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:
In my organisation, there are a couple of excel functions that return large array like more than 2000 rows and several columns.
Dummy Code / Dummy Example :
{=FunctionThatReturnArray(param1)}
where param1 is the date
I need to retrieve the selling price for the combination « Shoes » « Yellow » for different dates.
I don’t want to display an entire array, for every date I’m interested in.
Instead, I would like to display only the value that I need.
I tried to used the Index function as below, but as the combination Shoes/Yellow isn’t always at the fifth row, it doesn’t work.
{=INDEX(FunctionThatReturnArray(param1),5,4)}
where 5 is the RowNumber and 4 the ColNumber
I believe I need to use the Match Function somehow, but on the 2 different column.
How could I do that without displaying the entire array on my worksheet ?
Thanks in advance and kind regards
Largo
I'm not sure if this is the answer you want, but you are right, you would have to match on both the 2nd and 3rd columns then index into the 4th column like this:
=INDEX({43466,"Pants","Yellow",40;43466,"Shirt","Green",20;43466,"Shoes","Blue",70;43466,"Shoes","Yellow",75},MATCH(1,
(INDEX({43466,"Pants","Yellow",40;43466,"Shirt","Green",20;43466,"Shoes","Blue",70;43466,"Shoes","Yellow",75},0,2)=B2)*
(INDEX({43466,"Pants","Yellow",40;43466,"Shirt","Green",20;43466,"Shoes","Blue",70;43466,"Shoes","Yellow",75},0,3)=C2),0),4)
Where I have used an array constant to test it, you would need multiple references to your Array Function to achieve this.
BTW the 43466 is the number representation of 1/1/2019.
I have a view setup with a map reduce. Right now this code works great:
function(doc) {
if (doc.type == 'test'){
if(doc.trash != 1){
for (var id in doc.items) {
emit([id,doc.items[id].name], 1);
}
}
}
}
function(keys,prices){
return (keys, sum(prices));
}
I get a return and when using the group parameter, it condenses everything just fine.
My issue/question, I want to add a third key.... DATE, so I may only reduce records from certain dates. So for example:
function(doc) {
if (doc.type == 'test'){
if(doc.trash != 1){
for (var id in doc.items) {
emit([date,id,doc.items[id].name], 1);
}
}
}
}
My issue is that since date is at the beginning of the array, the reduce groups by date, id etc. I know I use group_level and say just take the first key from the array or the first 2 keys, but that doesn't help either because afaik, group_level goes from left to right in the array. I could put the date on the end of the emit array, but that doesn't help either because I need to have values at the beginning of my startkey and endkey to search on.
Here is an example of the output of data:
{"key":["2012-03-13","356752b8a5f6871f3","Apple"],"value":1},
{"key":["2012-03-20","123752b8a76986857","Pear"],"value":1},
{"key":["2012-04-12","3013531de05871194","Grapefruit"],"value":1},
{"key":["2012-04-12","356752b8a5f6871f3","Apple"],"value":1},
I want APPLE to be added up in one row, here it's adding up apples by date first. I was able to successfully just add up all the apples if I remove DATE as the first key in the array, but then I can't search by date range.
Any ideas on how to accomplish this?
If I correctly understand what you want to do, then you'd want to put the date as the first element of your array, and use group_level as well as start_key and end_key.
Eg. startkey=[1, "someid"] endkey=[1,"someid",{}] group_level=2
Will get you all items from date 1 (obviously choose your own format here), with id "someid" and any name. It seems funny that you emit id's before names, and without having more information about what you're actually trying to accomplish, it's hard to advise your general data model. If ID is a "type" id meaning that many items share the same ID then this makes sense. If ID is a unique per item ID, then it does not. In that case, you'd want to emit "name" before ID...
Edit 1
As per your comment, to do a range of dates you do this:
startkey=[1] endkey=[5,{}] group_level=2
You will get everything from date 1 to date 5 grouped by id ie. apples, oranges etc. I use this exact technique in a very large scale production application. I actually formatted the dates as an easily human readable integers of the format yyyymmdd, so 20140624 would sort to the top. If I want everything from the start of the month till now grouped by my group ids, I call
startkey=[20140601] endkey=[20140624,{}] group_level=2
It works perfectly and as far as I can tell that's what you're looking to do. I also have a third key layer "detail" which allows me to provide a deeper level of grouping for items that need it. I can then call
startkey=[20140601, "someid"] endkey=[20140624, "someid",{}] group_level=3
To drill to the detail level for a particular id, or just use the previous query with group_level=3 if I want the details for every id. I'm certain you can make this work - I've solved this exact problem in a production application using the techniques described.
Edit 2
If you want to group all apples regardless of date, then you'll need to let apples be the first element in the key. You can then get all apples over all time as a single row in the view result using group_level=1, and Apples over a date range using group_level=2. The difference here is that you'll only be able to do the group_level=2 query on a single item type at a time. If you want the best of both worlds, you unfortunately just need to make 2 views. That's just how key ordering works... If you need fast response times for both types of queries, all item types over a date range, and all of a particular item not grouped by date, I believe 2 views is the only way to achieve that.
Note
Another thing to note is about your reduce function. Wherever possible it is highly recommended that you use the built in reduce functions. They're implemented in erlang and are highly optimized compared to custom javascript reduce functions.
In your case, just replace your reduce function with this
_sum
Easy hey?
If you post more info about your application, data model etc. then I'd be happy to help out more with your database design.