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

Power BI中如何将FY格式财年字符串转换为对应精确日期

FY格式财年字符串转季度首月首日方案

规则说明

  • 输入字段为字符串格式,取值形如FY23Q1
  • 财年计算规则:财年从每年10月开始,到次年9月结束
  • 转换目标:将FY格式字符串重编码为对应季度首月的第一天,输出格式为MM/DD/YYYY

固定映射关系

输入值输出值
FY23Q110/01/2022
FY23Q201/01/2023
FY23Q304/01/2023
FY23Q407/01/2023

通用转换逻辑

  1. 从输入字符串中提取两位财年后缀(如FY23Q1提取23)、季度数字(如FY23Q1提取1)
  2. 按季度匹配对应公历年份和月份:
    • Q1:公历年份为2000 + 财年后缀 - 1,月份为10月
    • Q2:公历年份为2000 + 财年后缀,月份为1月
    • Q3:公历年份为2000 + 财年后缀,月份为4月
    • Q4:公历年份为2000 + 财年后缀,月份为7月
  3. 拼接为对应月份1日的日期,按MM/DD/YYYY格式输出即可

代码实现示例(Python)

import re
from datetime import date

def convert_fy_to_quarter_first_day(fy_str: str) -> str:
    pattern = r'^FY(\d{2})Q([1-4])$'
    match_res = re.match(pattern, fy_str.strip())
    if not match_res:
        raise ValueError('输入格式错误,需符合FYxxQx格式,例如FY23Q1')
    fy_num = int(match_res.group(1))
    q_num = int(match_res.group(2))
    
    q_config = [
        (10, -1),
        (1, 0),
        (4, 0),
        (7, 0)
    ][q_num - 1]
    target_month, year_offset = q_config
    target_year = 2000 + fy_num + year_offset
    return date(target_year, target_month, 1).strftime('%m/%d/%Y')

调用测试:

print(convert_fy_to_quarter_first_day('FY23Q1')) # 输出 10/01/2022
print(convert_fy_to_quarter_first_day('FY23Q2')) # 输出 01/01/2023
print(convert_fy_to_quarter_first_day('FY23Q3')) # 输出 04/01/2023
print(convert_fy_to_quarter_first_day('FY23Q4')) # 输出 07/01/2023

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 03:06:16