Forum Discussion
Max y-axis based on field parameter selected
Hi nleuck_101 ,
You can absolutely create a dynamic Y-axis that adjusts to your field parameter. The solution is to write a single DAX measure that calculates an appropriate maximum value based on the data visible in the chart. This measure will then be used to control the Y-axis range in your chart's formatting options.
First, you'll need to create a new measure in Power BI. This DAX code finds the highest value among the currently visible bars, adds a 15% buffer for headroom, and then intelligently rounds that value up to a clean, readable number. For instance, a max value of 860 will result in an axis of 1000, and a max value of 123 will result in an axis of 150.
Dynamic Y-Axis Max =
// Step 1: Find the maximum value currently displayed on the chart
VAR MaxValue =
MAXX(
ADDCOLUMNS(
SUMMARIZE(
ALLSELECTED(YourMainDataTable), // <-- 1. CHANGE to your main data table name
YourXAxisColumn, // <-- 2. CHANGE to your X-axis column
YourLegendColumn // <-- 3. CHANGE to your Legend column (e.g., 'Table'[Year])
),
"@ChartValue", [Your Field Parameter Measure] // <-- 4. CHANGE to your measure
),
[@ChartValue]
)
// Step 2: Add a buffer (e.g., 15%) and round up to a "nice" number
VAR PaddedValue = MaxValue * 1.15 // Adjust 1.15 for more/less padding
VAR RoundedValue =
SWITCH(
TRUE(),
MaxValue = 0, 10, // Handles case where max is 0 to prevent errors
PaddedValue > 500, ROUNDUP(PaddedValue, -2), // Rounds up to the nearest 100
PaddedValue > 100, CEILING(PaddedValue, 50), // Rounds up to the nearest 50
PaddedValue > 10, CEILING(PaddedValue, 10), // Rounds up to the nearest 10
CEILING(PaddedValue, 5) // Rounds up to the nearest 5
)
RETURN
RoundedValue
Before this code will work, you must replace the four placeholders with the actual names from your data model. YourMainDataTable should be the name of your primary data table. YourXAxisColumn is the column on the chart's X-axis. YourLegendColumn is the field used in the Legend area, which is Year in your case. Finally, [Your Field Parameter Measure] is the measure being displayed by your field parameter; if you dragged the parameter itself to the Y-axis field well, this would be the name of that parameter measure (e.g., [Parameter]).
After creating the measure, you need to apply it to your visual. Select the bar chart, then go to the Format your visual pane (the paintbrush icon 🖌️). Expand the Y-axis section, and under Range, find the Maximum value box. Click the conditional formatting (fx) button next to it. In the dialog that opens, set the Format style to Field value and select your new Dynamic Y-Axis Max measure as the field. Once you click OK, your chart's Y-axis will automatically resize based on your slicer selections, giving you a clean visual every time.
Best regards,
- nleuck_10110 months agoContinued Contributor
Hello DataNinja777 ,
Thank you for your response. I have an issue though, in the formula where you change the value to "your field paramter measure", mine is not a measure, it's a combination of 3 measures. The column that I created from the field parameter is actually 3 different measures. I use the column created by the field parameter select one of the measures to change the charts. Below is my DAX:
Table = {("Name1", NAMEOF([Measure1]), 0), ("Name2", NAMEOF([Measure2]), 1), ("Name3", NAMEOF([Measure3]), 2)