Here’s a question from Alice:
I am working on a dashboard which includes a stacked bar chart. Need to add a constant line that runs horizontally starting the ;eft hand side of the y axis and is based on a measure in the data model. Any ideas on how to proceed?
Step 1: Create your Power BI chart
- At the bottom of the Power BI Report View hit the + icon to add a new Page to your report.
- From the Visualizations pane, drag a new visual. In this example we will use a Stacked column chart.
- Drag the relevant fields into your chart. In this example we will use the Month name field for the X axis. the Amount Sum for the Y-Axis and the Course Name for the legend.
Step 2: Insert a constant horizontal line
To add an horizontal reference line parallel to the X – Axis, proceed as following:
- Select your visual.
- From the Visualizations pane, hit the magnifying glass (add analyses to your visual).
- Under Constant Line. hit the Add Line link.
- Name your horizontal line – in this case the selected name will be Profitability.
- Type a Value – in our case, we selected 13000.
- Set the Line style and Position.
Here’s the chart
Here’s our chart – note the dashed line running in parallel to the x-Axis line.
Note: Make sure to adjust the Transparency setting in case that your constant line is not showing up.
Step 3: Insert an horizontal line based on measure
Another option is to base the reference line on the value of a measure:
- First, off let’s define a measure in our data model. In our case we’ll define a measure representing the Average Sales Amount.
- We will then hit the Add further Analyses to your visual button.
- Under Value, hit the function button.
- Pick the Measure from the data model.
- Hit OK.
- The horizontal line data label and position values will be updated as shown below:
Plot a vertical constant line in Power BI
We got a question also on adding a reference line that is plotted vertically in our chart.
To add a vertical line to your Power BI chart, follow the steps detailed below:
- Create a new table in your Power Bi data model representing the vertical line:
Vertical Line = DATATABLE("Date", DATETIME, {{"2023-06-15"}})
- Define relationships between your new table and other relevant tables in the data model.
- Add both your main data table and the Vertical Line table to your line chart visual.
- From the Fields pane, drag the Date field from your main table to the Axis.
- Add your measure (e.g., Amount Sum) to the Values field.
- Drag the Date field from the Vertical Line table to the Axis as well.
- In the Format pane, under Series, find the new series created by the Vertical Line table. Set its color and increase the stroke width for visibility.
The vertical line is created by adding a single-point series to your existing chart, leveraging Power BI’s ability to plot multiple data series on the same visual.
If for some reason, the line doesn’t appear, ensure the date formats match between your tables and check that the vertical line date falls within your main data set’s range.
Next Learning
How to count table rows meeting a specific condition in Power BI?