嵌套表的转换

时间:2017-05-31 10:52:57

标签: powerquery

我有一个产品表,每个国家都有嵌套表(由产品代码加入)。每个嵌套表包含随机行数,从无行到2-3-4。

我需要转换它,以便嵌套表格中的所有行都在国家/地区列下展开,一个接一个,产品名称和密钥不重复。

如果列的行数少于其他列,则未使用的单元格将填充空值。

以下是示例(它只有一列):

let
    Source = Table.FromRecords({
[Product = "Product 1", Key = 1, Coutry 1 = Table.FromRecords({[Key = 1, LocalName = "ProductLocalName1", Date1 = #date(2016,1,29), Date2 = #date(2020,1,29)]})],
[Product = "Product 2", Key = 2, Coutry 1 = Table.FromRecords({[Key = 2, LocalName = "ProductLocalName2", Date1 = #date(2016,11,12), Date2 = #date(2020,11,12)]})]
}),
    Output = Table.FromRecords({
[Product = "Product 1", Key = 1, Country 1 = "ProductLocalName1"], 
[Product = "", Key = "", Country 1 = #date(2016,1,29)], 
[Product = "", Key = "", Country 1 = #date(2020,1,29)],
[Product = "Product 2", Key = 2, Country 1 = "ProductLocalName2"], 
[Product = "", Key = "", Country 1 = #date(2016,11,12)], 
[Product = "", Key = "", Country 1 = #date(2020,11,12)]
})
in
    Output

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

这对你有用。请注意,我在每个嵌套表中添加了另一条记录。

let
    Source = Table.FromRecords({
        [Product = "Product 1", Key = 1, Coutry 1 = Table.FromRecords({[Key = 1, LocalName = "ProductLocalName1a", Date1 = #date(2016,1,29), Date2 = #date(2020,1,29)],
                                                                       [Key = 1, LocalName = "ProductLocalName1b", Date1 = #date(2016,2,29), Date2 = #date(2020,2,29)]})],
        [Product = "Product 2", Key = 2, Coutry 1 = Table.FromRecords({[Key = 2, LocalName = "ProductLocalName2a", Date1 = #date(2016,11,11), Date2 = #date(2020,11,11)],
                                                                       [Key = 2, LocalName = "ProductLocalName2b", Date1 = #date(2016,11,12), Date2 = #date(2020,11,12)]})]
        }),

    // Transform the nested tables in a table with all current values in 1 column and a zero based index in another column (will be used for merging later on)
    TransformTable = Table.TransformColumns(Source, {{"Coutry 1", each
    let 
        Custom1 = List.Combine(Table.ToRows(_)),
        Custom2 = Table.FromColumns({Custom1},{"Country 1"}),
        #"Added Index1" = Table.AddIndexColumn(Custom2, "NestedIndex", 0, 1)
    in
        #"Added Index1"}}),

    // Create a separate table that only contains the expanded nested tables:
    Select = Table.SelectColumns(TransformTable,{"Coutry 1"}),
    Expanded = Table.ExpandTableColumn(Select, "Coutry 1", {"Country 1", "NestedIndex"}, {"Country 1", "NestedIndex"}),

    // Now get back to the original table, remove the nested tables and add a column with zeroes in order to merge with "Expanded"
    Removed = Table.RemoveColumns(Source,{"Coutry 1"}),
    AddedZeroes = Table.AddColumn(Removed, "Zeroes", each 0),

    // Now merge the 2 together, based on key and zeroes/nestedindex, with joinkinf RightOuter
    Merged = Table.NestedJoin(AddedZeroes,{"Key", "Zeroes"},Expanded,{"Country 1", "NestedIndex"},"NewColumn",JoinKind.RightOuter),
    ExpandedCountry1 = Table.ExpandTableColumn(Merged, "NewColumn", {"Country 1"}, {"Country 1"}),
    RemovedZeroes = Table.RemoveColumns(ExpandedCountry1,{"Zeroes"})
in
    RemovedZeroes