You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Power BI X轴稳定显示12个月财年区间(含无数据月份)

折线图制作方案

原始数据集

NameDateCertificate Completed
Apple01/21/2024Cert 1
Banana04/20/2023Cert 1
Cherry11/01/2024Cert 2
Desert06/06/2023Cert 4

需求说明

  • X轴需覆盖2023年7月至2024年6月的12个财年月份,无数据月份必须显示并计为0
  • Y轴为每月完成的证书总数
  • 图表类型为折线图

实现方法

方法一:Excel操作步骤

  1. 数据预处理

    • 在原始数据旁新增一列,用公式=TEXT(B2,"yyyy-mm")提取每条记录的年月(B2为Date列单元格,按需调整)
    • 单独列出2023-07到2024-06的所有年月,作为X轴的固定序列
    • 对每个年月用COUNTIF统计证书数量,比如=COUNTIF(提取的年月列范围, "2023-07"),无数据的月份会自动返回0
  2. 生成折线图

    • 选中固定年月序列和对应的计数数据
    • 插入折线图,将X轴设置为分类轴,确保12个月份全部显示
    • 修改Y轴标签为「每月完成证书总数」,调整图表样式

方法二:Python(Pandas + Matplotlib)代码实现

import pandas as pd
import matplotlib.pyplot as plt

# 构造原始数据集
raw_data = {
    "Name": ["Apple", "Banana", "Cherry", "Desert"],
    "Date": ["01/21/2024", "04/20/2023", "11/01/2024", "06/06/2023"],
    "Certificate Completed": ["Cert 1", "Cert 1", "Cert 2", "Cert 4"]
}
df = pd.DataFrame(raw_data)

# 转换日期格式并提取年月
df["Date"] = pd.to_datetime(df["Date"], format="%m/%d/%Y")
df["YearMonth"] = df["Date"].dt.to_period("M")

# 创建财年完整月份序列
fy_periods = pd.period_range(start="2023-07", end="2024-06", freq="M")

# 统计每月证书数,无数据填充0
monthly_totals = df.groupby("YearMonth").size().reindex(fy_periods, fill_value=0)

# 绘制折线图
plt.figure(figsize=(10, 6))
monthly_totals.plot(kind="line", marker="o", color="#1f77b4")

# 设置图表参数
plt.title("2023财年(7月-次年6月)每月证书完成情况")
plt.xlabel("月份")
plt.ylabel("每月完成证书总数")
plt.xticks(rotation=45)
plt.grid(axis="y", linestyle="--", alpha=0.7)
plt.tight_layout()
plt.show()

内容的提问来源于stack exchange,提问作者The Dodo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 17:37:21