PowerQuery——年度动态变化

PowerQuery - dynamic change of the year

我正在旋转和转置(输入)以便为内部系统格式化(输出)。 @RonRosenfeld 在 中提供的解决方案解决了这个问题。

然而,输出年份被硬编码为 2022 年,这在我们达到今年下半年之前都很好,然后我想将新月份动态更改为 2023 年,即 01.2023、02.2023...

我无法提出动态解决方案,所以我尝试了“财政年度”,即使在修改数据类型后也不起作用。

[输入]

[输出]


    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Index", Int64.Type}, {"Person", Int64.Type}, {"Dept", Int64.Type}, 
        {"Jan", Int64.Type}, {"Feb", Int64.Type}, {"Mar", Int64.Type}, 
        {"Apr", Int64.Type}, {"May", Int64.Type}, {"Jun", Int64.Type}, 
        {"Jul", Int64.Type}, {"Aug", Int64.Type}, {"Sep", Int64.Type}, 
        {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}, 
        {"Time", type datetime}, {"User", type text}},"en-150"),

    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", 
        {"Index", "Person", "Dept", "Time", "User"}, "Month", "Sales"),

    mnthCol = Table.TransformColumns(#"Unpivoted Other Columns", {"MNTH", each 
        Date.ToText(Date.FromText("1-" & _ & DateTime.ToText(Date.AddMonths(DateTime.LocalNow(),8),"yyyy")),"MM.yyyy"),type text}),
    #"Changed Type1" = Table.TransformColumnTypes(mnthCol,{{"MNTH", type date}}),

    FC = Table.AddColumn(#"Changed Type1", "Fiscal Month", each if Date.ToText(Date.Month([MNTH])) >=7 then Date.ToText(Date.Month([MNTH]))-6 else Date.ToText(Date.Month([MNTH]))+6),

    #"Reordered Columns" = Table.ReorderColumns(mnthCol,{"Index", "Person", "Dept", "Month", "Sales", "Time", "User"}),

    rename = Table.RenameColumns(#"Reordered Columns",{
        {"Time","STMP"},
        {"Dept","Depr"}
    })
in
   rename 

调整是为了改变

mnthCol = Table.TransformColumns(#"Unpivoted Other Columns", {"Month", each 
    Date.ToText(Date.FromText("1-" & _ & "-2022"),"M.yyyy"),type text}),

类似于

   mnthCol = Table.TransformColumns(#"Unpivoted Other Columns", {"Month", each 
    if  Date.Month(Date.FromText("1-" & _ & "-1900"))<7 then 
    Date.ToText(Date.FromText("1-" & _ & "-2023"),"M.yyyy")
   else 
   Date.ToText(Date.FromText("1-" & _ & "-2022"),"M.yyyy")
    ,type text}),

但您必须以某种方式动态设置 7 以确定哪一年的月份