简体   繁体   English

Excel 电源查询; 将表扩展为列

[英]Excel Power Query; Expand table into columns

I have two tables that I'd like to merge, but I can't quite get the merge to work the way I'd like it to.我有两个要合并的表,但我无法完全按照我希望的方式进行合并。 The first is a list of days and some data, the second is a ranking of items sold that day along with the number sold, etc. I'd like to get the top 'x' items added as columns to my daily data.第一个是日期列表和一些数据,第二个是当天售出商品的排名以及售出数量等。我想将前“x”个商品作为列添加到我的日常数据中。 I got the merge to work, and so now I have a column that says 'table.'我让合并开始工作,所以现在我有一个列显示“表格”。 However if I expand the table, each item turns into its own row.但是,如果我展开表格,每个项目都会变成它自己的行。 Instead I'd like table item 2 to repeat all of its columns with a postfix.相反,我希望表项 2 用后缀重复它的所有列。

date日期 total_sales总销售额 items项目
1/1/21 1/1/21 $50000 50000 美元 table桌子
1/2/21 1/2/21 $40000 $40000 table桌子
date日期 total_sales总销售额 items_1项目_1 units_1单位_1 items_2项目_2 units_2单位_2
1/1/21 1/1/21 $50000 50000 美元 pens钢笔 15 15 pencils铅笔 10 10
1/2/21 1/2/21 $40000 $40000 erasers橡皮擦 35 35 pens钢笔 5 5个

etc.等等

I can do this with visual basic but I don't think that's the best way to go about it.我可以用 visual basic 做到这一点,但我认为这不是 go 的最佳方式。 Thanks for your help, Also?谢谢你的帮助,还有吗? is there a specific term for this operation that I could have searched for?我可以搜索此操作的特定术语吗?

Let's suppose your second table looks like this:假设您的第二个表如下所示:

数据表

First, let's group by date where for each group we sort and index the subtable.首先,让我们按日期分组,对于每个组,我们对子表进行排序和索引。 (This is the hardest step.) (这是最难的一步。)

按日期分组

Once we have the index for each group, expand that column back so we're back to the start except with the new index column.一旦我们有了每个组的索引,就将该列展开回来,这样我们就回到了开始,除了新的索引列。

原加指数

From here, you can filter the index to get only the top N and then unpivot the [items] and [units] columns.从这里,您可以过滤索引以仅获取前 N 个,然后取消旋转 [items] 和 [units] 列。

反透视

Merge the [index] and [Attribute]合并 [index] 和 [Attribute]

合并列

and then pivot on [Merged]然后是 [Merged] 上的 pivot

枢

Ta da.达达。 Now your data is shaped so that you can easily merge it with the first table.现在您的数据已成型,以便您可以轻松地将其与第一个表合并。


Here's the full code for this example.这是此示例的完整代码。 (You can paste this into the Advanced Editor and look through Applied Steps.) (您可以将其粘贴到高级编辑器中并查看应用步骤。)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJU0lEqSM0rBlKGpkqxOugSyZk5YDkDqJwRTC61KLE4tQgkZ2yKLgc1EMM8hB5sWqBWGSnFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, items = _t, units = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"items", type text}, {"units", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"date"}, {{"indexed", each Table.AddIndexColumn(Table.Buffer(Table.Sort(_, {{"units", Order.Descending}})), "Index", 1, 1, Int64.Type), type table}}),
    #"Expanded indexed" = Table.ExpandTableColumn(#"Grouped Rows", "indexed", {"items", "units", "Index"}, {"items", "units", "Index"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded indexed", each [Index] < 3),
    #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Filtered Rows", {"date", "Index"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Unpivoted Columns1", {{"Index", type text}}, "en-US"),{"Attribute", "Index"},Combiner.CombineTextByDelimiter("_", QuoteStyle.None),"Merged"),
    #"Pivoted Column1" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Merged]), "Merged", "Value"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column1",{{"items_1", type text}, {"units_1", Int64.Type}, {"items_2", type text}, {"units_2", Int64.Type}})
in
    #"Changed Type1"

The only place I'm doing special M language magic (not just clicking stuff in the GUI) is this step which I created by applying a couple of steps to one of the sub-tables and pasting the GUI-generated code back into the group by step (plus Table.Buffer wrapper to make sure the sorting "sticks").我做特殊 M 语言魔术的唯一地方(不仅仅是单击 GUI 中的东西)是我通过对其中一个子表应用几个步骤并将 GUI 生成的代码粘贴回组中创建的这一步一步一步(加上 Table.Buffer 包装器以确保排序“坚持”)。

#"Grouped Rows" =
    Table.Group(
        #"Changed Type",
        {"date"},
        {
            {"indexed", each
                Table.AddIndexColumn(
                    Table.Buffer(
                        Table.Sort(_, {{"units", Order.Descending}})
                    ),
                    "Index", 1, 1, Int64.Type
                ), type table
            }
        }
    ),

I think this video can solve your case very well.我认为这个视频可以很好地解决你的问题。

Concatenate, Merge or Combine multiple rows into one value - Power Query for Power BI将多行连接、合并或组合成一个值 - Power Query for Power BI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM