Excel: Select Range in Formula based on column value - excel

I have two tables that have similar columns:
Table1:
Date Ticker Data
May 2008, XYZ, 6
Aug 2010, XYZ, 5
Sep 2008, ABC, 7
Table2:
Date Ticker Data
EDIT:
* Apply SMALL formula below to ABC tickers, XYZ etc...
I'm trying to apply this formula: =SMALL($A:$A,COUNTIF($A:$A,"<="&A1))
which finds the nearest date in Table 2 from the date in Table 1
Table 2 is huge and has numerous entries of all kinds of dates and tickers.
The problem is I need to apply the SMALL formula above only to similar tickers. How do I adjust the range in the formula to ONLY apply to range with the same ticker in Table 1 and Table2? There are a few thousand so can't manually name the ranges.
Thanks!

This seemed to work for me for Excel 2010 & 2013:
{=max(if(ticker range = TICKER,if(date range <= DATE,date range)))}
CTRL+SHFT+ENTER as it is an array
I believe Excel 2016 has MaxIFS function that might make it bit easier. Will have to apply to spreadsheet and see how it works.

Related

How to create different date ranges based on criteria?

I have a table with two active columns. In Column A I have all the dates between, let`s say, January 1, 2012 to December 31, 2019. In column B I have a corresponding name. For instance:
[
etc.
I want to create ranges based on criteria, like:
The trick is, this should be done by calendar year, which means that during the 8-year period, I should have 8 set of 3 columns (from / to / name), one for each calendar year. If one range covers two years (let's say, November 1, 2012 to February 1st, 2013), the last row of 2012 should read
2012-11-01 to 2012-12-31
while the first row of 2013 will read
2013-01-01 to 2013-02-01
I managed to separate the ranges, but for some reason I am not able to go further and do that for each calendar year. Is there a way to do that?
Let's assume you want to place your 8 set of 3 columns starting from column G and that your list in the range D:F has headers in row 1 and data from row 2 on. In cell G1 write down your first year (2012), in cell H1 "From", in cell I1 "To" and in cell J1 "Name".
Now in cell H2 write this formula:
=IF(IF(OR(IF(AND($E2>=DATE(G$1,1,1),$D2<=DATE(G$1,12,31)),1,0),IF(AND($D2>=DATE(G$1,1,1),$E2<=DATE(G$1,12,31)),1,0)),1,0),MAX($D2,DATE(G$1,1,1)),"")
In cell I2 write this formula:
=IF(IF(OR(IF(AND($E2>=DATE(G$1,1,1),$D2<=DATE(G$1,12,31)),1,0),IF(AND($D2>=DATE(G$1,1,1),$E2<=DATE(G$1,12,31)),1,0)),1,0),MIN($E2,DATE(G$1,12,31)),"")
In cell J2 write this formula:
=IF(AND(H2<>"",I2<>""),F2,"")
Drag the 3 of them all the way down accordingly to your need. You can then copy the G:J range and paste any time you need next to itself; just change the year in the top left cell and it should do the trick.
Report any question you have or bug you have encountered. If, according to your judgment, this answer (or any other) is the best solution to your problem you have the privilege to accept it (link).

Copy Data from once cell to another cell based on conditions

I am facing an issue in excel in driving the formulas. I have data (Year, Salary, Name) in one sheet. It is updated vertically. In another sheet, I want to get list of salary for particular year. For 2019 there may be 4 rows for salary so I want all of them one by one. Below is the data:
Salary EMP Year
10000 a 2017
11500 b 2018
13000 c 2019
14500 d 2017
16000 f 2018
17500 s 2019
19000 g 2017
20500 h 2018
22000 h 2019
Output I require:
Year 2017 2018 2019
10000 11500 13000
14500 16000 17500
19000 20500 22000
So output should contain list of data for 2019 when I enter 2019 (all rows). there may be 50 rows in actual as I have given simple example of 8 rows only.
Kindly help me on this.
Thanks, Sharmil
I tried with pivot but it is giving total value rather than individual values. Also I tried with OFFSET, Index + Match function but it does not provide me correct output. Combination of OFFSET, Index + Match gives all rows for all single year.
Excuse me for the German language. SUMMEWENN is, indeed, SUMIF function. You can use a sumif Formula:
Sumif(Your criteria, Your search criterua, Your desired range to be summed)
Please see the below screenshot illustrating your example
Your criteria: Range C2 to C11
Your search criteria: B5 --> 2017
Your desired range to be summed: A2 to A11
Pivot Table is a possible solution but you will need a helper column to "rank" the Year.
Put the following formula in Cell D2 suppose your data range is A2:C13:
=COUNTIF($C$2:C2,C2)
Dray it down to apply across rows.
Then sort the data range by Year and then by Salary from Smallest to Largest.
Then insert a pivot table and layout the data as shown below.
The problem can also be solved by formula or power query but if a list like that is all you want I believe using Pivot Table is the most efficient way.
Let me know if you have any question. Cheers :)

Create UI to filter and aggregate in Excel

I have a long excel containing dates and many value columns. I would want to create a dynamic filter where the user can input a date range in two cells and the values from each column will be filtered by the date range and an aggregated value calculated from the filtered values will be shown.
For example in the table:
Row year weight_pounds gestation_weeks
1 2005 6.0627122049999995 38
2 2006 7.12534030784 40
3 2007 7.4406013425 39
4 2008 3.30032006214 43
5 2009 7.1099079495 41
6 2010 7.31273323054 40
7 2010 9.31232594688 42
I want to filter by year 2007 to 2009 and show the average on both value columns in the sheet.
How can I approach this in excel? I have no experience to VBA but I am open to VBA if it is required.
Define a helper column that tests whether the row is within the required date range and filter based on that column.
Suppose the start date is in X1 and end date is in X2. And suppose your main data table starts with the heading Row in A1.
Then the formula in D2 might be something like =and(B2>=X1,B2<=X2)
Copy that formula down the whole list to give a column with TRUE and FALSE values
Turn on filters on the heading row
Select just TRUE in the helper column
For the average you can either use =SUBTOTAL(101,C:C) (the 101 parameter says to give the average of the rows that are still visible) or =AVERAGEIFS(C:C,B:B,">="&X1,B:B,"<="&X2) (which works regardless of how the main data table has been filtered.

Excel SUMPRODUCT with dynamic column range to sum

A B C D
1 Department Jan Feb Mar
2 Marketing 100 200 300
3 R&D 150 250 350
4 Marketing 300 400 500
I have a sample file like the above table.
The SUMPRODUCT formula below only sums the specific rows and column, which is row 2 to 20 and column B to C for summing
=SUMPRODUCT((A2:A20="Marketing")*(B2:C20))
Actual formula (from comments):
=SUMPRODUCT(INDEX(BW!$A$3:$AE$3,MATCH(C$35,BW!$1:$1,0)+MATCH("JAN 2016",BW!$F$2:$R$2,0)-1):INDEX(BW!$A$108:$AE$108,MATCH(C$35,BW!$1:$1,0)+MATCH("A‌​UG 2016",BW!$F$2:$R$2,0)-1)*(BW!$B$3:$B$108=$E$1))
My questions, is there a way to make SUMPRODUCT dynamic, in a sense, it knows the first row to take will be 2nd and ends at 20.
As for the dynamic column to sum, it'll depend on the criteria. i.e. if the criteria is Feb and Mar, it'll take column C2:D20...again, the row is dynamic as well.
If you turn your range into a table called Table1 then the following should work:
=SUMPRODUCT((Table1[Department]="Marketing")*(INDIRECT("Table1[[Jan]:["&VLOOKUP("*",Table1[#Headers],COLUMNS(Table1),FALSE)&"]]")))
To make the SUMPRODUCT dynamic we can use INDEX/MATCH to find the extents of the data.
Then all that is need is to put the months wanted listed in one cell, I used I7
=SUMPRODUCT((ISNUMBER(SEARCH($B$1:INDEX(1:1,MATCH("ZZZ",1:1)),I7)))*($A$2:INDEX(A:A,MATCH("ZZZ",A:A))="Marketing")*B2:INDEX(A:DD,MATCH("ZZZ",A:A),MATCH("ZZZ",1:1)))
The INDEX(A:A,MATCH("ZZZ",A:A)) will find the last cell with text in Column A, and use that to set the extent of the dataset.
For the Last column we use INDEX(1:1,MATCH("ZZZ",1:1))
If you are not going to work with a ListObject (aka Structured) table, then a dynamic named range would certainly help.
Go to Formulas, Defined Names, Name Manager and when the Name Manager dialog opens, click New.
Give your defined name range a unique name; I've chosen Departments.
Leave the Scope as workbook and use the following formula for the Refers to:  =BW!$A$1:INDEX(BW!$A:$M, MATCH("zzz",BW!$A:$A ), MATCH(1E+99,BW!$1:$1 )) Columns A:M will cover a Department column and Jan through Dec. Note that Jan 2016-Dec 2016 are real dates (e.g. 1/1/2016 to 12/1/2016) formatted as mmm yyyy.
You should end up with the following dynamic named range:
Now you can 'shape' the sections of the defined range using INDEX/MATCH function pairs.
=SUMPRODUCT((INDEX(Departments, 0, 1)=Q3)*
(INDEX(Departments, 0, MATCH(R3, INDEX(Departments, 1, 0), 0)):
INDEX(Departments, 0, MATCH(S3, INDEX(Departments, 1, 0), 0))))

Excel: Calculate number of months in date range, but only for specific year

My sheet looks like this (sample data)
"Rate" "Begin" "End"
"1$" "01.01.2011" "01.01.2015"
"2$" "01.06.2012" "01.08.2013"
I want to calculate the number of months for the date ranges which fall in specific years (2012, 2013, 2014) to estimate volume (= months*rate)
For example in the 2nd line I would get 6 months for 2012 (=12$) and 7 months for 2013 (=14$).
How can I achieve this?
Assuming you have your data in A1:C3 then list the years in the header row, e.g. D1 = 2012, E1 = 2013, F1 = 2014 then in D2 use this formula:
=IFERROR(DATEDIF(MAX($B2,DATE(D$1,1,1)),MIN($C2,DATE(D$1+1,1,1)),"m"),0)*$A2
format as currency and copy across and down
Assumes you are using Excel 2007 or later and that columns B and C contain valid dates. In earlier Excel versions you can use this alternative formula
=LOOKUP(99^99,IF({1,0},0,DATEDIF(MAX($B2,DATE(D$1,1,1)),MIN($C2,DATE(D$1+1,1,1)),"m")*$A2))

Resources