【问题标题】:Power Query create PIVOT but each Pivot column should have their "sub-column"Power Query 创建 PIVOT 但每个 Pivot 列都应该有它们的“子列”
【发布时间】:2023-02-02 21:28:48
【问题描述】:



标题解释:我有一个 txt 文件,我将它读入 excel,创建一个 Query 来编辑它。我的 txt 文件是shop.txt,包含以下内容 (*1):

Material nr Shop Trouser Size Trouser Color Available amount Gender
621077453 Target XXL Black 20 Male
621077453 Tesco XXL Black 12 Male
623989211 Target S Black 22 Male
630136378 Lidl L Black 21 Female
678048334 Target M Red 33 Female
678048334 Tesco M Red 15 Female
680603975 Target S White 30 Female
680603975 Tesco S White 20 Female
680603975 Lidl S White 33 Female
688820171 Target XS Blue 32 Male
688820171 Lidl XS Blue 15 Male

然后我从 range("A:F") 创建查询,删除空值,并创建一个自身的旋转版本:

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Material nr.", Int64.Type}, {"Shop", type text}, {"Trouser Size", type text}, {"Trouser Color", type text}, {"Available amount", Int64.Type}, {"Gender", type text}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([#"Material nr."] <> null)),
    #"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Shop]), "Shop", "Material nr.", List.Sum),
    #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"Target", "Tesco", "Lidl", "Trouser Size", "Trouser Color", "Available amount", "Gender"})
in
    #"Reordered Columns"


它把我带到this
我需要尽可能多的可用不同商店的列。但要注意的是:我需要列出所有材料只有一次,并且每个额外的数据(大小、颜色...)都应该与枢轴的名称一起出现。手动命名它根本不是问题。如果我以我知道的唯一方式这样做,材料的附加数据就会被丢弃(所以我最终只剩下 |Material nr| 和 |Shop| 可以使用)。

我正在寻找的表格类似于this



这甚至可以以某种方式实现吗?对我来说它看起来很乱。我几乎可以肯定可以在 VBS 中创建目标表,但我正在尝试使用 Power Query 做得更好。

先感谢您!

【问题讨论】:

    标签: excel powerquery


    【解决方案1】:

    在 Powerquery 中尝试此代码。您取消旋转,合并两列,然后旋转回来。当其中有数据时,性别会显示为一列。

    let  Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Shop", "Material nr"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(#"Unpivoted Other Columns",{"Shop", "Attribute"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Header"),
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Header]), "Header", "Value")
    in  #"Pivoted Column"
    

    请注意,如果您愿意,也可以直接读入文本文件,而无需先将数据加载到 Excel 表格中

    let Source = Table.FromColumns({Lines.FromBinary(File.Contents("C:Temp.txt"), null, null, 1252)}),
    #"Split Column by Delimiter" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByDelimiter("___", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3", "Column1.4", "Column1.5", "Column1.6", "Column1.7"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Split Column by Delimiter", [PromoteAllScalars=true]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Shop", "Material nr"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(#"Unpivoted Other Columns",{"Shop", "Attribute"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Header"),
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Header]), "Header", "Value")
    in  #"Pivoted Column"
    

    【讨论】:

    • 好吧,我不知道你是怎么这么快解决的,我什至无法理解第一段文字,为我显示错误,但第二段确实运行良好,谢谢!
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多