嵌套表的转换

Transformation of nested tables

我有一个产品 table,每个国家/地区都有嵌套的 table(通过产品代码连接)。每个嵌套 table 包含随机行数,从根本没有行到大约 2-3-4。

我需要对其进行转换,以便将嵌套的 table 中的所有行都展开到国家/地区的列下,一个接一个,并且产品名称和密钥不重复。

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

示例如下(只有一列):

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

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

这应该适合你。请注意,我向每个嵌套表添加了另一条记录。

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