如何创建显示起始于4月、次年3月截止的YTD数据图表?
创建非自然年(4月起)的YTD数据图表
用Excel实现的步骤
- 整理原始数据,确保日期列格式为标准日期格式(可通过「数据」→「分列」或设置单元格格式完成)
- 添加自定义YTD周期列:在空白列输入公式
=IF(MONTH(A2)>=4,YEAR(A2),YEAR(A2)-1)&"年4月-"&IF(MONTH(A2)>=4,YEAR(A2)+1,YEAR(A2))&"年3月",下拉填充,将每个日期归类到对应的4月起始周期中 - 生成YTD汇总数据:插入数据透视表,行区域选择自定义周期列,值区域选择需要统计的指标(如求和、平均值)
- 创建图表:选中透视表的汇总数据,插入折线图/柱状图,调整标签和样式即可
用Python(Pandas+Matplotlib)实现的代码示例
import pandas as pd import matplotlib.pyplot as plt # 1. 加载并预处理数据 # 假设数据包含date(日期)和value(指标值)两列 df = pd.read_csv('your_data.csv') df['date'] = pd.to_datetime(df['date']) # 2. 定义自定义YTD周期函数 def get_ytd_period(date): if date.month >= 4: start_year = date.year end_year = date.year + 1 else: start_year = date.year - 1 end_year = date.year return f"{start_year}年4月-{end_year}年3月" df['ytd_period'] = df['date'].apply(get_ytd_period) # 3. 按周期汇总YTD数据 ytd_summary = df.groupby('ytd_period')['value'].sum().reset_index() # 4. 绘制图表 plt.figure(figsize=(10, 6)) plt.bar(ytd_summary['ytd_period'], ytd_summary['value'], color='#4285F4') plt.title('4月起始的YTD数据汇总', fontsize=14) plt.xlabel('YTD周期', fontsize=12) plt.ylabel('指标汇总值', fontsize=12) plt.xticks(rotation=45, ha='right') plt.tight_layout() plt.show()
内容的提问来源于stack exchange,提问作者Asyraf PHMR
相关产品推荐
相关产品推荐

