Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

use google spreadsheet to analyze # of stops each day

Posted on 2013-11-13
7
377 Views
Last Modified: 2015-03-02
I have a google spreadsheet that contains a list of id#s and date/times.  For each id#, for each date, Id like to count the # of entries.  For example, id 3496978724, starting in row 2, has 4 entries for 10/1/2013.  After we count the # of entries an ID has for the day, the following rules should be applied.  If the # of entries is:

1
This should be considered "One Way."  All records for that id for the day should be classified as "One Way"

2
If the difference between the two times is less than 2.5 hours, all records for that id for the day should be classified as "One Way"

If the difference between the two times is greater than 2.5 hours, all records for that id for the day should be classified as "Round Trip"

3
If the difference between any two times is more than 2.5 hours apart, all records for that id for the day should be classified as "Round Trip"

Otherwise, all records for that id for the day should be classified as "One Way"

4
If the difference between any two times is more than 2.5 hours apart, all records for that id for the day should be classified as "Round Trip"

Otherwise, all records for that id for the day should be classified as "One Way"

5 or greater
If the difference between any two times is more than 2.5 hours apart, all records for that id for the day should be classified as "Round Trip"

Otherwise, all records for that id for the day should be classified as "One Way"

In the end
Id like a list of each id/date combination, along with whether that corresponding date was roundtrip or one way.


https://docs.google.com/spreadsheet/ccc?key=0Am5eEooQEL63dGc3NksyM3JhRTl0QVQwclAtSXdoUmc&usp=sharing
0
Comment
Question by:colonialiu20
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 39650792
I guess 2, 3, 4 and >=5 are all the same logic, so they can be combined as following:

1: Always output "One Way"
>=2: If the difference of any two times are larger than 2.5 hours, then output "Round Trip", otherwise "One Way"

I assumed you already grouped the student ID together and sort the data in the order of date within the same student ID. In this case, you may try to create a script (Tools -> Script Editor) and add a function like this:

function setOneWayOrRoundTrip() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var count = 0;
  
  for (var i = 1; i <= numRows - 1; i++) {
    var row = values[i];
    var studentID = row[0];
    var time = row[1];
    var date = row[2];
    var prevRow = values[i-1];
    var prevStudentID = prevRow[0];
    var prevDate = prevRow[2];
    
    if (studentID != prevStudentID || date != prevDate) {
      if (i > 1) {
        if (count == 1) {
          sheet.getRange("D" + i).setValue("One Way");
        }
        else {
          var isOneWay = true;
          for (var j = i; j > i-count; j--) {
            var time1 = sheet.getRange("B" + j).getValue();
            var time2 = sheet.getRange("B" + (j-1)).getValue();
            if (time1 - time2 > 60*1000*60*2.5) {
              isOneWay = false;
              break;
            }
          }
          for (var j = i; j>i-count; j--)          
            sheet.getRange("D" + j).setValue(isOneWay?"One Way":"Round Trip");
        }
      }
      count = 1;
    }
    else 
      count++;
  }
  if (count == 1) {
    sheet.getRange("D" + numRows).setValue("One Way");
  }
  else {
     var isOneWay = true;
     for (var j = numRows; j > numRows-count; j--) {
       var time1 = sheet.getRange("B" + j).getValue();
       var time2 = sheet.getRange("B" + (j-1)).getValue();
       if (time1 - time2 > 2.5/24.0) {
         isOneWay = false;
         break;
       }
     }
     for (var j = numRows; j>numRows-count; j--)          
        sheet.getRange("D" + j).setValue(isOneWay?"One Way":"Round Trip");
  }
};

Open in new window


Since I found my timezone is different from the data, so I added a column C to get the date as shown in column B by a formula "=TEXT(B?, "YYYY/MM/DD")" for each row.

A sample is referenced below:
https://docs.google.com/spreadsheet/ccc?key=0AoYcUhd_FqORdEZDLWcwZmVWeUFRVEdQODBsUE52RlE&usp=sharing

Please try. Thanks.
0
 

Author Comment

by:colonialiu20
ID: 39651647
Thank you!!!  Is there a way to now pull a record of every unique id #, date, combination, and pull wether it was a round trip or one way.  Similar to what is in f2:h7.

Thank you again
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 39652758
A simple way, I dragged column B to column D such that column B is now the date only and C is the result.

Then at cell F2, I add the following formula "=UNIQUE(A2:C)". After entering, it will show the unique results in F:H automatically.

The reference spreadsheet is modified for your reference.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:colonialiu20
ID: 39656867
Thank you.  There appears to be a problem with the script that i cant figure out.  Take a look at my spreadsheet, https://docs.google.com/spreadsheet/ccc?key=0Am5eEooQEL63dGdTT0pmclZ0QjVkeVZhWFFvZER6cXc&usp=sharing

If the last two records are:

456      11/19/2013            11/19/2013 8:18:00
456      11/19/2013            11/19/2013 8:20:00

The script will calculate it as a round trip.  This only happens on the last records.

If i put this as the last record:
999      11/20/2013            11/20/2013 8:00:00

it will calculate the student 456 record above correctly as one way as well as calculate 999 as a one way trip.  There just seems to be a problem when the last records are for round trip.  Any thoughts?
0
 

Author Comment

by:colonialiu20
ID: 39657749
In addition to the above problem, the script keeps on timing out after a few hundred rows.  is there any way to extend the timeout time.  If not, is there a way to modify that script for excel?
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 500 total points
ID: 39658713
1) Sorry I made a mistake.
2) I have revised and split the function into separate pieces, but I still have no idea how I can  run all using trigger to prevent timeout. The timeout default is ~5 minutes which cannot be modified.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many add-ons for Google docs, slides and sheets that can be extremely helpful in keeping your files organized and simple to use. Add-ons are installed into your Google docs, slides or sheets through Google's Add-On Store. One of my favorit…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question