Excel function to find next higher value - excel

I have data like below. I am calculating third column (next higher date. For each unique values of created date for data, I need to know what is next higher date available for data. There are duplicates. Also if there is no next higher date then I use generic date of 1/1/2017.
Last date = 1/1/2017
Now my question here is how can I achieve this using INDEX/MATCH or LARGE functions or do I need to use VBA scripting to get my results?

Per the sample image, put one if these in this in C2 and drag down.
'xl2003-xl2007
=MIN(MIN(INDEX(B$2:B$99+((A$2:A$99<>A2)+(B$2:B$99<=B2))*1E+99, , )), DATE(2017, 1, 1))
'xl2010-xl2013
=IFERROR(AGGREGATE(15, 7, B$2:B$99/((A$2:A$99=A2)*(B$2:B$99>B2)), 1), DATE(2017, 1, 1))
'xl2016/Office 365
=IFERROR(MINIFS(B:B, A:A, A2, B:B, ">"&B2), DATE(2017, 1, 1))

Related

Can SUMIFS calculate data from current month with 2 or more criteria?

I have a spreadsheet that uses a few SUMIFS formulas to calculate data based on 2 criteria and a normal range. Currently I'm having to change this manually every month to capture metrics of tickets reported that meet some of those criteria. Ideally i'd like to implement some logic to only calculate data that has dates from the current month.
Here's my original sumifs:
=SUMIFS($G$2:$G$100,$A$2:$A$100,"primary",B2:B100,"little")
=SUMIFS($G$2:$G$100,$A$2:$A$100,"primary",B2:B100,"panic")
etc..
I was told something to the extent of this would get me where I needed to be, but it doesn't seem to be actually calculating only data with dates within this month.
=SUMIFS($G$2:$G$100,$A$2:$A$100,"primary*",$B$2:$B$101,"little")+SUMIFS($G$2:$G$100,$F$2:$F$100,">="&DATE(YEAR(TODAY()),MONTH(TODAY()),1))
Any help would be greatly appreciated!!
Spreadsheet_data
Why not try using the Incredibly Versatile SUMPRODUCT Function here,
Formula used in cell I2
=SUMPRODUCT(($G$2:$G$9)*($A$2:$A$9=$J2)*($B$2:$B$9=$K2)*
(TEXT($F$2:$F$9,"mmmmyyyy")=$I$1&YEAR(TODAY())))
The SUMPRODUCT function simply multiplies arrays together and returns the sum of products. If only one array is supplied, SUMPRODUCT will simply sum the items in the array. Up to 30 ranges or arrays can be supplied.
It may seem boring, complex, and even pointless. But SUMPRODUCT is an amazingly versatile function with many uses. Because it will handle arrays gracefully, you can use it to process ranges of cells in clever, elegant ways. It can be used to count and sum like COUNTIFS or SUMIFS, but with more flexibility.
So, let me go through you a bit what the formula does,
Array 1
($A$2:$A$9=$J2) {TRUE;FALSE;TRUE;TRUE;TRUE;TRUE;TRUE;TRUE}
Array 2
($B$2:$B$9=$K2) {TRUE;FALSE;FALSE;FALSE;FALSE;FALSE;TRUE;FALSE}
Array 3
(TEXT($F$2:$F$9,"mmmmyyyy")=$I$1&YEAR(TODAY()))
{FALSE;TRUE;FALSE;FALSE;FALSE;TRUE;TRUE;TRUE}
And this is the whole part which is wrapped within SUMPRODUCT
($G$2:$G$9)*($A$2:$A$9=$J2)*($B$2:$B$9=$K2)*(TEXT($F$2:$F$9,"mmmmyyyy")=$I$1&YEAR(TODAY()))
When you select this part and press F9, it evaluates and shows you an array of numbers
{0;0;0;0;0;0;12;0}
Basically its creates an array of TRUE's & FALSE's and then multiplies with the corresponding values to give you the exact output which you need!
Also why you need to use SUMPRODUCT Function ---> Let me tell about it more
It never gives an error for text values unless you are not using a double negative or double minus, I always say SUMPRODUCT is much versatile function than other, it can be clubbed with many other excel functions without any issue.
The double negative (--) is one of several ways to coerce TRUE and FALSE values into their numeric equivalents, 1 and 0. One we have 1s and 0s, we can perform various operations on the arrays with Boolean logic. The table below shows the result in array1,array2, array3 based on the formula above, after the double negative (--) has changed the TRUE and FALSE values to 1s and 0s.
Yep, you can!
You'll need formulas to get the first date of this month and the next month:
Date(Year(Today()), Month(Today()) , 1)) -- first day of this month
Date(Year(Today()), Month(Today()) + 1, 1)) -- first day of next month
(This wraps the months intelligently i.e. Date(2021, 13, 1) is 2022-01-01)
You can then use these with greater-than and less-than operators within SUMIFS:
= SUMIFS(
value_column,
date_column, ">=" & Date(Year(Today()), Month(Today()) , 1),
date_column, "<" & Date(Year(Today()), Month(Today()) + 1, 1)
)
For example:
= SUMIFS(
G:G,
F:F, ">=" & Date(Year(Today()), Month(Today()) , 1),
F:F, "<" & Date(Year(Today()), Month(Today()) + 1, 1),
A:A, "primary",
B:B, "panic"
)
So here it's picking up the 33 and 57 to calculate the sum of 90.
Why this instead of SumProduct?
You can refer to entire columns e.g. G:G rather than a fixed range e.g. G$2:G$9999. SumProduct gives a #VALUE! error if any of the rows contain text, i.e. the column labels in the first row. SumIfs happily ignores the text.
Edit:
There may be a fault in this formula:
=SUMIFS($G$2:$G$100,$A$2:$A$100,"primary*",$B$2:$B$101,"little")+SUMIFS($G$2:$G$100,$F$2:$F$100,">="&DATE(YEAR(TODAY()),MONTH(TODAY()),1))
Making the format a little friendlier:
= SUMIFS(
$G$2:$G$100,
$A$2:$A$100, "primary*",
$B$2:$B$101, "little"
)
+ SUMIFS(
$G$2:$G$100,
$F$2:$F$100, ">=" & DATE(YEAR(TODAY()),MONTH(TODAY()),1)
)
It's adding all the Primary Littles from any month, and then also adding all the entries from this month onwards - double-counting any Primary Littles it's already counted.

How to SUMIFS horizontally with a criteria and offset

I'm having trouble with an excel formula where i'm trying to get the sum of all numbers in the row which are at 1 column offset with the criteria.
Eg. A1 = Price | B1 = $1000| C1 = Price| D1 = $1500 and so on....
Answer should be 1000+1500= $2500
I tried =SUMIF(1:1,"Price",OFFSET(1:1,0,1)) but gives me error!
I am guessing you have the cell with your formula in the same row, so it is a cyclic reference that gives you the error.
You can cut 1:1 before the cell with the SUMIF:
=SUMIF(OFFSET(1:1, 0, 0, 1, COLUMN() - 2), "Price", OFFSET(1:1, 0, 1, 1, COLUMN() - 2))
a possible solution is to offset your reference ranges. This means you will not be able to do an entire row reference. In your limited example your formula would wind up looking like this:
=SUMIF(A1:Q1,"price",B1:R1)
so you sum range will be limited to one less column than what is available to the sheet to allow for the second range (equal in size range) to be shifted one column to the right.
SIDE NOTE
Idealy you would want to arrange your data in a table. I understand this may not always be possible when dealing with 3rd party data dumps, limited VBA knowledge, and large amounts of data.
SUMIF(A20:ZZ20, "Price", OFFSET(20:20, 0, 1, 1, COLUMN() - 2))

How can I redistribute a range of values without duplicates between 1 and 100?

I have a list of values between 1 and 100, essentially a sort of ranking that occassionally skips a few numbers (for example, the first ten values are 2, 6, 6, 10, 10, 10, 10, 11, 12, 13). They're ordered ascendingly, so every number will be either higher than or equal to the number above it. Now, I wish to remove all the duplicates from this list while remaining between 1 and 100. So, for example, for the values above, something like 2, 6, 7, 10, 11, 12, 13, 14, 15, 16 would work or 2, 6, 7, 8, 9, 10, 11, 12, 13, 14. However, the formulas I've tried so far will either go over 100, go under 1, or create circular references.
Given the nature of the list, there's very little chance of the amount of values exceeding 100, so if that possibility can be accounted for, it'd be a nice bonus, but it's not required.
Please create a named range for all your numbers and name it Source. As an alternative, replace the named range Source in my formulas below with the sheet address of the range where you have your numbers.
[C2] =INDEX(Source,MATCH(0,COUNTIF($C$1:$C1,Source),0))
This is an array formula and needs to be confirmed with Ctl + Shift + Enter. Observe that the COUNTIF range is defined one row above the row in which the formula resides. Its formulated with one absolute and one relative end. The end of the range will expand as you copy the formula down.
You can achieve a similar result using the LOOKUP() function. But since MATCH() finds the first instance, LOOKUP() returns the last. Therefore the formula below will return the same sequence in descending order. LOOKUP() is capable of looping on its own and the formula doesn't require array enabling. Just confirm it normally, with only Enter.
[E2] =LOOKUP(2,1/(COUNTIF($E$1:E1,Source)=0),Source)
Observe that the result range is defined with an absolute and relative end, starting above the row of the formula as explained above.
In B2, formula copied down :
=IF(A2="","",IF((COUNTIF(B$1:B1,A2)>0)+(A2=A1),B1+1,A2))
Edit #1
If data have repeated 100 as per OP's comment, then B2 formula become :
=IF((COUNTIF(B$1:B1,100)>0)+(A2=""),"",IF((COUNTIF(B$1:B1,A2)>0)+(A2=A1),B1+1,A2))
Thank you everyone for posting! Thanks to Bosco's original reply, I managed to fiddle around and find the answer myself. The biggest issue I was facing was getting a circular formula error, but by using Bosco's formula as a help column I was able to get it all worked out.
So, the solution holds 3 different columns: Original Data (column A), Help Column (column B) and Final Result (column C).
In the Help Column, I did as suggested, in cell B2 and copied down:
=IF(A2="","",IF((COUNTIF(B$1:B1,A2)>0)+(A2=A1),B1+1,A2))
This would occassionally create results over 100, so in the Final Column I placed the following array formula, which incidentally also accounts for lists longer than 100 entries, in C2 and copied down:
=IF(B2="","",IF(ROW(C2)>101,ROW(C2),IF(AND(B2>=100,B3=""),100,IF($B2:$B$1000>=C3,C3-1,B2))))
As an array formula, this needs to be entered using Ctrl+Shift+Enter.
Thank you very much to everyone who posted replies, I'm sorry for my lack of clarity!

Find first date that contains a specific year

I'm trying to build a MATCH formula (or a similar one) that returns the position of the first value (date) within a range that matches a specific year.
For example, if I have a list of dates, like this:
A
1 12-31-2014
2 11-30-2015
3 12-29-2016
4 12-30-2017
For the value 2016, it should return 3.
MATCH(2016,A:A,0) doesn't work, since it's not an exact match. I can't use any aux columns either nor modify the dates.
Any ideas?
Are you allowed to use array formulas? If so, it may be as simple as this:
=IFERROR(MATCH(2016,YEAR($A$1:$A$4),0),"No Match")
Remember to commit the answer using Ctrl+Shift+Enter, since it'll be an array.
You'll want to change the references within the YEAR function to match your range. By using 0 as your [match_type] parameter, you're going to get the first value in the list that has a year of 2016.
For real dates, try,
=aggregate(15, 6, row($1:$999)/(year(a$1:a$999)=2016), 1)
'return the date with index as,
=index(a:a, aggregate(15, 6, row($1:$999)/(year(a$1:a$999)=2016), 1))
If they are in ascending order you could use,
=MATCH(42369, A:A)+1
=index(a:a, MATCH(42369, A:A)+1)
For a list of strings that look like dates try,
=MATCH("*2016", A:A, 0)
=index(a:a, MATCH("*2016", A:A, 0))

SUMIFS within a date range

I am working on a excel file for my monthly budget. I am exporting my monthly transactions into a CSV file and then copying it over.
I have a tab for each month and all my categorizes that I am budgeting for. I then copy over the csv file to a tab called transactions in my budget workbook. Then I have a drop down list with all the categorizes from my monthly categories. Once I have categorized all my transactions that will total up on the corresponding budget sheet.
The issue I am having a hard time with is how do I create a specific equation that will recognize the month and then the specific category item. For an example "His - Income" I can easily use a sumif to get that information from a list, but how do I now separate further it for April only.
The data is organized on a tab by Date in column "A", description in column "B", and amount in "D". I am looking for an equation that will find "His - income" for 4/1/2015 to 4/30/2015.
You want to use a SUMIFS
SUMIFS(sum_range, criteria_range1, criteria1, [criteria_range2,criteria2])
With one IF for the category, and two more for the date range. I will look something like this:
=SUMIFS(D1:D50, B1:50,"=His - income",A1:A50,">=4/1/2015",A1:A50,"<=4/1/2015")
I would change all the hard coded ranges (e.g. D1:D50 to named ranges) as well as the dates. If you put the dates in a cell it will change the formula to something like this: BudgetDates,"<="&F$1 (notice the ampersand)
The SUMIFS function can use operators other than equals; equals is just the default. With 01-Apr-2015 in J1 (sometimes 04/01/2015 formatted a mmmm) and His - Income in K1, you could use one of the following.
=sumifs(D:D, B:B, K1, A:A, ">="&J1, A:A, "<"&edate(J1, 1))
=sumifs(D:D, B:B, K1, A:A, ">="&date(2015, 4, 1), A:A, "<"&date(2015, 5, 1))
=sumifs(D:D, B:B, "His - Income", A:A, ">=4/1/2015", A:A, "<5/1/2015")
Unless you want to use it as a visual reminder, the = for exact martch is unnecessary. Typically, the upper limit of the date range is less than one day higher in case the dates contain times as well. The EDATE function will raise or lower a date by a number of months the equal to the integer in its months parameter.
For all intents and purposes, there is no adverse effect when using full column references with SUMIFS.

Resources