【问题标题】:Changing structure of table depending on one colulmn in PowerQuery根据 Power Query 中的一列更改表的结构
【发布时间】:2021-07-13 00:41:55
【问题描述】:

我最近在处理表的 Power Query 转换问题。我一直在尝试来自 Internet 的不同提示,并且由于旋转,我已经接近了,但由于我缺乏与 PowerQuery 工具相关的知识,我不得不放弃。 所以我有一个表格,其中第一列讲述了给定操作的计划日期和实际日期。它看起来像这样:

当前表的结构

我想重组表格以删除第一个表格并添加到标题“操作 1 计划”、“操作 1 实际”等等。所以我的愿望是完成以下情况:

所需表的结构

我一直在尝试不同的事情,但我仍然无法做到这一点。我希望有人能给我一些关于如何完成这项工作的提示。如果有任何建议,我将不胜感激。

【问题讨论】:

    标签: excel pivot powerquery transformation divide


    【解决方案1】:

    经常遇到此类问题,您必须先unpivotgroup,然后才能获得所需的数据透视结果。

    M 码

    let
        Source = Excel.CurrentWorkbook(){[Name="Table6"]}[Content],
    
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Type of Date", "Project"}, "Attribute", "Value"),
    
    //create the New column headers by merging
        #"Inserted Merged Column" = Table.AddColumn(#"Unpivoted Other Columns", "Op Type", each Text.Combine({[Attribute], [Type of Date]}, " "), type text),
        #"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Type of Date", "Attribute"}),
    
    //Group by "Project" and Pivot each grouped table
        #"Grouped Rows" = Table.Group(#"Removed Columns", {"Project"}, {
            {"Pivot", each Table.Pivot(_, List.Distinct(_[#"Op Type"]), "Op Type", "Value")}
            }),
    
    //remove unneeded columns and expand the Pivoted column
        #"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"Project"}),
        #"Expanded Pivot" = Table.ExpandTableColumn(#"Removed Columns1", "Pivot", 
            {"Project", "Operation 1 Planned", "Operation 2 Planned", "Operation 3 Planned", "Operation 4 Planned", "Operation 1 Actual", "Operation 2 Actual", "Operation 3 Actual", "Operation 4 Actual"}, {"Project", "Operation 1 Planned", "Operation 2 Planned", "Operation 3 Planned", "Operation 4 Planned", "Operation 1 Actual", "Operation 2 Actual", "Operation 3 Actual", "Operation 4 Actual"}),
    
        //type the date columns
        dateCols = List.RemoveFirstN(Table.ColumnNames(#"Expanded Pivot"),1),
        dateTypes = List.Zip({dateCols,List.Repeat({type date}, List.Count(dateCols))}),
        typed = Table.TransformColumnTypes(#"Expanded Pivot",dateTypes)
    in
        typed
    

    数据

    结果

    编辑 我注意到结果表中的列名顺序不是您在示例中显示的。我在代码中添加了几行以使顺序正确。

    试试这个代码:

    let
        Source = Excel.CurrentWorkbook(){[Name="Table6"]}[Content],
    
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Type of Date", "Project"}, "Attribute", "Value"),
    
    //create the New column headers by merging
        #"Inserted Merged Column" = Table.AddColumn(#"Unpivoted Other Columns", "Op Type", each Text.Combine({[Attribute], [Type of Date]}, " "), type text),
        #"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Type of Date", "Attribute"}),
        //Group by "Project" and Pivot each grouped table
    
    //create list of the Pivot Column Names in the desired order
    //Extract a unique list of the names, then sort them => {op1Act, op1Plan, op2Act, op2Plan, ...}
    //Split the list (List.Alternate) taking every other 
    //    starting with either the first or second entry => {op1Act,op2Act,...}, and {op1Plan,op2Plan...}
    //Zip those two lists together with the second list first; then Combine them into a single list
    pivotColNames = List.Combine(
                        List.Zip({
                            List.Alternate(List.Sort(List.Distinct(#"Removed Columns"[Op Type])),1,1),
                            List.Alternate(List.Sort(List.Distinct(#"Removed Columns"[Op Type])),1,1,1)
                                })),
    
        #"Grouped Rows" = Table.Group(#"Removed Columns", {"Project"}, {
            {"Pivot", each Table.Pivot(_, pivotColNames, "Op Type", "Value")}
            }),
    
    //remove unneeded columns and expand the Pivoted column
        #"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"Project"}),
    
    //create column name list for the expanded pivot table, in the desired order
    x = List.Combine({{"Project"},pivotColNames}),
        #"Expanded Pivot" = Table.ExpandTableColumn(#"Removed Columns1", "Pivot", 
            x,x),
    
        //type the date columns
        dateCols = List.RemoveFirstN(Table.ColumnNames(#"Expanded Pivot"),1),
        dateTypes = List.Zip({dateCols,List.Repeat({type date}, List.Count(dateCols))}),
        typed = Table.TransformColumnTypes(#"Expanded Pivot",dateTypes)
    in
        typed
    

    新代码的结果

    【讨论】:

    • 这正是我想要的!非常感谢。我还有一个要求。您能否给我一个提示,我可以在哪里获得有关如何进行此转换的知识?我还有一张桌子要转换,所以我正在尝试按照您的步骤进行操作。不知何故,我正在挣扎。所以我想为自己的未来了解这些根本性的转变:)
    • @miasekk 我发现 MS 文档有限。我在线阅读了各种博客、入门书等;以及其他人的反向工程答案。我认为通过一些在线资源你会收获很多,但我不够熟悉,不能推荐比一般搜索更多的东西;而且我认为任何单一资源都不会为您提供所需的一切。我的主要学习方法是解决别人的问题,但这是一个缓慢的方法。
    • @miasekk 我注意到结果表中的列顺序与您在示例中显示的不同。我在代码中添加了几行以使顺序正确。
    • 感谢您的额外关心。我之前是手动完成的,但这不是问题,而我的主要问题对我来说是一个巨大的挑战,所以我非常感谢您的回复,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多