top of page
Writer's pictureHari Prasad

How to Implement Legends in Power BI Visuals

How to Implement Legends in Power BI Visuals



Legends play a crucial role in Power BI visuals, providing context to data and enhancing the interpretability of charts. This blog post demonstrates how to implement legends in line and clustered column charts using DAX formulas.

Steps to Implement Legends

1. Create a Legend Table

We start by creating a Legend Table using the DATATABLE function to define the legend items and their associated properties, such as icons and colors.


Legend Table =

DATATABLE(

"Attribute Name", STRING,

"Legend Item 1", STRING,

"Circle Icon1", STRING,

"Color1", STRING,

"Legend Item 2", STRING,

"Circle Icon2", STRING,

"Color2", STRING,

"Legend Item 3", STRING,

"Circle Icon3", STRING,

"Color3", STRING,

{

{"BUDGET_LABOR_HOURS", "Budget Labor Hours", "●", "#ADD8E6", BLANK(), BLANK(), BLANK(), BLANK(), BLANK(), BLANK()},

{"BUDGET_TOTAL_LABOR_HOURS", "Budget Total Labor Hours", "●", "#00008B", BLANK(), BLANK(), BLANK(), BLANK(), BLANK(), BLANK()},

{"BUDGET_LABOR_VAR_WITH_DUV", "Budget Labor Var", "●", "#FFA500", "Budget Labor Hours", "●", "#ADD8E6", "Budget Total Labor Hours", "●", "#00008B"}

}

)


The Legend Table defines:

  • Attribute Name: A unique identifier for each attribute.

  • Legend Items: Names, icons (e.g., ●), and colors associated with each legend item.

2. Create Dynamic Color Measures

To dynamically apply colors to visuals based on the selected attribute, use the following measures:


Dynamic Color1 =

VAR SelectedAttribute = SELECTEDVALUE('Labour Data'[Attribute Name])

RETURN

LOOKUPVALUE(

'Legend Table'[Color1],

'Legend Table'[Attribute Name], SelectedAttribute,

"#FFFFFF" // Default color

)


Dynamic Color2 =

VAR SelectedAttribute = SELECTEDVALUE('Labour Data'[Attribute Name])

RETURN

LOOKUPVALUE(

'Legend Table'[Color2],

'Legend Table'[Attribute Name], SelectedAttribute,

"#FFFFFF" // Default color

)


Dynamic Color3 =

VAR SelectedAttribute = SELECTEDVALUE('Labour Data'[Attribute Name])

RETURN

LOOKUPVALUE(

'Legend Table'[Color3],

'Legend Table'[Attribute Name], SelectedAttribute,

"#FFFFFF" // Default color

)


These measures use LOOKUPVALUE to fetch colors from the Legend Table based on the selected attribute. A default color (#FFFFFF) is applied if no match is found.

3. Create a Title Table and Dynamic Title

A dynamic title enhances the visualization by providing context based on user selection.


Title Table =

DATATABLE(

"Attribute Name", STRING,

"Display Title", STRING,

{

{"BUDGET_LABOR_HOURS", "Budget Labor Hours"},

{"BUDGET_TOTAL_LABOR_HOURS", "Budget Total Labor Hours"},

{"BUDGET_LABOR_VAR_WITH_DUV", "Budget Labor Var"},

{"All Attributes", "All Attributes"}

}

)


Dynamic Title =

VAR SelectedAttribute = SELECTEDVALUE('Labour Data'[Attribute Name], "All Attributes")

VAR DisplayTitle = LOOKUPVALUE('Title Table'[Display Title], 'Title Table'[Attribute Name], SelectedAttribute)

VAR SelectedStartDate = MIN('Labour Data'[Calendar Day])

VAR SelectedEndDate = MAX('Labour Data'[Calendar Day])

RETURN

"Analysis of " & DisplayTitle &

" from " & FORMAT(SelectedStartDate, "YYYY-MM-DD") &

" to " & FORMAT(SelectedEndDate, "YYYY-MM-DD")


  • Dynamic Title adjusts based on the selected attribute and date range.

  • LOOKUPVALUE fetches the title from the Title Table, while FORMAT ensures proper date display.

Visual Implementation

  1. Add the Legend Table to Your Data Model

    • Import or create the Legend Table directly in Power BI.

  2. Assign Colors to Data Points

    • Use the dynamic color measures (Dynamic Color1, Dynamic Color2, Dynamic Color3) to conditionally format visuals.

  3. Apply Dynamic Titles

    • Set the chart title to the Dynamic Title measure for a contextual heading.

Example Use Case

Consider a Line and Clustered Column Chart showing labor hours:

  • X-axis: Calendar day.

  • Y-axis: Values for budget labor hours, total labor hours, and labor variance.

  • Legend: Dynamically updates based on selected attributes and colors.

Conclusion

Implementing legends in Power BI visuals enhances clarity and user experience. By using DAX formulas, dynamic measures, and tables, you can create visually appealing and context-rich charts tailored to your data's needs. Explore these techniques to make your Power BI reports more insightful!

3 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page