Get closest date based on matching mutiple criteria in Excel - excel

I have two tabs as below in Excel:
Table 1:
Date Vehicle Number Planned Leave Time Actual Leave Time
8/7/2017 10001 8/7/17 4:59 AM
Table 2:
Date Vehicle Number Actual Leave Time
8/7/2017 10001 8/7/17 4:29 AM
8/7/2017 10001 8/7/17 5:59 AM
8/7/2017 10001 8/6/17 3:05 PM
8/7/2017 10003 8/6/17 2:05 PM
I want to get the actual leave time from table 2 based on matching the same date, same vehicle number between table 1 and table 2;
Also, as you can see, there are multiple records for a same vehicle and same date in table 2, so I think I have to compare the time (8/7/17 4:59 AM) to all the time in table 2 that on 8/7/2017 and for vehicle 10001, to get the closest time, which has the minimum abs difference between my planned time and active time.
So can anyone please tell me what formula I should use in Excel in order to achieve my purpose?
Thanks so much
Pudge

I turned your data tables into Tables and used structured references, so it doesn't really matter where your tables are located. In addition, the formulas and references will automatically update if you add rows to either table. Also, you can rename the tables to something meaningful, if you like.
The formula is an array formula so must be entered by holding down ctrl+shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula:
=INDEX(Table2[Actual Leave Time], MATCH(MIN(ABS(([#Date]=Table2[Date])*([#[Vehicle Number]]=Table2[Vehicle Number])*Table2[Actual Leave Time]-[#[Planned Leave Time]])),ABS(([#Date]=Table2[Date])*([#[Vehicle Number]]=Table2[Vehicle Number])*Table2[Actual Leave Time]-[#[Planned Leave Time]]),0))

Related

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!

How to reference one value to various cells, depending on a date?

Please have a look at the picture I attached, it'll make understanding my problem easier because it's hard to describe.
In the first table, I have capacity data for a product. The capacity changes by the date indicated in the column, i.e. from July 2017 the capacity would be 56, from December 2018 78, and from October 2019 99. The reason why I don't write down the capacity for every month is that I want to save columns.
In the second table, I have every month. I want to reference the correct capacity for each month, e.g. it would be 56 for every month until December 2018.
I have been considering an =INDEX function, but it seems to complex for that. Is there a way to reference like this without using VBA? Would the VBA solution be simple? Or am I forced to write a column for every month's capacity in the first table? Thank you!
https://i.imgur.com/mRoBtTo.png
You can simply use several IF statements to compare the month in your column with the months given in your first table, and put the value of the correponding month.
Let's admit your first row is 1 and first column is A, it should give something like:
= IF(D7>=$F$2; $F$3; IF(D7 >= $E$2; $E$3; IF(D7 >= $D$2; $D$3; "")))
I dont see you columns and rows so i hope you will change them correctly on this formula:
=HLOOKUP(C111,$C$106:$P$107,2,TRUE)
C111 is the cell above your red row.
$C$106:$P$107 is the tableof capacities, i know it is bigger then the current one so you see you can add more columns.
2 is the row number from the capacity table.
true is becouse you dont want it to be the exact value it will take the previews in hte order of items
Both previous answers work perfectly, but I would go this way-
You don't actually need an if to find the previous capacity. you can simply use the approximate match (similar to the hlookup answer) in an index formula
=+INDEX($B$4:$E$5,MATCH($B$9,$B$4:$B$5,0),MATCH(C9,$B$4:$E$4,1))
The product $B$9 matches exact (0), but the date C9 is bigger than or equal (1).
$B$4:$E$5 is the source of capacity and
C9:AF9 the date timeline
Final advantage would be that you can have several products to index, not only a single one.
Could you please try the below formula and provide feedback please?
=IF(AND(D8>=D2,D8<E2),"56",IF(AND(D8>=E2,D8<F2),"78",IF(D8>=F2,"99")))

Duplicate in Excel

I am having an issue with excel formula which I am unable to write. I have 2 columns Interest rate and Maturity. Through pivot I get the data year wise. Problem is that when there are multiple values for same year, pivot should keep value as blank and I also need to add a formula that checks if interest rate for particular year remains same like in below case of 2021. If that stands true then I need to populate value. If you provide me a code for VBA or formula then it will be a big help !!!
Int Rate Maturity
2.14% 2020
4.08% 2023
3.82% 2024
3.19% 2026
3.93% 2027
2.11% 2021
2.11% 2021
2.79% 2019
2.99% 2023
This is how I solved it:
Start with a simple COUNTIF to count whether a year is duplicate or not.
Then create a (hidden) column that concatenates the year and its interest rate by using CONCAT.
Now perform the same COUNTIF on the column containing the concatenated data. By testing this column, you are testing the uniqueness of a combination "year-interest rate".
Use a simple IF. If the number you obtained in (1) is equal to the number obtained in (3) it means the year/interest rate combination is valid. If the numbers are not equal, it means there are at least 2 conflicting entries.

Formatting Existing Excel Cells to Time Format Without Date

I'm working on an excel 2010 sheet where I mark down the date and time an event happens. The date is in one column, and auto formats to 17-Nov when I would type in 11-17 (I was fine with this). The time is in a separate column.
I am trying to find the average time an event occurred, without regard to the date, so I would use =AVERAGE(C1:C10). However, I only receive a date back (like 17-APR).
I did not format the cells before I began to enter in data, and I would simply type in a 3:27pm event as 1527, and no reformatting would happen.
Now, when I attempt to reformat the column to hhmm, all the numbers entered so far turn to 0000. When I try to edit the 0000, it is formatted as 6/13/1906 12:00:00 AM.
What I want to do is have the time formatted as hhmm and not include a date in the cell, and be able to run formulas on it, such as the average time an even occurred.
Summary:
*Currently time is entered simply as ####. I entered 3:27pm as 1527.
*Trying to reformat the time column results in 0000 in all cells in the column that previously had a ####.
*Modifying the 0000 displays as 6/13/1906 12:00:00 AM
*I want to format the time as hhmm so I can simply type in 2357, and have it display as 2357, but understand I mean 11:57pm, and let me take averages.
*Hell, even being able to enter 1547 and have it auto format to 15:47 or 3:47p would be great.
Thanks for reading my question!
An easy way to apply an autoformat (though Excel won't see it as a true "Time") is to go into Format Cells>Custom> and use ##":"##. This will turn 1245 into 12:45. Mind you, this will be a text string so if you copy it to another cell and then apply a time, it will show as 12:00:00. Excel will also not be able to run formulas on it, but it's a quick and dirty way to make it look pretty.
Another option is to have a formula such as =TIME(LEFT(A1,2),RIGHT(A1,2),) where A1 would be replaced with the cell you are actually referencing. This will convert the number to a time that Excel will recognize as a time allowing you to run other functions on it, but requires another column.
If you are entering the times as 4-digit numbers, you'll need to do a calculation to get the hours and minutes, then use the TIME function to get an actual time:-
=TIME(A1/100,MOD(A1,100),0)
Another way is
=LEFT(A1,2)/24+RIGHT(A1,2)/1440
but then you have to format the result as a time.
Excel sees a number like 1547 as approximately 4 years on from 1st January 1900 if you format it as a date, so it will come out as something like 26/3/1904 in UK format or 3/26/1904 in US-style format.
Note that the time function can only give you values up to 23:59:59 (stored as 0.999988426), but the second method will give you a datetime value with one or more days as the whole number part. This can be useful if you want to do calculations on times spanning more than one day.
The above behaviour is because dates and times are stored as real numbers with the whole number part representing days and the decimal part representing fractions of a day (i.e. times). In spite of misleading information from Microsoft here, dates actually start from 31/12/1899 (written as 0/1/1900) with serial number 0 and increment by 1 per day from then on.

Excel - calculate date time

I am trying to calculate the hour difference between two times. What I've been doing now only gives me hour indications like 1:30, 2:45, etc but I can't make diagrams based on these values. If I get 2:30 as the hour difference, it should become 2,5. if the difference is 2:45 that should be 2,75.
Change your formula to:
(B2-A1)*24
and format as General
You should just be able to subtract 1 datetime from the other. Try this:
In cell A1, enter "09/17/2012 10:00" (Excel should automatically recognize this as a date)
In cell A2, enter "09/17/2012 11:30"
In cell A3, "=(A2-A1)*24". The result is 1.5.
The problem may be that you are trying to subtract 1 'time' from another 'time' without a date component. In that case, Excel may not recognize your value as a 'time'. Try adding a dummy date to the beginning of the time.
One limitation of this is that you will get an error response of "########" if the 2nd date is earlier than the 1st (because the result is negative). If this is an issue, try "=ABS(A2-A1)*24" instead.

Resources