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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jonnyglass
New Member

Newbie Questions on dataViewMappings and ColorHelper

Hello All, I've been through the tutorials and have starting working my way through my first custom visual.  Unfortunately it is a "real world" example and fairly complex.  I'm reasonably familiar with d3 and have build a bunch of visuals on another platform.  I have a few basic questions and running into some roadblocks...

 

Data View Mappings - I assume we are to choose the best one for our needs?  I've looked at the different options and the table seems the most straightforward and flexible - is there any other advantage/flexbility given by the other types (categories, matrix) and any reason not to start with the table results and transform that in code to what I need for my visual?

 

I have started with the table mapping for now and am running into an issue where the returned data is incomplete.  I have four fields in my dataset - category, entity, start date and end date.  I have all four fields defined as groupings.  PowerBI is returning only the first set of start/end dates for each category/entity.  Is there something that would cause the engine to aggregate or not consider all the fields when generating the visual's data?

 

I'm also trying to figure out the proper usage for the ColorHelper to assign color based on the category field.  The simple solution is using the ColorPalette.getColor() method, but the examples I've seen use the ColorHelper.  These examples call "DataViewValueColumns = visualUpdateOptions.dataViews[0].categorical.values" which seems to seed the ColorHelper.  If I'm using the table data view mapping, do I need to contruct my own "categorical.values" object or is there another method to using the ColorHelper with data in the table format.

 

Thanks so much for any help and I apologize in advance if my language isn't precise enough.

4 REPLIES 4
dm-p
Super User
Super User

Hi @jonnyglass,

One of the hardest challenges coming from a D3 background is that it's relatively easy to port your knowledge to Power BI visuals, but the concept of dynamically changing datasets (either through cardinality or sheer number of possible use cases when it comes to an end-user's model, plus handling dynamic sizing etc.) is where you can start to rack up the hours 😉

With data mappings, yes, you choose the most suitable one. table is easiest to start with, as it's similar to how many d3 examples etc. work, so is very flexible, particularly as visuals only have a single query, and therefore dataset. You can do whatever transforms you like in your code to achieve what you want, but when it comes to binding data back to Power BI - for example by distinct values in a particular column for colour assignment or interactivity purposes - it is the least flexible. It is not easy to reconcile by a high-level grouping over several rows and this is what the categorical or matrix mappings are for: you specify how the data gets grouped in the data view mapping and you can pass the details of that group's "selector" back to Power BI for reconciliation.

It is a hard concept to explain succinctly, but in short, the visual dataset is pushed from the main window and you cannot directly talk back to the model except for exposed APIs. In these cases, you use selectors to signal to Power BI how to delegate what you want to do , based on what it knows about the dataset it gave you orginally and what it knows about the data in other visuals on the page.

If you want to so colour assignment and you are expecting to do this by distinct values, this is where you might use a categorical mapping, in conjunction with the categorical and series properties. By specifying that Power BI group them this way, you can create selectors for these groups, which will ensure that Power BI knows to treat them en masse when assigning colours and displaying the entries in the properties pane, ot managing cross-filtering or drillthrough based on a unique value. Similarly this limits what you may be able to do with your data inside your visual, so there is a possible compromise in flexibility this way. It's a bit of a balancing act.

If you want a 'real-world' visual to look at, then I have developed this one as part of a course which intends to provide end-to-end coverage of development - the visual itself is open source and heavily commented for newcomers, so if you don't want to pay for the lessons then it may give you enough context to work your existing knowlege into what needs to be done to make things play nicely with Power BI.

Regarding your low cardinality, Power BI will group data by distinct values, so if you are seeing a low number of rows it is likely that your start/end dates are being treated as implicit measures. You might want to see if Power BI is aggregating by checking the field. For example, if Don't summarize is not checked, then it should work as you expect:

dmp_0-1634240678778.png

The other thing to consider is if you're adding a measure to a grouping column, Power BI will always aggregate to the highest level, so you will need to add a suitable column to create the necessary row context. This may not be your issue but it is something to thing about. I have an article that explains the concepts a bit further here which may help solidify your understanding. It's from an end-user pespective but does list a few common gotchas for those of use used to having a single row for what we assume is a dataset we're familiar with.

Hopefully some of this may help. Good luck!

Daniel





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

Proud to be a Super User!


My course: Introduction to Developing Power BI Visuals


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




MichaelBakker
Regular Visitor

For example code using ColorHelper this might be useful: https://github.com/microsoft/powerbi-visuals-sankey 

v-rzhou-msft
Community Support
Community Support

Hi @jonnyglass 

You may refer to this blog to get more details about ColorHelper : ColorHelper Class

For more details about data view mapping, you can refer to this code: PowerBI-visuals

 public static customizeQuery(options: CustomizeQueryOptions): void {
            var dataViewMapping = options.dataViewMappings[0];
            if (!dataViewMapping || !dataViewMapping.categorical || !dataViewMapping.categorical.categories)
                return;

            if (options.preferHigherDataVolume) {
                dataViewMapping.categorical.dataVolume = 4;
            }

            var dataViewCategories = <data.CompiledDataViewRoleForMappingWithReduction>dataViewMapping.categorical.categories;
            var categoryItems = dataViewCategories.for.in.items;
            if (!ArrayExtensions.isUndefinedOrEmpty(categoryItems)) {
                var categoryType = categoryItems[0].type;

                var objects: DataViewObjects;
                if (dataViewMapping.metadata)
                    objects = dataViewMapping.metadata.objects;

                if (CartesianChart.getIsScalar(objects, columnChartProps.categoryAxis.axisType, categoryType))
                    dataViewCategories.dataReductionAlgorithm = { sample: {} };
            }
        }

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

The ColorHelper you refer to has nothing to do with Power BI. Instead it is a component in the UWP toolkit.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.