Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
paul-maessen
Resolver I
Resolver I

Creating SelectionIds when you have two categories in your DataViewMapping

Hi,

 

I do have a question about the creation of SelectionIds when your DataViewMapping has 2 categories.

 

The dataRoles and dataViewMapping:

 

 

{
    "dataRoles": [
        {
            "displayName": "Cat 1",
            "name": "cat1",
            "kind": "Grouping"
        },
        {
            "displayName": "Cat 2",
            "name": "cat2",
            "kind": "Grouping"
        },
        {
            "displayName": "Values",
            "name": "values",
            "kind": "Measure"
        }
    ],
    "dataViewMappings": [
        {
            "categorical": {
                "categories": {
                    "for": {
                        "in": "cat1"
                    },
                    "for": {
                        "in": "cat2"
                    }
                },
                "values": {
                    "select": [
                        {
                            "bind": {
                                "to": "values"
                            }
                        }

                }
            }
        }
    ]
}

 

 

My question is now how I can create the SelectionIds for this situation:

Is the following assumption correct?

 

 

// categories
const categories = dataView.categorical.categories;


// get count of category elements
const categoriesCount = categories[0].values.length;

// iterate all categories to generate selection and create button elements to use selections
for (let categoryIndex = 0; categoryIndex < categoriesCount; categoryIndex++) {

    const categorySelectionId = this.host.createSelectionIdBuilder()
        .withCategory(categories[0], categoryIndex) 
        .withCategory(categories[1], categoryIndex) 
        .createSelectionId();
}

 

 

Thanks in advance,

 

Regards

 

Paul

 

 

 

 

@dm-p

1 ACCEPTED SOLUTION
dm-p
Super User
Super User

Hi @paul-maessen,

Yes, I believe so. It should be fairly easy to tell from the resulting SelectionId if it has both categories and indices in the resulting map though, if you wanted to try it out.

The only thing I might do differently (and this is more of a personal preference and has no net effect on your code) is swap the for with a forEach and a fat arrow function, e.g.:

 

categories[0].values.forEach((v, vi) => {
    const categorySelectionId = this.host.createSelectionIdBuilder()
        .withCategory(categories[0], vi)
        .withCategory(categories[1], vi)
        .createSelectionId();
});

 

Cheers,

Daniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




View solution in original post

2 REPLIES 2
dm-p
Super User
Super User

Hi @paul-maessen,

Yes, I believe so. It should be fairly easy to tell from the resulting SelectionId if it has both categories and indices in the resulting map though, if you wanted to try it out.

The only thing I might do differently (and this is more of a personal preference and has no net effect on your code) is swap the for with a forEach and a fat arrow function, e.g.:

 

categories[0].values.forEach((v, vi) => {
    const categorySelectionId = this.host.createSelectionIdBuilder()
        .withCategory(categories[0], vi)
        .withCategory(categories[1], vi)
        .createSelectionId();
});

 

Cheers,

Daniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




Hi @dm-p 

Thanks for the answer and the confirmation.

I normally also would use a forEach, but this code I copied from the documentation from Microsoft and modified it 🙂

 

Regards

 

Paul

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.