How to mass copy rows depending on a counter in either SPSS or excel - excel

The problem I have is as such:
I have a database full of xrays and associated information for a long list of patients, ordered by patient ID. What I want to do is to find the time of the first xray performed for each patient, and I want to fill this in for each xray of these patients. The issue is that each patient often has multiple xrays, and I'm not sure how to copy the data from another column into my timeOfFirstXray column.
For example; (sorry for my very poor formatting)
ptid ...dateandtimeofxray .....dateandtimeof1stXRAY
4 .......21/1/2011 4:30 ............21/1/2011 4:30
4 .......22/2/2011 5:11 ...........21/1/2011 4:30
4 .......25/5/2011 5:00 ........... 21/1/2011 4:30
5 .......29/7/2000 3:00 ............ 29/7/2000 3:00
5 .........30/7/2000 4:00 ......... 29/7/2000 3:00
5 ........ 31/7/2000 5:00 ......... 29/7/2000 3:00
5 .........1/8/2000 1:00 ........... 29/7/2000 3:00
6 .........1/9/2002 2:00 ........... 1/9/2002 2:00
8 ........ 3/10/2008 3:23 ......... 3/10/2008 3:23
8 .........4/10/2008 5:00 ......... 3/10/2008 3:23
8 ........ 5/10/2008 9:00 ......... 3/10/2008 3:23
My psuedocode is:
Set a counter to 0
initialise date variable to first row's date value
foreach row,
if counter < patientID, counter++ until equal with id, set date variable to value of date in this row, and set this value to timeOfFirstXray for the row.
else if counter == patientID, copy existing value in date variable into timeOfFirstXray for this row
I've never used SPSS or excel for this type of stuff before, so basically what I'm looking for is the above translated.
Thankyou for reading!

Here is an SPSS option.
`do if (missing(lag(ptid)) or ptid ne lag(ptid)).
compute dateandtimeof1stxray = dateandtimeofxray.
end if.
leave dateandtimeof1stxray.`
This presumes that dateandtimeof1stxray does not exist in the input dataset.
HTH,
Jon Peck

Here's an Excel option. Assuming your data starts in row 2, enter this formula in C2 and copy down. Note that this is an array formula and must be entered with Ctrl-Shft-Enter. (If done correctly you'll see curly braces around the formula.) Then just copy down as far as needed:
=MIN(IF(($A$2:$A$12=A2)*($B$2:$B$12),$B$2:$B$12))
Adjust your ranges to fit you data. Note that it gets the earliest date even if they're not in chronological order in your source data.
If you are interested in the logic behind the formula, see this post on my blog.
If you wanted to just show the minimums, you could accomplish something similar to Andy W's comment above by using a pivot table in Excel (Excel 2010 shown here). The key is to "show the values as" Min:

Related

Using MOD with Time values to determine time differences that transition midnight

I created an Excel Spreadsheet with a Total Time for the Duration of the Shift ie 8:30-17:30.
Then I created this formula
=(MOD([#[Supposed Shift End]]-[#[Supposed Shift Start]],1))*24
with Format cells -> Number 2 decimal places. This gives me the [Total Supposed Shift Hours]
Giving me Duration of the Shift that needs to be covered.
Now I also created another column for the ACTUAL time the shift covered
=(MOD([#[Actual Time End]]-[#[Actual Time Start]],1))*24
This gives me the [Total Actual Time Hours]
For the Actual Time End and Actual Time Start When the employee DIDN'T show up we entered 0 for both cells with the same cell format Number -> 2 decimal places
The Problem:
This is the formula I wrote to subtract these 2 Columns
=(MOD([#[Total Supposed Shift Hours]]-[#[Total Actual Time Hours]],1))*24
Why when I subtract say the [Total Supposed Shift Hours] 9.00 - the [Total Actual Time Hours] 0.00 = 24.00 ???
9 - 0 = 9 not 24.... sigh
Will the formula be affected if the time goes from previous night 21:00 to 8:00 with the MOD formula?
Sample data (as requested)
Note: Nomenclature differs from description above: Open = Supposed and Covered = Actual
Service Date
Open Post Start
Open Post End
Total Hrs Missing
Covered Post Start
Covered Post End
Total Hrs Covered
Category
Hours Not Covered
02/06/2021
16:00
00:00
8
16:00
03:00
11
A
0
04/06/2021
16:00
00:00
8
00:00
00:00
0
A
0
10/21/2021
10:30
00:00
13.5
18:00
19:30
1.5
B
0
Initial Answer
A minor point first: You don't need to wrap the MOD function in brackets. As the function already produces a result to the *24. Thus the following works just fine:
=MOD([#[Supposed Time End]]-[#[Supposed Time Start]],1)*24
To your question: Your non-working formula reads as if it simply wants the difference (in hours) between Supposed and Actual. If that's so, simply do this:
=[#[Total Supposed Shift Hours]]-[#[Total Actual Shift Hours]]
EDIT: Using the (now posted) table, I've constructed what I think it is you're trying to doRefer to Sample results image below.
Formula in Colum E: =MOD([#[Open Post End]]-[#[Open Post Start]],1)*24
Formula in Colum H: =MOD([#[Covered Post End]]-[#[Covered Post Start]],1)*24
Formula in Colum J: =[#[Total Hours Missing]]-[#[Total Hours Covered]]
Sample results:
Now: If Column J (i.e. the response to your core question) isn't the result you're after, can you tell me what it is you would expect there (using actual expected values for each row).
Notes:
Your table column header Total Hours Missing is somewhat confusing.
But, I'm reading that a Post Duration (Duration of Shift in your original parlance).
If I understand what transpired correctly, the "spanner in the works" was #P.b suggestion to remove MOD. Revert your formulas in columns E and H as shown above.

Get Value of first Pair

I have this file
Date | Time | Table | Checkin Time | Bill Number ..| .. |
7/1/2018 **18:03:48** **6** **18:03:48** **4332**
7/1/2018 18:11:23 **6** **18:03:48** **4332**
7/1/2018 18:23:10 **6** **18:03:48** **4332**
7/1/2018 19:24:11 8 19:24:11 4333
7/1/2018 19:56:17 8 19:24:11 4333
7/1/2018 20:31:11 *6 20:31:11 4332*
I need to calculate Checkin Time which is 1st time of table number and bill number.
For example See one customer checkin at 18:03:48 with table number 6
His Bill number is 4332 .
So for table number 6 and Bill number 4332 chekin time will be 1st time
which is 18:03:48.
So I applied the formula:
=IF(AND(E3=E2,C3=C2,A3=A2),D2,MIN(OFFSET(E3,0,-3,MATCH(0,(E3:E$35470=E3)*(C3:C$35470=C3)*(A3:A$35470=A3),0)-1,1)))
But the value changes at 6th row .
It's giving 20:31:11 as checkin time .
It should be 18:03:48 because the 6th row table number and bill number is same as 1st one.
So How do I make it possible, to calculate the checkin time as per 1st time who have same bill and table number , no matter which row .
Here is the excel file I have uploaded to Google drive.
Here is the screenshot.
Edit:- Formula by #jeeped work, But as the log is of 30 days
The data keeps repeating the other day too.
Can it be done by giving some unique date value ?
If you don't have the newer MINIFS and MAXIFS, use AGGREGATE to achieve the same result.
=AGGREGATE(15, 7, ((A$2:A$7)+(B$2:B$7))/((C$2:C$7=C2)*(E$2:E$7=E2)), 1)
Format the result any way you want; as date and time, time only, etc.
You can make use of an array formula combining INDEX+MATCH to return the earliest time value that matches your conditions first. In cell D2, enter the following:
=INDEX($B$2:$B$7,MATCH(1,($C$2:$C$7=C2)*($E$2:$E$7=E2),0))
When returning, be sure to press CONTROL+SHIFT+ENTER instead of just ENTER to make sure the formula calculates correctly.
#Jeeped's answer is spot on, here is an adjustment for the formula to work over multiple days that have the same table numbers and bill numbers:
=AGGREGATE(15, 7, ((A$2:A$13)+(B$2:B$13))/((C$2:C$13=C2)*(E$2:E$13=E2)*(A$2:A$13=A2)), 1)
All I did was add this extra conditional argument, that checks if the "Date" is the same:
*(A$2:A$13=A2)

Excel SUMPRODUCT with ISNUMBER and TIMEVALUE and LEFT to meet criteria

In Cell D$3$:D$38$ I have
## Heading ##
Thu Oct-05
Off
Off
Off
Maternity
Off
Off
Off
Off
Off
Off
VACATION
04:00-16:00
04:00-14:00
05:00-14:00
05:00-14:00
06:00-16:00
06:00-15:00
07:00-17:00
07:30-16:30
07:30-16:30
08:00-17:00
08:00-17:00
08:00-18:00
08:00-17:00
08:00-18:00
09:00-18:00
09:30-18:30
10:00-19:00
10:00-19:00
10:30-19:30
10:30-19:30
11:00-20:00
12:00-22:00
13:00-22:00
13:00-22:00
15:00-22:00
I used the formula
=SUMPRODUCT(--(ISNUMBER(TIMEVALUE(LEFT($D$3:$D$38,5)))))
to successfully obtain the result of 25 cells with number in the specified range but when I try to add a criteria like:
>=ISNUMBER(TIMEVALUE(B44)) and B44 is 4:00 am and the second criteria <=ISNUMBER(TIMEVALUE(c44)) B44 is 5:00 am
I'm not able to get the result I should that's: 2
Do you know why it is not working the way it should?
Thank You
You can change the formula to this:
=SUMPRODUCT(--(ISNUMBER(TIMEVALUE(LEFT(TEXT($D$3:$D$50,"hh:mm:ss"),5)))))
The reason it doesn't recognize 4:00 am or 5:00 am is due to TIMEVALUE evaluate time text and therefore, you need to convert it to time text first.

Adding/Subtracting Whole numbers from Time

I have tried every which way to format cells to subtract the result from time for instance the formula in the cell = 11(this is 11 minutes) I want to take that result minus 8:00:00 to give me 7:49:00 but it doesn't work the result is ####### no matter how big I make the cell. And if I format the cells with the formula to custom [m]:ss then the value changes.
Sample of the Worksheet:
I want Y2 = X3-W3 in a time format.
So, if A1=11
Then in some other cell, (B1 in this example): =TIME(,A1,)
Then subtract from the cell with 8:00:00. (If it's C1...:)
=C1-B1
That will give you the time you want.
Info: The main thing is that you have to tell Excel that your cell with the "11" in it, is minutes. By using the =TIME(,A1,) you will get the value of: 12:11 am. (If you keep it in Date format.) 12:11 am could also be viewed as: 0 Hours, 11 minutes, 0 seconds. And now that it knows, you should be able to subtract.
Try this:
=TIME(HOUR(X3),MINUTE(X3)-W3,SECOND(X3))
The ######### is because you have a negative time. Becuase Excel reads time as decimals of 24 hours, 8:00:00 is .3333 and if you subtract 11 from that you get -10.6666 and date/time can not be negative.

Excel date values compute incorrectly

I am trying to create an excel sheet where each line contains a date/time field for each 15 minute interval for the entire year. I put "00:15:00" in A1 and "1/1/2014" in A2 and "=A2+$A$1" in A3. I then copied A3 down enough times to go thru the year. I set column B equal to col A. I set the format in col A to Short Date and col B to Time. Then I converted everything to values. When I do that, all the entries for midnight are not right. For example the entry between Jan 1 and Jan 2, displays as 1/2/2014 and 12:00:00AM. But the actual value is "1/1/2014 12:00:00 AM". This value is incorrect, it is 24 hours off. What's going on here? How can I fix it?
I'm using Office 2010.
It's standard for 12:00 AM to denote the midnight that starts the day. So, in 15-minute increments, 1/1/14 11:45 PM would typically be followed by 1/2/14 12:00 AM, followed by 1/2/14 12:15 AM.
See: http://en.wikipedia.org/wiki/12-hour_clock#Confusion_at_noon_and_midnight

Resources