Using Date Questions With Irregular Time Periods

From Q
Jump to navigation Jump to search

Date questions in Q assume that you have regular time periods. That is, they assume that the data is one and only one of monthly, daily, quarterly, etc. You can work around this limitation as follows:

  1. Create a variable that contains a 1 for the first time period, a 2 for the second time period, a 3 for the third time period and so on. In the rest of this page, it is assumed that the Variable Name of this variable is timePeriods. If such a variable is not in your data file, the fastest way to create it will usually be as a JavaScript Variable. An easy way to do it is to:
    • Create filters for each time period.
    • In the Variables and Questions tab, if required, change the names of the variables to period1, period2, etc.
    • Insert a JavaScript Variable with Expression of period1 + 2 * period2 + 3 * period3 etc..
    • Change the Name and Label of the variable to timePeriods.
  2. Insert a new JavaScript Variable with:
    • Name: fakeDate
    • Label: Whatever you wish to appear the report. E.g., Wave.
    • Expression: Q.EncodeDate(timePeriods+4700, 1, 1)
  3. Set the Variable Type to Date.
  4. Press the Values button and check that the Aggregation is set to 1 and Year.
  5. Select a table in the Outputs Tab and select Automate > Custom Rule, paste in the code below.
  6. Modify the contents of the first line to list the names that you wish to give to the waves. Note that you will get an error in the next step if you make a mistake (e.g., too many or too few new names, forgetting a comma or the quotation marks.
  7. Press Close and OK.
  8. Move the newly-created Custom Rule called Correct wave names to the top of the list. It is not always required to be at the top of the list, but if you have any other rules that change the names of columns or row labels, it will prevent this rule from being applied, so it is preferable to have this rule at the top.
  9. To the right of the rule, select Apply > Add to every item in the project.
  10. To the right of the rule, ensure that New Items is checked.

A side-effect of this rule is that any tables containing labels of 4701 or above may inadvertently be renamed. You can work around this by replacing 4700 in the code above, and 4701 correspondingly in the code below.

correct_names = ["My first wave","2nd wave", "3rd wave"];
form.setSummary("Correct wave names");

// Function to adjust and set labels 
correctNames = function(by_rows, is_span) {
    var labels;
    if (is_span) {
        var spans = by_rows ? table.rowSpans : table.columnSpans;
        if (spans.length == 0)
        labels = (obj) { return obj.label; } )
    } else
        labels = by_rows ? table.rowLabels : table.columnLabels;
    if (labels == null)
    var n = labels.length;
    if (correct_names.length > n || n > 200)
    if (labels.indexOf("4701") == -1)
    for (var i = 0; i < n; i++) {
        var current_label = labels[i];
        if (current_label >=4701)
            labels[i] = correct_names[current_label - 4701];
    if (is_span) {
        // Remove all spans (these will be rebuilt below)
        if (by_rows)
        // Set spans with altered labels
        spans.forEach(function (obj, ind) {
            if (by_rows)
                table.spanRows(obj.indices, labels[ind]);
                table.spanColumns(obj.indices, labels[ind]);    
    } else {
        if (by_rows)
            table.rowLabels = labels;
            table.columnLabels = labels;
// Adjust labels (and spans in Q 4.11 and higher) 
correctNames(true, false);
correctNames(false, false);
if (fileFormatVersion() >= 8.81) {
    correctNames(true, true);
    if (table.columnLabels != null)
        correctNames(false, true);