Skip to content

Mapping.Date to Date.Date to Prior Year Date

The below code builds a mapping from Date elements to prior year Date elements. For each Date element in the Date List hierarchy, it maps back one year to that prior year date.

End outcome would be Date:2016-01-01 maps to date:2015-01-01.

Process Code

vb
var mapName = "Mapping.Date to Date.Date to Prior Year Date";
var lDims = ["Date"];
var rDims = ["Date"];

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 dates = JSON.parse(hierarchy.rootElements("Date", "Date List"));

    for (let i = 0; i < dates.length; i++) {
        let date = dates[i];
        let year = date.substring(0, 4);

        let lastYear = (Number(year) - 1).toString()

        let lastYearDate = date.replace(year, lastYear)
        if (hierarchy.hasMember("Date", "Default", lastYearDate)) {
            mapping.rowAdd(mapName, [date], [lastYearDate]);
        }

        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.');
}