How To Add Labels In Power Bi

How to Add Labels in Power BI

Labels are an essential component in Power BI as they help to provide context and descriptions to the visual elements in your reports. Here’s a detailed explanation on how to add labels in Power BI with examples:

  1. Adding Labels to Text Elements:
  2. Labels can be added to text elements such as titles, axis labels, and data labels.

                
                <div id="myVisual"></div>
                
                <script>
                    var textElement = {
                        @type: "Text",
                        @objects: [
                            {
                                @$schema: "http://powerbi.com/product/schema#basic",
                                @target: {
                                    @table: "myData",
                                    @column: "myColumn"
                                },
                                @properties: {
                                    @fontSize: 12,
                                    @fontColor: "#000000",
                                    @textAlignment: "center",
                                    @fontWeight: "bold"
                                }
                            }
                        ]
                    };
                    
                    var visualContainer = $('#myVisual');
                    var visual = powerbi.visuals.createVisual('text', null, null);
                    visual.init(options);
                    visualContainer.append(visual.element);
                    visual.update(textElement);
                </script>
                
            

    The above code snippet demonstrates how to add a label to a text element in Power BI. Here, we define the text element properties such as font size, font color, text alignment, and font weight. By specifying the target table and column, we associate the label with a specific data field.

  3. Adding Labels to Visualizations:
  4. You can also add labels to visualizations such as charts, tables, and maps to provide additional information.

                
                <div id="myVisual"></div>
                
                <script>
                    var data = [{
                        "Category": "A",
                        "Value": 10
                    }, 
                    {
                        "Category": "B",
                        "Value": 20
                    }];
                    
                    var visualElement = {
                        @type: "Visual",
                        @objects: [
                            {
                                @$schema: "http://powerbi.com/product/schema#basic",
                                @target: {
                                    @table: "myData",
                                    @visualType: "table"
                                },
                                @properties: {
                                    @showHeader: true,
                                    @showSubtotals: false,
                                    @showGrandTotals: true
                                }
                            }
                        ]
                    };
                    
                    var visualContainer = $('#myVisual');
                    var visual = powerbi.visuals.createVisual('table', null, null);
                    visual.init(options);
                    visualContainer.append(visual.element);
                    visual.update(data);
                </script>
                
            

    The above code demonstrates how to add labels to a table visualization. In this example, we define the table properties such as showing headers, subtotals, and grand totals. The data array contains the values to be displayed in the table.

  5. Adding Custom Labels:
  6. Power BI also allows you to add custom labels by utilizing the built-in Custom Visuals feature.

                
                <div id="myVisual"></div>
                
                <script>
                    var customLabel = {
                        "data": {
                            "labels": ["Label 1", "Label 2", "Label 3"],
                            "values": [10, 20, 30]
                        }
                    };
                    
                    var visualContainer = $('#myVisual');
                    var visual = powerbi.visuals.createVisual('myCustomVisual', null, null);
                    visual.init(options);
                    visualContainer.append(visual.element);
                    visual.update(customLabel);
                </script>
                
            

    In the code above, we provide a custom label object with an array of labels and corresponding values. The custom visual ‘myCustomVisual’ is created and updated with the custom label data.

By following the above steps, you can easily add labels to different elements and visualizations in your Power BI reports. Labels provide context and improve the understanding of your data for users.

Read more

Leave a comment