Skip to content

Mapping.Time to Time.Month to Year Annual

The below code builds a mapping from Time.Month elements to Time.Annual elements. For each month, it walks two levels up the Default hierarchy — month to quarter, then quarter to year — then maps the month to that year's annual element (the year name suffixed with - Annual).

End outcome would be 2016 - Jul maps to 2017 - Annual

Time Default

Time Annual Assumption

Process Code

vb
var mapName = "Mapping.Time to Time.Month to Year Annual";
var lDims = ["Time"];
var rDims = ["Time"];

function pre() {
    // This function is called once before the processes is executed.
    // Use this to setup prompts.
    script.log('process pre-execution parameters parsed.');
}

function begin() {
    // This function is called once at the start of the process
    script.log('process execution started.');

    if (mapping.exists(mapName)) {
        mapping.wipe(mapName);
    } else {
        mapping.create(mapName, lDims, rDims, false, true);
    }

    let months = JSON.parse(hierarchy.rootElements("Time", "Month List"));

    for (let i = 0; i < months.length; i++) {
        let mon = months[i];

        let quarter = hierarchy.parentByIndex("Time", "Default", mon, 0);
        let year = hierarchy.parentByIndex("Time", "Default", quarter, 0);

        let yearAnnual = year + " - Annual"
        if (hierarchy.hasMember("Time", "Default", year)) {
            mapping.rowAdd(mapName, [mon], [yearAnnual]);
        }

        if (script.IsCancelled()) {
            return;
        }
    }
}

function data(record) {
    // This function is called once for each line of data on the second cycle
    // Use this to build dimensions and push data into cubes
}

function end() {
    // This function is called once at the end of the process
    script.log('process execution finished.');
}