Skip to content

Mapping.Date to Date.Date to Prior Year Month

The below code builds a mapping from Date elements to prior year Date parent elements. For each Date element in the Date List hierarchy, it walks up one level in Default hierarchy - date to month, then maps backs one year to that prior year month.

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

Process Code

vb
var mapName = "Mapping.Date to Date.Date to Prior Year Month";
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 mon = hierarchy.parentByIndex("Date", "Default", date, 0);


        let year = mon.substring(0, 4);

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

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

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