How to Use Measure as Legend in Power BI

Cody Schneider7 min read

Using a measure for dynamic color-coding in your Power BI charts is a powerful trick that isn't immediately obvious. While you can't drag a measure directly into the 'Legend' field, you can achieve the same result with a clever DAX technique. This article will walk you through, step-by-step, how to dynamically segment your visuals by using a measure as a legend.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Why Use a Measure as a Legend, Anyway?

You might be wondering why you'd go through the trouble. The primary reason is dynamic categorization. Normally, you use a static column from your data - like "Product Category" or "Region" - to create a legend. This is great, but what if you want to group your data based on its performance?

Imagine you want to classify your products based on their sales performance within a selected date range:

  • Products selling over $10,000 are "High Performance."
  • Products selling between $1,000 and $10,000 are "Medium Performance."
  • Products selling less than $1,000 are "Low Performance."

These categories don't exist in a static column in your source data because a product's performance changes depending on the filters applied in your report (like the date, region, or customer segment). A product could be "High Performance" this quarter but was "Medium Performance" last quarter.

This is where using a measure as a legend comes in. It allows you to create visuals that automatically re-classify and re-color your data as you interact with slicers and filters, giving you immediate insight into performance shifts.

The Core Concept: A Disconnected Slicer Table

Since Power BI expects a column in the Legend field, we need to create one. The solution is to build a small, separate table in your data model that contains only your desired categories. This is often called a "disconnected table" because you will not create any relationships between it and your main data tables.

Here’s the simple, three-part process:

  1. Create a disconnected table that lists your dynamic categories (e.g., "High," "Medium," "Low"). This table’s column will be what you physically drag into the legend field of your visual.
  2. Write a "switcher" measure in DAX. This is the magic ingredient. This measure will link your main data metric (like Total Sales) to the categories in your disconnected table.
  3. Build the visual using the disconnected table's column as the legend and your new "switcher" measure as the value.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

Step-by-Step Guide: Building a Dynamic Sales Performance Chart

Let's walk through a common business scenario: categorizing products based on total sales. We’ll create a bar chart that displays monthly sales for each product, with the bars colored according to which performance tier they fall into for that month.

Assume you have a Sales table with columns like Product Name, Sale Date, and Sale Amount.

Step 1: Create a Disconnected Table for Your Categories

First, we need a table to hold our performance category labels. This will be the source for our legend.

In the Power BI ribbon, go to the Modeling tab and click on New Table. In the formula bar that appears, enter the following DAX expression:

Performance Categories = DATATABLE( "Category", STRING, { { "High Performance" }, { "Medium Performance" }, { "Low Performance" } } )

Here’s what this code does:

  • DATATABLE() is a DAX function that creates a new table from scratch.
  • "Category", STRING defines the name of our column ("Category") and its data type (a text STRING).
  • The values inside the curly braces {} are the rows for our table.

After you hit Enter, you’ll see a new table named Performance Categories in your data model with a single column. Remember, do not create any relationships between this table and your Sales table.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Step 2: Create Your Core and "Switcher" Measures

Now we need two measures. The first is a simple base measure to calculate our primary metric. The second is the "switcher" measure that will handle the dynamic logic.

First, the Base Measure

Let’s start with a standard calculation for total sales. Right-click on your Sales table and select New Measure. Enter this formula:

Total Sales = SUM('Sales'[Sale Amount])

This is a straightforward measure a lot of reports already have, which simply adds up an amount.

Next, the "Switcher" Measure

This is the most important part. This measure will evaluate the Total Sales for each product in your chart's context (e.g., for each product on the x-axis) and check which performance category it belongs to. It checks which category is currently being 'asked for' by the legend and returns the sales value only if it matches.

Create another new measure in your Sales table with the following DAX:

Dynamic Sales by Performance = VAR CurrentCategory = SELECTEDVALUE('Performance Categories'[Category]) VAR SalesForProduct = [Total Sales]

RETURN SWITCH( TRUE(), AND(SalesForProduct > 10000, CurrentCategory = "High Performance"), SalesForProduct, AND(SalesForProduct >= 1000, SalesForProduct <= 10000, CurrentCategory = "Medium Performance"), SalesForProduct, AND(SalesForProduct < 1000, CurrentCategory = "Low Performance"), SalesForProduct, BLANK() )

Let’s break down this powerful formula:

  • VAR CurrentCategory = SELECTEDVALUE('Performance Categories'[Category]): In the context of a visual's legend, Power BI evaluates this measure once for each category. This line captures which category is currently being evaluated - "High Performance," "Medium Performance," or "Low Performance."
  • VAR SalesForProduct = [Total Sales]: This calculates our base Total sales measure within the current filter context of the visual (e.g., for a specific product and a specific month).
  • RETURN SWITCH(TRUE(), ...): The SWITCH(TRUE()) pattern is a clean way to handle multiple IF-THEN conditions. It looks for the first condition that evaluates to TRUE and executes its result.
  • Conditions check if sales are above certain thresholds and match the current category, if so, return the sales value, otherwise BLANK() to avoid displaying irrelevant bars.

Step 3: Build Your Visualization

Now for the easy part! Let’s assemble our stacked column chart:

  1. Select a Stacked column chart (or another chart type like a Scatter plot) from the Visualizations pane.
  2. Drag Product Name onto the X-axis well.
  3. Drag the Category column from Performance Categories into the Legend well.
  4. Drag the measure Dynamic Sales by Performance into the Y-axis well.

You should now see a chart where each product has a single column, color-coded based on its sales performance! If you add a date slicer to your report page, you can watch the colors change in real-time as different time periods are selected, dynamically regrouping products into their current performance tiers.

Tips for Better Dynamic Legends

Once you've mastered the basic technique, you can refine it further.

Tip 1: Make Your Thresholds Dynamic

What if your performance thresholds ($10,000 and $1,000) need to change? You can make them more manageable by defining them as their own measures or bringing them in from a parameters table. That way, you only need to update them in one place instead of digging into your main DAX formula.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

Tip 2: Keep Performance in Mind

For enormously large datasets, complex SWITCH or IF statements can impact report performance. This technique is generally very fast, but if you notice slowness, be mindful of how many conditions you're adding. Always test performance with your real data volumes.

Tip 3: The Advantage Over a Calculated Column

A common first thought for this problem is, "Why not just create a calculated column on the Sales table that assigns the category?" The answer is that a calculated column is static. It's calculated once when the data loads and doesn't change when you interact with slicers. Our measure-based approach recalculates on-the-fly, giving you the dynamic behavior that makes this technique so valuable.

Final Thoughts

Creating this kind of dynamic segmentation might feel like a workaround, but it’s a standard and powerful pattern in the Power BI world. By combining a disconnected table with a carefully crafted DAX measure using SELECTEDVALUE and SWITCH, you can build incredibly responsive and insightful reports that go far beyond standard visualizations.

Of course, this solution requires time spent thinking through DAX logic and data models. As our team has spent years building reports and wrangling tools like Power BI to get these answers, we've come to believe the process should be simpler. We created Graphed to simplify precisely this kind of task – you can just describe what you want to see in plain English, like "Show me sales by product, categorized as high, medium, or low," and our AI data analyst builds the live, interactive visual for you without any complex setup.

Related Articles