Powerquery 透视和逆透视具有不同内容(标题)的列

问题描述 投票:0回答:2

我需要清理一份包含 45.000 行格式错误的销售数据的报告,如下所示:

第 1 栏 第 2 栏 第3栏 第 4 栏
00/01 产品A 产品B
00/01 1 5 5
00/01 2 5 5
00/02 产品A 产品B
00/02 1 5 5
00/02 2 5 5
00/03 产品C 产品B
00/03 2 5 5

问题:

  • 我们有多个标题行以不一致的间距重复(因为有时某些商店缺少销售周),因此固定间隔的 Table.Split 不起作用
  • 当商店未列出产品时,很简单,报告中不会输出,因此商店 00/03 是唯一具有 ProductC 的商店

我需要取消透视 Col3(实际上所有产品列以某种方式到达我可以使用的正确表格:

店铺编号 产品A 产品B 产品C
00/01 1 5 5
00/01 2 5 5
00/02 1 5 5
00/02 2 5 5
00/03 2 5 5

我进行了广泛的研究,但要么对 PowerQuery M 不够了解和/或不知道如何调用这个问题来找到正确的解决方案。 注意:真实的表有88列,这样有各种混合产品(总共大约有30种不同的产品)

excel powerbi powerquery powerbi-desktop m
2个回答
3
投票

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDQNzBUUNJRCk9NzQbRAUX5KaXJJY5IbCelWB0klWDCFEygShhhkTAi2mwjXGYb4TLbGIvZzljNNsYwIhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Col1 " = _t, #"Col2 " = _t, #"Col3 " = _t, Col4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1 ", type text}, {"Col2 ", type text}, {"Col3 ", type text}, {"Col4", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Col1 "}, {{"All", each _, type table [#"Col1 "=nullable text, #"Col2 "=nullable text, #"Col3 "=nullable text, Col4=nullable text]}}),
    Custom1 =  Table.TransformColumns(#"Grouped Rows", {{"All", each Table.PromoteHeaders(_) }}),
    #"Expanded All" = Table.ExpandTableColumn(Custom1, "All", {"Week ", "ProductA ", "ProductB", "ProductC "}, {"Week ", "ProductA ", "ProductB", "ProductC "})
in
    #"Expanded All"

3
投票

注意:使用大卫的答案作为更好的答案

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Base=({"Col1","Col2"}), //names of columns to keep
repeating_groups=1,  // then stack repeating groups of xx columns
Combo = List.Transform(List.Split(List.Difference(Table.ColumnNames(Source),Base),repeating_groups), each Base & _),
#"Added Custom" =List.Accumulate(
        Combo, 
        #table({"Column1"}, {}),
        (state,current)=> state & Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, current)),1)
),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each if [Column2]="Week" then [Column3] else null),
#"Filled Down" = Table.FillDown(#"Added Custom1",{"Custom"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Column2] <> "Week")),
#"Pivoted Column1" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Custom]), "Custom", "Column3", List.Sum),
#"Renamed Columns1" = Table.RenameColumns(#"Pivoted Column1",{{"Column2", "Week"}, {"Column1", "StoreNo"}})
in  #"Renamed Columns1"

© www.soinside.com 2019 - 2024. All rights reserved.