Computing most recent smaller value - excel

I have an excel sheet with dates (sorted) in one column and values in another. Ex:
1/1/2019 10
1/2/2019 12
1/3/2019 8
1/4/2019 20
1/10/2019 8
1/12/2019 22
I want to compute in a third column, the most recent date such that value was less than or equal to the current value (if the current is the lowest, then use the current date). So, for the sample data above,
1/1/2019 10 1/1/2019
1/2/2019 12 1/1/2019
1/3/2019 8 1/3/2019
1/4/2019 20 1/3/2019
1/10/2019 8 1/3/2019
1/12/2019 22 1/10/2019
Is there a way of accomplishing this without VBA macros?

Here's a way. Paste these in and copy down the column.
Column C: =IF(COUNTIF(B2:B6,D1)=0,A1,MINIFS(A2:A6,B2:B6,D1))
Column D: =CONCATENATE("<",TEXT(VALUE(B1),"#"))
You can hide column D to make it prettier. It's the criteria being used by the COUNTIF and MINIFS. Column C is the output.
1/1/2019 10 1/3/2019 <10
1/2/2019 12 1/3/2019 <12
1/3/2019 8 1/3/2019 <8
1/4/2019 20 1/10/2019 <20
1/10/2019 8 1/10/2019 <8
1/12/2019 22 1/12/2019 <22
Formula view:
43466 10 =IF(COUNTIF(B2:B6,D1)=0,A1,MINIFS(A2:A6,B2:B6,D1)) =CONCATENATE("<",TEXT(VALUE(B1),"#"))
43467 12 =IF(COUNTIF(B3:B7,D2)=0,A2,MINIFS(A3:A7,B3:B7,D2)) =CONCATENATE("<",TEXT(VALUE(B2),"#"))
43468 8 =IF(COUNTIF(B4:B8,D3)=0,A3,MINIFS(A4:A8,B4:B8,D3)) =CONCATENATE("<",TEXT(VALUE(B3),"#"))
43469 20 =IF(COUNTIF(B5:B9,D4)=0,A4,MINIFS(A5:A9,B5:B9,D4)) =CONCATENATE("<",TEXT(VALUE(B4),"#"))
43475 8 =IF(COUNTIF(B6:B10,D5)=0,A5,MINIFS(A6:A10,B6:B10,D5)) =CONCATENATE("<",TEXT(VALUE(B5),"#"))
43477 22 =IF(COUNTIF(B7:B11,D6)=0,A6,MINIFS(A7:A11,B7:B11,D6)) =CONCATENATE("<",TEXT(VALUE(B6),"#"))
This is a little sloppy in that you could use a named value or absolute value for the end of the range, e.g. B$6. Otherwise you're going to be looking at cells below your table, which is fine as long as they're empty, but kind of sloppy.
Column C: =IF(COUNTIF(B2:B$6,D1)=0,A1,MINIFS(A2:A$6,B2:B$6,D1))

Related

How to select a set of values in pandas data frame (multiple colums with multiple row conditions)

I have a huge ass csv file like given below which I opened as dataframe using pandas. I want to extract data from multiple columns at different date sets.
I want to select from a particular date and hour to another for the last 3 column values. The slicing options I tried and googled were for single column.
date heure PM10 NO2 O3
0 01/01/2016 1 27 22 36
1 01/01/2016 2 25 29 27
2 01/01/2016 3 26 47 10
3 01/01/2016 4 16 40 13
4 01/01/2016 5 15 34 13
5 02/01/2016 1 15 34 13
6 02/01/2016 2 15 34 13
Target output - taking data from a particular data and hour to another one.
3 01/01/2016 4 16
4 01/01/2016 5 15
Thank you. The data set is obviously way bigger than 4 No.
You can do this:
df_selected = df[(df.date >= "01/01/2016") &
(df['hour']>=4) &
(df.date < "02/01/2016") &
(df['hour']<6)
].iloc[:,:3] #first three columns
Alternatively, for the columns selection you can use .loc[:,['name', 'of', 'columns']] or for the last n columns .iloc[:,-n:].
Be careful with date because I'm not sure what happens with an "English" date, maybe you have to change the date using df['date'] = pd.to_datetime(df.date).

Difference between the last and the first item of a criteria in a list

I have the following Excel spreadsheet
A B C D
1 Product ID Time of Event
2 27152 01.04.2017 08:45:00 27152 70 Min.
3 27152 01.04.2017 09:00:00 29297 108 Min.
4 27152 01.04.2017 09:55:00 28802 28 Min.
5 29297 02.04.2017 11:02:00
6 29297 02.04.2017 12:50:00
7 28802 18.04.2017 11:48:00
8 28802 18.04.2017 12:00:00
9 28802 18.04.2017 12:13:00
10 28802 18.04.2017 12:16:00
In Column A you can find different Product IDs.
In Column B the time when an event happens in the Product ID.
Each event is listed in the table; therefore, a ProductID can appear
several times in Column A.
In Column D I want to show now the difference in minutes between
the first and the last event which happens in a product ID.
D2 = 9:55:00 - 8:45:00 = 70 Min.
D3 = 12:50:00 - 11:02:00 = 108 Min.
D4 = 12:16:00 - 11:48:00 = 28 Min.
Therefore, I would need something like a DIFFERENCE-IF-Formula.
One of my ideas so far was going by the LARGE and SMALL function.
=LARGE(B2:B4;1)-SMALL(B2:B4;1)
However, this way I would have to find each array (B2:B4, B5:B6, B7:B10) seperatly; therefore, I would prefer to have the productID as a criteria in the formula.
Summarized:
Do you have any idea how I could calculate the difference in minutes between the last and the first event of a certain ProdcutID in the list?
I would prefer to avoid any kind of array formula.
=ROUND(MMULT(AGGREGATE({14,15},6,B$2:B$10/(A$2:A$10=C2),1),{1;-1})*1440,1)&" Min"
and copied down.
I've a feeling the separators for horizontal and vertical arrays in German versions of Excel are the period (.) and semicolon (;) respectively, so I believe you'll need:
=RUNDEN(MMULT(AGGREGAT({14.15};6;B$2:B$10/(A$2:A$10=C2);1);{1;-1})*1440;1)&" Min"
though please let me know if that doesn't give the required results.
Regards
With some conditions,
1. assuming that you convert column B into 2 columns
2. times is in ascending order
A B C
Product ID Time of Event TIMES
27152 01.04.2017 8:45:00
27152 01.04.2017 9:00:00
27152 01.04.2017 9:55:00
29297 02.04.2017 11:02:00
29297 02.04.2017 12:50:00
28802 18.04.2017 11:48:00
28802 18.04.2017 12:00:00
28802 18.04.2017 12:13:00
28802 18.04.2017 12:16:00
This will work without using array
=(INDEX($C$2:$C$10,SUMPRODUCT(MAX(ROW($A$2:$A$10)*(D2=$A$2:$A$10))-1))-INDEX($C$2:$C$10,MATCH(D2,$A$2:$A$10,0)))*1440
Convert time into minutes
=(time*1440)
Look for first matching value
=INDEX($C$2:$C$10,MATCH(D2,$A$2:$A$10,0))
Look for last matching value
=INDEX($C$2:$C$10,SUMPRODUCT(MAX(ROW($A$2:$A$10)*(D2=$A$2:$A$10))-1)
NOTE If last value is SMALLER then first value, you will receive an error.

Error in COUNTIFS function

I want to count how many vehicles are delayed more than 4 min on a given day according to a given departure (let's assume from 00:00 to 05:00).
This is a sample of the data:
A B C D
1 Line Day Departure Delayed (sec)
2 11 Weekday 02:30:00 120
3 11 Weekday 03:40:00 500
4 22 Weekday 01:45:00 10
5 44 Weekday 06:44:00 1000
6 55 Weekday 04:35:00 145
7 111 Saturday 14:40:00 450
8 111 Saturday 04:20:00 300
9 111 Saturday 20:20:00 220
10 111 Saturday 07:00:00 125
11 333 Sunday 09:15:00 700
I used a "TÆL.HVISER" function (Danish) or COUNT.IFS function to count the vehicles:
=TÆL.HVISER(A2:A11;"11";B2:B11;"Weekday";C2:C11;00:00:00>C2:C11>05:00:00;D2:D11;">240")
But it is not working. When I break this restriction into four restrictions, the individual restrictions are working but when I combine them it's not working.
I've laid out your data according to how I read your sample formula.
    
The EN-US formula in G4 is,
=COUNTIFS($A$2:$A$11, G$3, $B$2:$B$11, $F4, $C$2:$C$11, ">="&TIME(0, 0, 0), $C$2:$C$11, "<="&TIME(5, 0, 0), $D$2:$D$11, ">="&240)
Fill both right and down. I've use the TIME function so that a) real times could be referenced and b) it makes it easier to set to new values.
TÆL.HVISER, funktionen
Funktionen TID
It is the part
00:00:00>C2:C11>05:00:00
if you change it to two criteria like this
C2:C11;">00:00:00";C2:C11;"<05:00:00"
it will work. Here is the full formula:
=COUNTIFS(A2:A11;"11";B2:B11;"Weekday";C2:C11;">00:00:00";C2:C11;"<05:00:00";D2:D11;">240")

Excel table formulas to return blank

I have a table in excel with 2 columns, the E column is the running total the D column is the input value so normally it would be = E15+D16 however, i want the E column to return a blank if nothing is entered in the D column- what formula do I need?
1 Nov-23 Nov-30 1,230 1,230
2 Dec-01 Dec-07 130 1,360
3 Dec-08 Dec-14 416 1,776
4 Dec-15 Dec-21 124 1,900
5 Dec-22 Dec-28 102 2,002
6 Dec-29 Jan-04 83 2,085
7 Jan-05 Jan-11 95 2,180
8 Jan-12 Jan-18 88 2,268
9 Jan-19 Jan-25 102 2,370
10 Jan-26 Feb-01 130 2,500
11 Feb-02 Feb-08 311 2,811
12 Feb-09 Feb-15
13
14
15
16
17
18
19
20
21
22
=if(D16="","",E15+D16)
You dont even need the ISBLANK, just use empty quotes.
ISBLANK is what you're looking for.
In E16 =IF(ISBLANK(D16), "", E15+D16)
Use If and IsBlank in conjunction.
Do a check to see if the cell is blank, then depending on the result do the sum or the return blank. I don't have excel in front of me atm, but it should go something like this:
=if(ISBLANK(D16), "", E15+D16)
You can drag this down starting from the second cell in the column where this running total lives. The first one is obviously just the value from the adjacent cell.

Sum number according to date and name in excel

To sum the third column (numbers o companies) I've used this
=SUM(1/COUNTIF(Names;Names))
Names is name of array in C column and CTRL+SHIFT+ENTER and it works perfectly.
Now I'd like to sum earnings but only for each company once and with the latest data. For example, the result shoud be like this
=C4+C6+C7+C8+C9+C10
(93)
Thanks
A B C D
1 # company earnings date
2 1 ISB 12 10/11/2011
3 2 DTN 15 11/11/2011
4 3 ABC 13 12/11/2011
5 4 ISB 17 13/11/2011
6 5 RTV 18 14/11/2011
7 6 DTN 22 15/11/2011
8 7 PVS 11 16/11/2011
9 8 ISB 19 17/11/2011
10 9 ANH 10 18/11/2011
Sum 6 93
Assuming ascending dates, you could try with CTRL+SHIFT+ENTER in C11:
=SUM((MAX(A2:A10)-MATCH(B2:B10,LOOKUP(MAX(A2:A10)-A2:A10,A2:A10-1,B2:B10),0)=A2:A10-1)*C2:C10)
I'd suggest using a helper column as the easiest approach. In E2 use this formula
=IF(COUNTIF(B2:B$1000,B2)=1,C2,"")
and copy down the column. Now sum column D for the required answer.
Note that the above formula assumes 1000 rows of data maximum, increase if required.

Resources