Sign In

Welcome Back,

Signup/Login with your account

You must login to ask a question.

You must login to add post.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

  • 0
Ankush singh
Contributor

can i use derived variable in custom code?

can i use derived variable in custom code?

Leave an answer

You must login to add an answer.

1 Answer

  1. Yes, in Lumenore Self Service, you can use a derived variable in Custom Code, but only if:

    • It is created in the schema or dataset
    • It is included in the query used by the visualization

    1. Create the Derived Variable

    You can create it at two levels:

    Option A: Schema Level (Recommended)

    Steps:

    1. Go to Data → Manage Schema
    2. Open your schema
    3. Go to Variables / Derived Variables
    4. Create the variable

    Example:

    Profit_Margin = ([Profit] / [Sales]) * 100
    1. Save & Publish Schema

    Option B: Dashboard Level

    • You can also create a derived variable inside the dashboard
    • But schema-level variables are more reliable in custom code

    2. Add Derived Variable to Query (IMPORTANT)

    This is the step most people miss.

    Steps:

    1. Open your visualization
    2. Go to custom code
    3. Create variable and give it a name say datapool
    4. Add Profit_Margin and other columns if needed

    3. Access It in Custom Code

    • data.datapool.metaData
    • data.datapool.resultSet

    Example Structure:

    datapool = {
    queryInfo: {…},
    metaData: […],
    resultSet: [
    [12000, “Furniture”, 35]
    ]
    }

    Access Using Index (Basic Way)

    let r = data.datapool.resultSet;

    r.forEach(row => {
    let sales = row[0];
    let category = row[1];
    let profitMargin = row[2];

    console.log(“Profit Margin:”, profitMargin);
    });


    4. Use Metadata

    Avoid hardcoding index — use metaData.

    let index = data.datapool.metaData.findIndex(
    col => col.columnName === “Profit_Margin”
    );

    data.datapool.resultSet.forEach(row => {
    console.log(row[index]);
    });