电源查询转置表

时间:2016-05-16 11:04:15

标签: excel-2013 transpose powerquery

需要帮助才能获得所需的输出,如下所示。我正在使用带有Power Query的Microsoft Excel 2013。请帮忙。谢谢!

输入文字(test.txt)

Menu1:1>1
Menu2:1>2
Menu3:1>3
Menu1:2>1
Menu2:2>2
Menu3:2>3
Menu1:3>1
Menu2:3>2
Menu3:3>3
Menu4:3>4
Menu1:4>1
Menu2:4>2
Menu3:4>3
Menu1:5>1
Menu2:5>2
Menu3:5>3
Menu1:6>1
Menu2:6>2
Menu3:6>3
Menu4:6>4

电源查询

let
    Source = Csv.Document(File.Contents("C:\test.txt"),[Delimiter=":", Columns=2, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column1", "Type"}, {"Column2", "Value"}}),
    GroupedRows = Table.Group(#"Renamed Columns", {"Type"}, {{"DistinctValues", each _[Value]}}),
    Output = Table.FromColumns(GroupedRows[DistinctValues], GroupedRows[Type])
in
    Output

输出

Menu1   Menu2   Menu3   Menu4
1>1     1>2     1>3     3>4
2>1     2>2     2>3     6>4
3>1     3>2     3>3 
4>1     4>2     4>3 
5>1     5>2     5>3 
6>1     6>2     6>3 

欲望输出

Menu1   Menu2   Menu3   Menu4
1>1     1>2     1>3     
2>1     2>2     2>3     
3>1     3>2     3>3     3>4
4>1     4>2     4>3 
5>1     5>2     5>3 
6>1     6>2     6>3     6>4

4 个答案:

答案 0 :(得分:2)

Ehren-MST的Technet解决方案。接受任何输入值。

let
        Source = Csv.Document(File.Contents("C:\test.txt"),[Delimiter=":", Columns=2, Encoding=1252, QuoteStyle=QuoteStyle.None]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Column1] = "Menu1" then [Index] else null),
        #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
        #"Removed Columns" = Table.RemoveColumns(#"Filled Down",{"Index"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Index"}}),
        #"Pivoted Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[Column1]), "Column1", "Column2"),
        #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
     in
         #"Removed Columns1"

仅为其他数据更改此代码;

 #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Column1] = "Menu1" then [Index] else null),
  

"菜单1"需要进行更改以适合您的数据。 tq All ..

答案 1 :(得分:0)

您必须使用pivot功能,但首先拆分column2,以便将枢轴作为所需的输出。

let
    Source = Csv.Document(File.Contents("C:\test.txt"),[Delimiter=":", Columns=2, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column1", "Type"}, {"Column2", "Value"}}),

//new part
    #"Duplicated Column" = Table.DuplicateColumn(#"Renamed Columns", "Column2", "Column2 - Copy"),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Duplicated Column","Column2 - Copy",Splitter.SplitTextByEachDelimiter({">"}, QuoteStyle.Csv, false),{"Column2 - Copy.1", "Column2 - Copy.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter1",{"Column2 - Copy.2"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Column1]), "Column1", "Column2"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Column2 - Copy.1"})
in
    #"Removed Columns1"

答案 2 :(得分:0)

Pay attention to the WeChat Public Number: Excel实战分享

let
    Source = Csv.Document(Binary.Buffer(File.Contents("C:\test.txt")),2,":"),
    pos=(n)=>try List.PositionOf(List.Distinct(Source[Column1]),Source[Column1]{n}) otherwise 1/0,
    tblTemp = Table.TransformColumns(Table.AddIndexColumn(Source, "newCol", 0, 1),{"newCol",each pos(_)-pos(_-1)}),
    tblRes = Table.Combine(Table.Group(tblTemp,"newCol",{"N",each #table([Column1],{[Column2]})},0,(x,y)=>Number.From(y<0))[N])
in
    tblRes

答案 3 :(得分:0)

您可以在分组表中转换所需的格式,这可能并不容易。

let

Source = Csv.Document(File.Contents("c:\testdata.txt"), [Delimiter=":", Columns=2, Encoding=1252, QuoteStyle=QuoteStyle.None]),
GroupedRows = Table.Group(
                  Source, 
                  {"Column1"}, 
                  {"All", each Table.FromRows({[Column2]}, List.Transform([Column2], each Text.BeforeDelimiter(_, ">")))}
              ),
ExpandTable = Table.ExpandTableColumn(GroupedRows, "All", {"1", "2", "3", "4", "5", "6"}, {"1", "2", "3", "4", "5", "6"}),
Transpose = Table.PromoteHeaders(Table.Transpose(ExpandTable)) 

in Transpose