Data view mapping in Power BI visuals

670 Views Asked by At

Good day

I am creating custom visualization on d3js and pbiviz for powerbi

Here is the code in capabilities.js:

{
"dataRoles":[
    {
        "displayName": "HoleDepth",
        "name": "depth",
        "kind": "Grouping"
    },
    {
        "displayName": "Days",
        "name": "days",
        "kind": "Measure"
    },
    {
        "displayName": "Diametrs",
        "name": "diametrs",
        "kind": "Measure"
    },
    {
        "displayName": "Sensor1",
        "name": "sensor_1",
        "kind": "Measure"
    },
    {
        "displayName": "Sensor2",
        "name": "sensor_2",
        "kind": "Measure"
    },
    {
        "displayName": "Sensor3",
        "name": "sensor_3",
        "kind": "Measure"
    },
    {
        "displayName": "Sensor4",
        "name": "sensor_4",
        "kind": "Measure"
    }
],
"dataViewMappings": [
    {
        "categorical": {
            "categories": {
                "for": { "in": "depth" }
            },
            "values": {
                "select":[
                    { "bind": { "to": "days" } },
                    { "bind": { "to": "diametrs" } },
                    { "bind": { "to": "sensor_1" } },
                    { "bind": { "to": "sensor_2" } },
                    { "bind": { "to": "sensor_3" } },
                    { "bind": { "to": "sensor_4" } }
                ]
            }
        }
    }
]

}

But in visualization it is inconvenient to use categorical -> values array enter image description here

Is it possible to categorical -> values was like an object with keys?

enter image description here

2

There are 2 best solutions below

3
Marek On

I do not think that this is possible directly through data mapping. What I usually do if I want to have data prepared in the specific format, convenient for visualization with d3.js, is the custom function that transforms the data from VisualUpdateOptions. Then I call this function inside public update(options: VisualUpdateOptions)

0
TRAVIS WINTER On

Bit of an old post, but I've just started on pbiviz and typescript and had to work this out, so hopefully this helps others who come by

You can reference objects from the dataview using find

For example:

let days = dataView.categorical.values.find(value => value.source.roles.days === true);
let diametrs = dataView.categorical.values.find(value => value.source.roles.diametrs === true);
let sensor_1 = dataView.categorical.values.find(value => value.source.roles.sensor_1 === true);

and then pull their values:

let days_value = days.values[0];
let diametrs_value = diametrs.values[0];
let sensor_1_value = sensor_1.values[0];

so you don't need to rely on the array index