Excel、Power Query:将查询 2 中的多列添加到查询 1 中

问题描述 投票:0回答:1

我的问题的延续:Excel,查询:如何通过查找将查询 2 中的特定信息添加到查询 1 中

我有 2 个查询,“概述”和“数据”。我想将“概述”查询中“数据”的信息添加到正确的位置。 ITEMITEM 2Y/N 列的信息需要添加到正确的行,由 NAME.1 和 NAME.2 中的值确定。

这是两个查询。 (与我上一个问题略有不同)

概述

名称.1 名称.2 数字 随机
姓名11 243324 qwsa
姓名22 6747 dsfsdf
姓名13 455 yyu
姓名14 908098 hfn
姓名25 34 呃特
132 uil
姓名17 姓名27 64 tgvc
姓名18 姓名28 876 iorts

数据

项目 第 2 项 是/否 名称.1 名称.2
123123 AA 姓名25
234324 BB 姓名11
345345 抄送 N 姓名17
456456 AA 姓名12 姓名22
567567 AA N
678678 DD N 姓名13 姓名23
789789 DD 姓名16
890890 AA N 姓名14

我想添加信息 ITEMITEM 2Y/N 从查询“数据”中查询正确行上的“概述”。因此,“概述”的第一行将是:

名称.1 名称.2 数字 随机 项目 第 2 项 是/否
姓名11 243324 qwsa 234324 BB

我在上一个问题中从 Ron 那里得到的代码是:

let
    Source = Excel.CurrentWorkbook(){[Name="Overview3"]}[Content],
    #"Overview" = Table.TransformColumnTypes(Source,{
        {"NAME.1", type text}, {"NAME.2", type text}, {"NUMBER", Int64.Type}, {"RANDOM", type text}}),
  
//Add index column to be able to go back to original table order
    #"Added Index" = Table.AddIndexColumn(Overview, "Index", 0, 1, Int64.Type),

//Create a Name column that we will expand to all names in a single column
    #"Added Custom" = Table.AddColumn(#"Added Index", "Name", each List.RemoveNulls({[NAME.1]} & {[NAME.2]})),
    #"Expanded Name" = Table.ExpandListColumn(#"Added Custom", "Name"),

//Since Data table could also have Names in both NAME columns, we can unpivot (and delete the unneeded columns)
    #"Unpivot Data" = Table.UnpivotOtherColumns(Table.RemoveColumns(Data3,"Y/N"),{"ITEM"},"Attribute","Name"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivot Data",{"Attribute"}),

//Now we can merge the tables and return the ITEM
    #"Add Item" = Table.TransformColumns(
        Table.NestedJoin(#"Expanded Name","Name",#"Removed Columns","Name","ITEM",JoinKind.LeftOuter),
        {"ITEM", each try [ITEM]{0} otherwise null, type text}),
    #"Removed Columns1" = Table.RemoveColumns(#"Add Item",{"Name"}),

//Group by Index to return initial table in order
    #"Grouped Rows" = Table.Group(#"Removed Columns1", {"Index"}, {

    //If two names in the same row of Overview have different ITEMs, we will returned a concatenated string
        {"All", each  Table.Distinct(Table.TransformColumns(_, {"ITEM", (c)=>Text.Combine(_[ITEM], ", ")}))  , 
        type table [NAME.1=nullable text, NAME.2=nullable text, NUMBER=nullable number, RANDOM=nullable text, Index=number, ITEM=text]}}),


    #"Removed Columns2" = Table.RemoveColumns(#"Grouped Rows",{"Index"}),
    #"Expanded All" = Table.ExpandTableColumn(#"Removed Columns2", "All", {"NAME.1", "NAME.2", "NUMBER", "RANDOM", "Index", "ITEM"}),
    #"Removed Columns3" = Table.RemoveColumns(#"Expanded All",{"Index"})
in
    #"Removed Columns3"

这对这个问题有用。但是,当我尝试以高效/干净的方式添加更多列时,我无法使其工作。 (一次添加所有列,而不是重复代码3次。到目前为止,重复代码3次还不起作用,但我认为我在某个地方犯了一个小错误。但是,我认为应该可以直接这样做一次 3 列。在我想要使用它的实际文件中,我需要添加多个列。因此,更有效的方法会更好。无论是对于使用还是对于其他人(当有人想要使用/编辑我的文件时)


回复评论 Ron Rosenfield 的其他信息:

当我在实际文件中使用此代码时,它会一直工作到

#"Expanded Fields"
。然而,它在
#"Grouped Rows"
处给出了错误。它说:
Expression.SyntaxError: Invalid identifier.
并且它指向 [Sub-供应商]

    #"Expanded Fields" = Table.ExpandRecordColumn(#"Removed Columns1", "Fields", {"Report Number", "Sub-supplier", "text text text text.", "text text text", "text text text text text", "text text"}),
    #"Grouped Rows" = Table.Group(#"Expanded Fields", {"Index"}, {{"all", 
        each Table.TransformColumns(_, {
            {"Report Number", (c)=>Text.Combine(_[Report Number], ", ")},
            {"Sub-supplier",(c)=>Text.Combine(_[Sub-supplier], ", ")}, 
            {"text text text text.",(c)=>Text.Combine(_[text text text text.], ", ")}, 
            {"text text text",(c)=>Text.Combine(_[text text text], ", ")}, 
            {"text text text text text",(c)=>Text.Combine(_[text text text text text], ", ")}, 
            {"text text", (c)=>Text.Combine(_[#"text text"], ", ")}}
        ){0}}}),
excel excelquery
1个回答
0
投票

根据您的数据,以下结果将产生您显示的结果:

let
    Source = Excel.CurrentWorkbook(){[Name="Overview"]}[Content],
    #"Overview" = Table.TransformColumnTypes(Source,{
        {"NAME.1", type text}, {"NAME.2", type text}, {"NUMBER", Int64.Type}, {"RANDOM", type text}}),
  
//Add index column to be able to go back to original table order
    #"Added Index" = Table.AddIndexColumn(Overview, "Index", 0, 1, Int64.Type),

//Create a Name column that we will expand to all names in a single column
    #"Added Custom" = Table.AddColumn(#"Added Index", "Name", each List.RemoveNulls({[NAME.1]} & {[NAME.2]}), type {text}),
    #"Expanded Name" = Table.ExpandListColumn(#"Added Custom", "Name"),

//Use same technique on Data table
//Be certain the ITEM column is of "type text" if not set that way initially
    #"Added Custom2" =Table.AddColumn(Data,"dataName",each {[NAME.1],[NAME.2]}, type {text}),
    #"Expanded dataName" = Table.ExpandListColumn(#"Added Custom2", "dataName"),

//Extract these fields from the Data table
    #"Data Fields" = List.FirstN(Table.ColumnNames(#"Expanded dataName"),3),

//Merge the tables and return all the columns from the DATA table
    #"Merge Overview/Data" = Table.NestedJoin(#"Expanded Name","Name",#"Expanded dataName","dataName","Join",JoinKind.LeftOuter),

//Extract the desired fields
    #"Add Custom3" = Table.AddColumn(#"Merge Overview/Data", "Fields", each if [Name]=null then null
            else if Table.RowCount([Join]) = 0 then null
            else Record.SelectFields([Join]{0}, #"Data Fields"),type record),
    #"Removed Columns" = Table.RemoveColumns(#"Add Custom3",{"Name", "Join"}),
    #"Expanded Fields" = Table.ExpandRecordColumn(#"Removed Columns", "Fields", {"ITEM", "ITEM 2", "Y/N"}),
    #"Grouped Rows" = Table.Group(#"Expanded Fields", {"Index"}, {{"all", 
        each Table.TransformColumns(_, {
            {"ITEM", (c)=>Text.Combine(_[ITEM], ", ")},
            {"ITEM 2",(c)=>Text.Combine(_[ITEM 2], ", ")}, 
            {"Y/N", (c)=>Text.Combine(_[#"Y/N"], ", ")}}
        ){0}}}),
       
    #"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"Index"}),
    #"Expanded all" = Table.ExpandRecordColumn(#"Removed Columns1", "all", {"NAME.1", "NAME.2", "NUMBER", "RANDOM", "Index", "ITEM", "ITEM 2", "Y/N"}),
    #"Removed Columns2" = Table.RemoveColumns(#"Expanded all",{"Index"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns2",{
        {"NAME.1", type text}, {"NAME.2", type text}, {"NUMBER", Int64.Type}, 
        {"RANDOM", type text}, {"ITEM", type text}, {"ITEM 2", type text}, 
        {"Y/N", type text}})
in
    #"Changed Type"

概述

数据

结果

© www.soinside.com 2019 - 2024. All rights reserved.