Sorting alphanumeric dates in Excel - excel

I have a data set were a column represents the expiry date of a product in the following format: 1M, 2M, 1Y, 2Y, 1W, 2W, 1D, 2D. I would like to sort my data based on these dates in Excel, but I'm doing it in a very inefficient way, by expressing everything in days and then sorting the data according to the Days_column I create. Is there a better way to deal with this kind of data?

I think I have this understood. Here is a picture of the final result:
I found some good information here: https://support.office.com/en-us/article/Add-or-subtract-dates-b83768f5-f695-4311-98b1-757345f7e926#bm6
I put this in D2 and copied it down: =LEFT(C2,LEN(C2)-1). In cell E2 I put =RIGHT(C2,1), and copied it all the way down. In F2 I have =IF(E2="Y",DATE(YEAR(A2)+D2,MONTH(A2),DAY(A2)),IF(E2="M",EDATE(A2,D2),IF(E2="D",A2+D2,IF(E2="W",A2+(D2*7),"x")))).
Notes: Adding months is based on the number of days in the upcoming months, and not always equal to 30. Notice that 1 month added to 1/31/2016 puts you at 2/29/2016. Adding the year works the same way, you don't have to worry about leap years messing you up.
For weeks I used the number of weeks multiplied by 7, and then just added those days to the Buying date.

You could split your expiry into two columns - Expiry_number i.e. the duration or numeric part of the expiry, and Expiry_unit i.e. M, D, Y. You can split this using functions, for example your numeric column:
=LEFT(D3, LEN(D3)-1)
where D3 is the expiry. And then your units column:
=Right(D3, 1)
Then you can sort the the data on two levels - alphabetically on the Expiry_unit (as chance would have it the durations units increase in length alphabetically D, M, Y) and then numerically on Expiry_number.

Related

How can I (easily) format long intervals (over 30 days, but including time components)

I am using a spreadsheet to record the intervals between certain medical events. Each event's timestamp is recorded in one column, so the interval between each event is the difference between consecutive cells (and the time since the last event uses now()).
First problem: I want to display the interval in days, hours and minutes. None of the built-in formats will do this, they report the days remaining after discarding complete months. So I am using this expression:
TEXT(TRUNC(C2-C1),"0") & " days " & TEXT(MOD(C2-C1,1),"hh "" hrs "" mm ""mins""")
which (e.g.) shows "46 days 13 hrs 44 mins". I was hoping there was a way to format a date/time value to show this rather than making the cell a string value, but I haven't been able to find one.
Second problem: I want to display the average value of all completed intervals in the same format. Because I can't average the string values produced by the previous expression I need to average the numeric equivalent (which I'd prefer not to have visible in the sheet) and then convert it to a string as for a single interval.
I can probably do this with a similar approach (if I don't run out of characters to enter the formula) but it seems to me that there must be a better way.
Ideally there is a solution which will work in Excel 2010. Has anyone solved a similar problem before and can give me some pointers?
Thanks, T
Edit: Some data to show what I am working with (I hope the image is readable). Here's a few lines from the sheet.
The formula for H2 etc. is
=IF(G2="c",NOW()-C2,"")
I5 is calculated as the difference between C5 and Cprev (where prev is chosen so that D5 and Dprev are both set). Obvious extension of this for J and K.
M2, M3 and M4 are respectively
=AVERAGEIF(K5:(INDIRECT("K"&(ROWS(K:K)))),"<>0")
=AVERAGEIF(J5:(INDIRECT("J"&(ROWS(J:J)))),"<>0")
=AVERAGEIF(I5:(INDIRECT("I"&(ROWS(I:I)))),"<>0")
Now, I can use a custom format for the values in H, I and J, and for M3 and M4, because these values will never exceed a few days. But values in K and M2 will be somewhere around 100 so I can't just format the raw value.
With custom formatting applied:
Here K6 and M2 say "27 days.." not "87 days..". That's what I'd like to fix nicely, hopefully without populating additional cells or writing a 3gl function to do it.
Date_Times in Excel are stored as days (with the decimal part representing parts of a day).
If the date matters, rather than just a number of days, then day zero is 1899/12/31.
Your first thought was right - do with formatting, not by turning a number into a string.
Entering date into A and time into B, with C=A+B is a good start, so that you can E.g. subtract one point in time from another without having to do anything about straddling midnight, month-ends etc, calculate averages etc.
Consolidating the comments already here: per https://support.microsoft.com/en-us/office/format-numbers-as-dates-or-times-418bd3fe-0577-47c8-8caa-b4d30c528309 you cannot get d for days above 31 (and it won't accept a format 0 hh:mm:ss )
I suggest that you do all your calculations using just numbers to get to say column M, and in N2 put =M2 etc, so you have the same values twice.
Then for formatting, use Format Cells | Number | Custom.
In column M put 0 "days".
In column N put hh "hours" mm "minutes".

Excel formula to split an amount per year depending on expenditure days within a date range

I am in the process of building a formula to split a total cost (in column J) based on start and end expenditure periods that can vary from 2021 to 2031. Based on the days between the expenditure period dates (column M), I managed to work out to split the cost using the formulas below up to 2023 but it is not consistent and at times incorrect.
In cell P5 I have the following formula. For year 2021, I seem to get the correct split result.
=IF($K5>AS5,0,$J5/$M5*(AS5-$K5))
In cell Q5, I have the following formula. For year 2022, I seem to get the correct spit as well
=MIN(IF(SUM($N5:P5)>=$J5,0,IF($L5>=AS5,$J5/$M5*(AS5-AR5),$J5/$M5*($L5-MAX(AR5,$K5)))),K5)
However, I don't get the right result in cell Q6 which has the same formula but different dates
=MIN(IF(SUM($N6:P6)>=$J6,0,IF($L6>=AS6,$J6/$M6*(AS6-AR6),$J6/$M6*($L6-MAX(AR6,$K6)))),K6)
Cell R6 shouldn't return any result because it is out of date range. This is where things get mixed up.
Note that from column AR to BC, it is all year end dates from 2020 to 2031 as shown below.
Is there a better way to tackle this sort of formula as I seem to get dragged into a long and unreliable way of doing this.
Here single function(♣) that will create a series of pro-rata multipliers (of appropriate length) for any given start/end date:
EDIT: see end of soln for extended version per OP comment to original soln...
SINGLE FUNCTION
=J11*(LET(dates,EDATE(DATE(YEAR($K11),1,1),12*(SEQUENCE(1,YEAR($L11)-YEAR($K11)+2,1))),IF(dates<K11,K11,IF(dates<L11,dates,L11)))-LET(dates,EDATE(DATE(YEAR($K11)-1,1,1),12*(SEQUENCE(1,YEAR($L11)-YEAR($K11)+2,1))),IF(dates<K11,K11,IF(dates<L11,dates,L11))))/(L11-K11)
It may appear somewhat unwieldy in length, but it is far more robust (and concise) compared to the combination of steps/series you have created. What's more, it returns the precise answer RE: pro-rata payments and is guarenteed to never over/under-run RE: total payment (by design).
BREAK-DOWN
Comprises 3 distinct parts (some of which are similar in pattern/formation):
1] First part - create a series (array) of years spanning start-end dates:
=LET(dates,EDATE(DATE(YEAR($K5)-1,1,1),12*(SEQUENCE(1,YEAR($L5)-YEAR($K5)+2,1))),IF(dates<K5,K5,IF(dates<L5,dates,L5)))
Thanks to the lovely Spill functionality the new Office 365 variant Excel boasts, you never have to worry about how many years are required -- so long as you have the space to the right of this workbook (would be unusual otherwise - assuming you start in column O and clear any content to the right of this, you'd need an end date beyond the year 2557 (26th century) to run out of columns! ☺
2] Second part is merely a replica of the firs series, albeit shifted to the right 'once' (so starts with the 2nd element in the 1st series):
=LET(dates,EDATE(DATE(YEAR($K5),1,1),12*(SEQUENCE(1,YEAR($L5)-YEAR($K5)+2,1))),IF(dates<K5,K5,IF(dates<L5,dates,L5)))
3] Third part - you have the basic ingredients from parts 1 and 2 to complete the required task easily: simply deduct series 2 from 1 (giving days between successive dates in series 1 - i.e. days for each year between start and end dates), divide by total days (to yield pro-rata multipliers), and then multiply these by the total £amount and voila - you have your series!
=J5*(O6#-O5#)/(M5)
♣ Caveat(s) - assuming you have Office 365 compatible version of Excel (which is quite common nowadays)
*EDIT - EXTENDED VERSION
Given above, the following extends this to align monetary results (1st table, o11:w21) within respective calendar period columns (spanning the entire period in question).
This soln:
Determines header row based upon the number of columns & corresponding calender periods (financial yrs commencing 1/1) as an array function (i.e. dynamic range)
Utilises a modified version of the eq. provided for dates arrage (refer: "First Part", original soln)
Comment - same caveats as before - i.e. Office 365 etc.
Screenshot(s)/here refers:
DATES (HEADER) - Y10 (array)
=LET(y_,MIN(K11:K21),x_,MAX(L11:L21),EDATE(DATE(YEAR(y_)-1,1,1),12*(SEQUENCE(1,YEAR(x_)-YEAR(y_)+2,1))))
Comment - enter once within single cell Y10 - i.e. as an array function with Spill to right
ALIGNED/SHIFTED FINANCIALS - Y11:Y21 (each cell in col is an array)
=IFERROR(IF(Y$10#<EDATE(K11,-12),"",IF(Y$10#>EDATE(L11,12),"",INDEX(O11#,1,MATCH(Y$10#,EDATE(DATE(YEAR($K11)-1,1,1),12*(SEQUENCE(1,YEAR($L11)-YEAR($K11)+2,1))),0)))),"")
Comment - enter this as an array fn. (#SPILL! to the right) in each cell within column Y (can drag this function down Y11:Y21 as required)

Get sum of cells containing "Vacation" except when related date matches certain day & month

oversimplified i have two columns: Date and Text; I want to check my current amount of vacation days based on the first date in row 2, so i came up with the following formula:
="Available vacation days: "&YEARFRAC(A2;TODAY())*12*(25/12)
I calculate the fraction of the year based on the first date and todays date, multiply it by 12 to get months and multiply it again by the total amount of vacation days in my contract per month. Now i got another formula to collect me all cells in column B containing "Vacation", pretty straight forward:
=COUNTIF(B:B;"Vacation")
Now the interesting part - i got the formula who gives me a boolean if a datetime matches the 24th or 31st of december:
=AND(OR(DAY(A53)=24;DAY(A53)=31);MONTH(A53)=12)
I want to count vacation days happening on a 24th or 31st of december as a half-vacation day (0.5), and otherwise fully (as a 1). Then i want to combine my first statement with this result and subtract the used vacation days. I read about VLOOKUP and XLOOKUP but am unsure if this fits this purpose. I want to avoid having an extra column with my boolean returns and rather have this one cell giving me all the information combined.
Without introducing another column, and using DAY and MONTH
It's nearly impossible, and just unnecessarily so...
Please reconsider this, what will happen if you want to add 4th of July as a holiday?
Your formula =AND(OR(DAY(A53)=24;DAY(A53)=31);MONTH(A53)=12) only works for 1 row at a time. So, we can't ever use it with a list, because you will get the whole list as a result every single time. You can't divide them into smaller lists and join them together, there is no such functionality without VBA.
In the future, do not set arbitrary constraints like "no additional columns", you can hide them if you don't like them. And if you don't need them, remove unnecessary rows like non-vacation rows. They are irrelevant, so why not separate the two.
Just to prove my point, here's the solution you wanted:
Solution
=COUNTIFS(B2:B9;"Vacation") - (COUNT(IFERROR(FILTER(FILTER(FILTER(A2:A9;B2:B9="Vacation");MONTH(FILTER(A2:A9;B2:B9="Vacation"))=12);DAY(FILTER(FILTER(A2:A9;B2:B9="Vacation");MONTH(FILTER(A2:A9;B2:B9="Vacation"))=12))=31);0))+(COUNT(IFERROR(FILTER(FILTER(FILTER(A2:A9;B2:B9="Vacation");MONTH(FILTER(A2:A9;B2:B9="Vacation"))=12);DAY(FILTER(FILTER(A2:A9;B2:B9="Vacation");MONTH(FILTER(A2:A9;B2:B9="Vacation"))=12))=24);0))))*0,5
It works, but it's a pain to read, use and maintain.
A2:A9 refers to the dates column
B2:B9 refers to the text column
So in the future, the last thing you want to do is set arbitrary constraints. Furthermore, why use functions like MONTH and DAY when we can just read the text? That way you could even create a table of holidays to search for instead. That will be no fun task with this setup. (Oh, and if it's because of the year, just strip it away from the text when you want to know only the month and day.
Best of luck!

Finding a shorter period of days within a longer period for calculation

I have a set of dates, in total 7 periods spanning 6 months each with corresponding calculation factor.
I will have user input of period for which they'd want the calculation to be done, which can fall within one of the 6 months periods or encompass between two or more such periods wholly or partially.
Illustration:
The preset periods:
User input:
I've obtained decimal value (monthly basis) of the periods input by the user for calculation. For first instance (see user input), the decimal value for the period 01-01-2015 to 29-04-2015 will be 3.97 on monthly basis. The calculation for that period would be like:
n*3.97*113%
For the second instance, the decimal value for the period 30-04-2015 to 30-06-2015 would be 2.03, which would be used to do calculation at 113% and then the result will be added to the calculation done at 119% using the decimal value 5.65 for the period 01-07-2015 to 20-12-2015:
(n*2.03*113%)+(n*5.65*119)
Think I can handle the breaking up of periods since the revision event is bi-annual on particular dates but advice regards to that is welcome. More importantly, I need help tracing the preset calculation factor (say 132%) corresponding to the period input by the user as illustrated above. Is it feasible?
I will use the standard approach for finding the overlap between two dates and will split the task into three parts as in my answer to this recent question.
(1) The first part is finding the overlap between the user's range of dates and one or more revision periods in whole months and will need an array formula. I have chosen to use the Datedif function 1 to get the difference in months between the beginning and end of the overlaps. If there is no overlap, the start date fed in to Datedif will be after the end date, and it will return an error which can be trapped by Iferror. If the user's dates start in A2 and B2, this gives in C2:
=SUM(IFERROR((DATEDIF(IF(K$2:K$8>A2,K$2:K$8,A2),IF(L$2:L$8<B2,L$2:L$8,B2),"m")+1)*M$2:M$8,0))
which has to be entered as an array formula using CtrlShiftEnter
The above result includes the first and last months entered by the user even if they are incomplete months. It's then necessary to subtract any missing days in the first and last months.
(2) Missing days in first month as a fraction of the number of days in that month in D2:
=SUMIFS($M$2:$M$8,$K$2:$K$8,"<="&A2,$L$2:$L$8,">="&A2)*(A2-EOMONTH(A2,-1)-1)/(EOMONTH(A2,0)-EOMONTH(A2,-1))
A noted by OP, this could also have been done using sumproduct, vlookup or index/match.
(3) Missing days in last month as a fraction of days in that month in E2:
=SUMIFS($M$2:$M$8,$K$2:$K$8,"<="&B2,$L$2:$L$8,">="&B2)*(EOMONTH(B2,0)-B2)/(EOMONTH(B2,0)-EOMONTH(B2,-1))
The total is just (1)-(2)-(3) or
=C2-D2-E2
I have put the results of OP's two examples for comparison in H2 and H3: my results agree with them in the first 3 significant figures.
n*3.97*113%
(n*2.03*113%)+(n*5.65*119)
In all cases I have set n=1 and ignored the fact that the rate is a percentage.
This shows how the results would be calculated manually:
1 Pros of using Datedif:
(1) Works across year boundaries unlike just using Month function.
(2) Works conveniently with Iferror to identify non-matching date ranges.
Cons of using Datedif:
(1) It is an undocumented function and may be withdrawn in future.
(2) In this particular case, all date calculations are within the same year so Month would be useable.

Excel AVERAGEIFS else statement

I'm trying to perform an AVERAGEIFS formula on some data, but there are 2 possible results and as far as I can tell AVERAGEIFS doesn't deal with that situation.
I basically want to have an ELSE inside it.
At the moment I have 2 ranges of data:
The first column only contains values 'M-T' and 'F' (Mon-Thurs and Fri).
The second column contains a time.
The times on the rows with an 'F' value in column 1 are an hour behind the rest.
I want to take an average of all the times, adjusting for the hour delay on Fridays.
So for example I want it to take an average of all the times, but subtract 1 hour from the values which are in a row with an 'F' value in it.
The way I've been doing it so far is by having 2 separate results for each day, then averaging them again for a final one:
=AVERAGEIFS(G3:G172, B3:B172, "M-T")
=AVERAGEIFS(G3:G172, B3:B172, "F")
I want to combine this into just one result.
The closest I can get is the following:
=AVERAGE(IF(B3:B172="M-T",G3:G172,((G3:G172)-1/24)))
But this doesn't produce the correct result.
Any advice?
Try this
=(SUMPRODUCT(G3:G172)-(COUNTIF(B3:B172,"=F")/24))/COUNTIF(B3:B172,"<>""""")
EDIT
Explaining various steps in the formula as per sample data in the snapshot.
SUMPRODUCT(G3:G17) sums up all the value from G3 to G17. It gives a
value of 4.635416667. This after formatting to [h]:mm gives a value
of 111.15
OP desires that Friday time be one hour less. So I have kept one hour less for Friday's in the sample data. Similar SUMPRODUCT on H3:H17 leads to a value of 4.510416667. This after formatting to [h]:mm gives a value
of 108.15. Which is exactly three hours less for three occurrences of Fridays in the sample data.
=COUNTIF(B3:B17,"=F") counts the occurrences of Friday's in the B3:B17 range which are 3 occurrences.Hence 3 hours have to less. These hours are to be represented in terms of 24 hours hence the Function COUNTIF() value is divided by 24. This gives 0.125. Same is the difference of 4.635416667 and 4.510416667 i.e. 0.125
Demonstration column H is for illustrative purposes only. Infact Friday accounted values that is 108.15 in sample data has to be divided by total data points to get the AVERAGE. The occurrences of data points are calculated by =COUNTIF(B3:B17,"<>""""") with a check for empty columns.
Thus 108:15 divided by 15 data points give 7:13 in the answer.
Revised EDIT Based upon suggestions by #Tom Sharpe
#TomSharpe has been kind enough to point the shortcomings in the method proposed by me. COUNTIF(B3:B172,"<>""""") gives too many values and is not advised. Instead of it COUNTA(B3:B172) or COUNT(G3:G172) are preferable. Better Formula to get AVERAGE as per his suggestion gives very accurate results and is revised to:
=AVERAGE(IF(B3:B172="M-T",G3:G172,((G3:G172)-1/24)))
This is an Array Formula. It has to be entered with CSE and further cell to be formatted as time.
If your column of M-T and F is named Day and your column of times is named TIME then:
=SUMPRODUCT(((Day="M-T")*TIME + (Day="F")*(TIME-1/24)))/COUNT(TIME)
One simple solution would be to create a separate column that maps the time column and performs the adjustment there. Then average this new column.
Is that an option?
Ended up just combining the two averageifs. No idea why I didn't just do that from the start:
=((AVERAGEIFS(G$3:G171, $B$3:$B171, "F")-1/24)+AVERAGEIFS(G$3:G171, $B$3:$B171, "M-T"))/2

Resources