折线图过滤数据点且不限制X轴范围,仅展示指定X轴值折线
Alright, let's figure out how to make your line chart behave exactly like you want—keeping the full hourly X-axis labels while only drawing the line for hours 21, 22, 23 when your slicer picks value 3. Here's the step-by-step fix, assuming you're working in Power BI (since you mentioned slicers):
1. Create a Filtered Measure Using DAX
The key here is to use a measure that returns values only for your target hours when the slicer is set to 3, and returns blank for all other cases. Blanks won't render on the line chart, so those segments won't appear.
Here's the DAX code you can adapt to your table names:
Filtered Inbound Value = VAR SelectedSlicer = SELECTEDVALUE('Slicer Table'[Your Slicer Column], 0) VAR CurrentHour = SELECTEDVALUE('Hour Dimension Table'[Hour]) RETURN IF( SelectedSlicer = 3 && CurrentHour IN {21, 22, 23}, SUM('Inbound Data Table'[Inbound Value]), BLANK() )
SelectedSlicergrabs the value you've picked in the slicer (the0is a fallback if nothing's selected)CurrentHourtargets the hour value currently being evaluated on the X-axis- The
IFstatement checks if both conditions are met: slicer is 3, and hour is 21/22/23. If yes, it calculates the sum of your inbound values; if not, it returns blank.
2. Set Up Your Line Chart
Now wire this measure into your chart:
- Drag your Hour column (from your hour dimension table) into the X-axis well. Make sure this table includes all hours (0-23) you want displayed—this ensures the full X-axis labels stay visible even if there's no data for some hours.
- Drag the
Filtered Inbound Valuemeasure you just created into the Y-axis well. - Keep your slicer connected to the same table you referenced in the measure.
3. Test the Behavior
When you select value 3 in the slicer, you'll only see the line plotted for hours 21, 22, 23. All other hour labels will still show up on the X-axis, but no line will be drawn for them. If you select any other value in the slicer, the measure returns blank, so no line will appear at all (you can adjust the IF condition if you want other slicer values to show full data—just tweak the logic to your needs).
内容的提问来源于stack exchange,提问作者Sid

