在Power Query中组合列而不拆分为多个查询并附加

时间:2017-03-24 23:23:17

标签: excel powerbi powerquery m

我有一些报告说我正在处理电源查询,我有一个解决方案,但我希望这里的向导有更好的方法。

在下面的示例中,我当前的方法是拆分为3个查询,仅连接,具有名称和记录编号,以及单个项目和值(删除.1,.2,.3以便标题记录是相同的)然后我会将它们一起追加到一个查询中,转动数据,然后加载到表中。对于某些文件,我会多次这样做,并且为了回收查询,我必须分别复制每个文件(我不善于调用函数)。有没有更好的方法在一个查询中执行此操作,最好通过界面,以便我可以与比我更新的队友分享?我知道分组功能有潜力,但我还不擅长使用这些工具。请参阅下面的带有示例表的Google工作表:

https://docs.google.com/spreadsheets/d/14f-7GjUMwwzcUj9sAFBxaPjLnOW_1hKBYPtelRHfr70/edit?usp=sharing

2 个答案:

答案 0 :(得分:1)

有趣的问题。经过几次试验后,我设法在一个查询中完成了这项工作。

查询:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY49D4MgEIb/C7MLYL9m07GNadwMw9UwmKLXACb67z1sbaAMR9477rk8bcuafmAF45zTW+ux6w0FSVWB6SYDHm34p2r07J+IL4pHpooUfUxG74sVDm9wjpIInYUFRwrnDLqj19+LYfNqwW1Hyo/LNg7MDewSGiFiyfJfUqSSp5zdLUViKWPLS05FmjLWPPw0JVNqBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name = _t, Record = _t, Item.1 = _t, Value.1 = _t, Item.2 = _t, Value.2 = _t, Item.3 = _t, Value.3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Record", Int64.Type}, {"Item.1", type text}, {"Value.1", Int64.Type}, {"Item.2", type text}, {"Value.2", Int64.Type}, {"Item.3", type text}, {"Value.3", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name", "Record", "Value.1", "Value.2", "Value.3"}, "Attribute", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "ItemKey"}, {"Value", "Item"}}),
    #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Renamed Columns", {"Name", "Record", "ItemKey", "Item"}, "Attribute", "Value"),
    #"Renamed Columns1" = Table.RenameColumns(#"Unpivoted Columns1",{{"Attribute", "ValueKey"}}),
    #"Filtered Rows" = Table.SelectRows(#"Renamed Columns1", each Text.EndsWith([ItemKey], Text.End([ValueKey], 1))),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"ItemKey", "ValueKey"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Item]), "Item", "Value", List.Sum)
in
    #"Pivoted Column"

结果:

result

一点解释:

  • 在所有项目列上取消透视
  • 取消所有Value列的透明度 (所以这里它将创建n x n行记录)
  • 过滤ItemKey与ValueKey匹配的记录(例如Item.1 = Value.1等)
  • 删除ItemKey和ValueKey列
  • 将项目列上的数据透视为标题,其值为值

不是最好的解决方案,因为它创建了额外的记录,但更少涉及手工工作,这应该解决您的问题。

P.S。 #“过滤行”涉及一些内置功能,但在UI中不可用,如果您的实际数据对每条记录具有超过9个项目 - 值对,则可能需要对其进行自定义。 (因为我只是比较键的最后一个字符)

答案 1 :(得分:0)

我是怎么看的:

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

//Add index for each row of source column. Use Add Index Column
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1), 
//Unpivot table 
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index", "Name", "Record"}, "Attribute", "Value"),

//At this step I corrected default column names in formula editor. 
// Also, it is important to have only 1 dot in column names. 
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns","Attribute",Splitter.SplitTextByEachDelimiter({"."}, QuoteStyle.Csv, false),{"ColumnName", "ColumnIndex"}), 
//As a result we get old column names clean of numbers in one column, and index of each column in another

//Next we combine row index and column index in order to generate identifier for new row. Add custom column, write Text.From([Index]) & Text.From([ColumnIndex]) in formula window
    #"Added Custom" = Table.AddColumn(#"Split Column by Delimiter", "Idx", each Text.From([Index]) & Text.From([ColumnIndex])), 

//some cleanup
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Idx", "Name", "Record", "ColumnName", "Value"}), 

// Pivot columns back. Without row identifier this won't work! 
    #"Pivoted Column" = Table.Pivot(#"Removed Other Columns", {"Item", "Value"}, "ColumnName", "Value"), //Step on ColumnNames, select Pivot, Value as values column, Don't Aggregate as function.
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Idx"}),
//Finally, Pivot to the desired look. 
    #"Pivoted Column1" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Item]), "Item", "Value", List.Sum) //Step on Item, select Pivot, Value as values column, Sum or Don't Aggregate as function.
in
    #"Pivoted Column1"

这将需要在步骤&#34处添加一些公式;添加自定义"。

这适用于任何合理数量的item.x-value.x列对。