功率查询-M语言-将列转换为行

时间:2018-06-23 16:47:52

标签: powerbi powerquery m

我有一个电子表格,其中包含“名称”列作为产品名称,数量,成本。

我想将其转换为包含产品名称,数量,成本的数据行。

有关我想要的内容,请参见下图。

enter image description here

使用Power Query M语言处理此问题的最佳方法是什么?

不确定我是否只想透视具有商品名称,数量和成本的列?

谢谢

2 个答案:

答案 0 :(得分:0)

这是 A 的方式...

从此表开始,如表1:

enter image description here

您可以选择“客户”列和“取消其他列”来获取此信息:

enter image description here

然后,您可以添加一个索引列(将其保留为Index),然后添加一个自定义列(将其保留为Custom),并以if Text.EndsWith([Attribute],"Cost") then 1 else 0作为公式来获取:

enter image description here

然后添加另一个自定义列...将其命名为Total Cost并输入#"Unpivoted Other Columns"[Value]{[Index]+(List.Count(#"Added Custom"[Custom])/List.Sum(#"Added Custom"[Custom]))}作为其公式以获取:

enter image description here

上面的两个步骤是,首先根据“值”列中“成本”的位置来设置相应的T恤成本,然后实际定位成本并将其记录在与相应成本相同的行上T恤衫。 ``索引''列提供行定位信息,而``自定义''列提供计数信息-整个列表计数和带有成本的行数。我使用计数信息来确定要在“值”列中向下移动多少索引位置,以动态获取关联的成本值。

然后使用“文本过滤器”>“不以...结尾”在“属性”列上过滤,然后键入单词Cost。所有带有“成本”一词的“属性”条目的行均应消失:

enter image description here

删除“索引”和“自定义”列,并将“属性”和“值”列分别重命名为“产品名称”和“数量”,以获得最终结果:

enter image description here

这是我的M代码:

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Customer"}, "Attribute", "Value"),
#"Added Index" = Table.AddIndexColumn(#"Unpivoted Other Columns", "Index", 0, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if Text.EndsWith([Attribute],"Cost") then 1 else 0),
#"Added Custom2" = Table.AddColumn(#"Added Custom", "Total Cost", each #"Unpivoted Other Columns"[Value]{[Index]+(List.Count(#"Added Custom"[Custom])/List.Sum(#"Added Custom"[Custom]))}),
#"Filtered Rows" = Table.SelectRows(#"Added Custom2", each not Text.EndsWith([Attribute], "Cost")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index", "Custom"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Attribute", "Product Name"}, {"Value", "Quantity"}})
in
#"Renamed Columns"

答案 1 :(得分:0)

这里的关键是旋转和不旋转。


从这样的表开始,

Start

选择右边的四列,然后单击“变换”>“取消透视列”以获取此表:

Unpivoted

现在创建一个自定义列,使用此公式对值进行分类。

if Text.EndsWith([Attribute], "Cost") then "Cost" else "Quantity"

我还砍掉了" Cost"列末尾的Attribute部分。您可以变换>替换值并将" Cost"替换为空白,或者变换>提取>分隔符之前的文本" Cost"

Custom Column

现在旋转自定义列(选择Value列作为“值列”选项),最后,将Attribute列重命名为Product Name

Pivoted


这是我所有步骤的M代码:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcknNK0stUnAuLS7Jz00tUtJRMjIGEoYmIJapqZ6pAYhnZKpnYKAUqxOt5JyRmZyYno+swdAQSJiagtUZgNQBeeYQDbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Customer = _t, #"Product Orange T-shirt" = _t, #"Product Blue T-shirt" = _t, #"Product Orange T-shirt Cost" = _t, #"Product Blue T-shirt Cost" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Product Orange T-shirt", Int64.Type}, {"Product Blue T-shirt", Int64.Type}, {"Product Orange T-shirt Cost", type number}, {"Product Blue T-shirt Cost", type number}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Customer"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Custom", each if Text.EndsWith([Attribute], "Cost") then "Cost" else "Quantity"),
    #"Replaced Value" = Table.ReplaceValue(#"Added Custom"," Cost","",Replacer.ReplaceText,{"Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Replaced Value", List.Distinct(#"Replaced Value"[Custom]), "Custom", "Value", List.Sum),
    #"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"Attribute", "Product Name"}})
in
    #"Renamed Columns"