AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1617540
Accepted
Rory
Rory
Asked: 2021-01-15 04:04:53 +0800 CST2021-01-15 04:04:53 +0800 CST 2021-01-15 04:04:53 +0800 CST

MS Excel - 使用 M 代码组合表格

  • 772

我有一个文件,每张纸上有 3 个不同的表格。这些表都有不同数量的列和行,但它们都有 1 列共同。我之前在此链接中询问了我正在尝试完成的简化版本 MS Excel - 仅将表格与一些匹配数据合并

另一个用户,罗恩给了我一些代码,解决了我想做的基本前提,所以我拿了那个代码,我试着编辑它。在我的第一次尝试中,我尝试将第一张和第二张表中的表格添加在一起。它几乎奏效了,但还没有完全奏效。

前 3 步似乎工作得很好,我认为第 4 步(分组行)已经奏效,但我不确定。主要问题似乎是添加自定义和添加自定义 1 的步骤放在了我需要的前 2 列中,但是当我到达添加自定义 2 时,而不是在右侧添加另一列,它替换了之前添加的列。我想在其余列中添加的接下来的几个步骤,它仍然只用新列替换最后一列,因此只有 2 个自定义列。这是我的 3 张桌子

Excel 表格

这是我编辑后的代码:

let
    Source1 = Excel.CurrentWorkbook(){[Name=”WACEAchievement”]}[Content],
    Source2 = Excel.CurrentWorkbook(){[Name=”MedianATAR”]}[Content],
    combTbl = Table.Combine({Source1,Source2}),

    #”Grouped Rows” = Table.Group(combTbl, {“School”}, {{“Grouped”, each _, type table [School=text, Number of eligible year 12 students=nullable text, percent students who achieved the WACE=nullable text, Number of students with an ATAR=nullable text, percent of students with an ATAR=nullable text, Median ATAR=nullable text]}}),

    #”Added Custom” = Table.AddColumn(#”Grouped Rows”, “Number of eligible year 12 students”, each try
            List.RemoveNulls(Table.Column([Grouped],”Number of eligible year 12 students”)){0}
        otherwise null),

    #”Added Custom1” = Table.AddColumn(#”Added Custom”, “percent students who achieved the WACE”, each try
            List.RemoveNulls(Table.Column([Grouped],”percent students who achieved the WACE”)){0}
        otherwise null),
        
    #”Added Custom2” = Table.AddColumn(#”Added Custom”, “Number of students with an ATAR”, each try
            List.RemoveNulls(Table.Column([Grouped],”Number of students with an ATAR”)){0}
        otherwise null),
        
    #”Added Custom3” = Table.AddColumn(#”Added Custom”, “percent of students with an ATAR”, each try
            List.RemoveNulls(Table.Column([Grouped],”percent of students with an ATAR”)){0}
        otherwise null),
        
    #”Added Custom4” = Table.AddColumn(#”Added Custom”, “Median ATAR”, each try
            List.RemoveNulls(Table.Column([Grouped],”Median ATAR”)){0}
        otherwise null),        
    
    #”Removed Columns” = Table.RemoveColumns(#”Added Custom1”,{“Grouped”})
in
    #”Removed Columns”

以下是不同步骤所发生情况的一些示例。 步骤1 第2步 在此处输入图像描述

这是我卡住的进度镜头 重复的整体

microsoft-excel code
  • 2 2 个回答
  • 104 Views

2 个回答

  • Voted
  1. Best Answer
    FlexYourData
    2021-01-15T06:29:05+08:002021-01-15T06:29:05+08:00

    如果您不尝试编辑代码而是使用 UI 来获取结果,您会发现这更容易。例如,如果我从这里开始:

    在此处输入图像描述

    我依次使用 Data>From Table/Range 在每个表上创建一个查询。我在 Power Query 编辑器中单击“关闭并加载到”并将其配置如下:

    在此处输入图像描述

    即“仅创建连接”

    现在我有三个查询:

    在此处输入图像描述

    接下来,我使用 Data>Get Data>Combine Queries>Merge 并像这样配置它:

    在此处输入图像描述

    请注意,在 Join Kind 下拉列表中选择“Full Outer”非常重要。

    单击确定后,我有这个:

    在此处输入图像描述

    我单击“Table2”列顶部的双箭头并执行以下操作:

    在此处输入图像描述

    即我取消选择“使用原始列名作为前缀”并保留所有列选中。

    现在我有这个:

    在此处输入图像描述

    请注意,我现在有两个“学校”列——“学校”包含 Table1 中的学校列表,“School.1”包含 Table2 中的学校列表。

    We need to merge these columns before adding the data from Table3 to the query.

    To help make this clearer, I first rename each of those school columns by double-clicking the header and typing a new name:

    在此处输入图像描述

    Next, I use Add Column>Custom Column and configure it like this:

    在此处输入图像描述

    Note that M is case-sensitive and the if-then-else must be in lower case.

    Now I have my new column:

    在此处输入图像描述

    I right click Table1Schools and Table2Schools and select "Remove", then drag the new "School" column to the left of the remainder of the columns (not strictly necessary, but helps to stay organized). Now I have a School column with a school ID in each row. I also have the data from Table1 and Table2:

    在此处输入图像描述

    Next, I want to Merge Table3 with this query. So, in the Power Query Editor, I use Merge Queries on the Home tab. I configure it like this:

    在此处输入图像描述

    Note that the first table is actually "Merge1", which was the end-result of merging Table1 and Table2.

    After expanding the Table3 column in the same way as above, I have this:

    在此处输入图像描述

    So, I have one row where School is null. I need to repeat the new column process I followed above. So, I rename the columns:

    在此处输入图像描述

    Then create a new column with a formula similar to above:

    在此处输入图像描述

    After removing Merge1School and Table3School, then moving the new column to the far-left, I have the required result:

    在此处输入图像描述

    Again, I recommend using the UI where possible as it will save you lots of time. For info, the resulting query is this:

    let
        Source = Table.NestedJoin(Table1, {"School"}, Table2, {"School"}, "Table2", JoinKind.FullOuter),
        #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"School", "Data 2"}, {"School.1", "Data 2"}),
        #"Renamed Columns" = Table.RenameColumns(#"Expanded Table2",{{"School", "Table1Schools"}, {"School.1", "Table2Schools"}}),
        #"Added Custom" = Table.AddColumn(#"Renamed Columns", "School", each if [Table1Schools] is null then [Table2Schools] else [Table1Schools]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Table1Schools", "Table2Schools"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"School", "Data 1", "Data 2"}),
        #"Merged Queries" = Table.NestedJoin(#"Reordered Columns", {"School"}, Table3, {"School"}, "Table3", JoinKind.FullOuter),
        #"Expanded Table3" = Table.ExpandTableColumn(#"Merged Queries", "Table3", {"School", "Data 3"}, {"School.1", "Data 3"}),
        #"Renamed Columns1" = Table.RenameColumns(#"Expanded Table3",{{"School", "Merge1School"}, {"School.1", "Table3School"}}),
        #"Added Custom1" = Table.AddColumn(#"Renamed Columns1", "School", each if [Merge1School] is null then [Table3School] else [Merge1School]),
        #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Merge1School", "Table3School"}),
        #"Reordered Columns1" = Table.ReorderColumns(#"Removed Columns1",{"School", "Data 1", "Data 2", "Data 3"})
    in
        #"Reordered Columns1"
    

    Of course, the column renaming steps are not strictly necessary in this query (since they are removed shortly after renaming them) and you could wait until the last step to remove columns you don't want.

    • 1
  2. Ron Rosenfeld
    2021-01-15T18:24:15+08:002021-01-15T18:24:15+08:00

    You were very close in doing your code editing:

    You wrote, for example:

     #”Added Custom2” = Table.AddColumn(#”Added Custom”, “Number of students with an ATAR”, each try
                List.RemoveNulls(Table.Column([Grouped],”Number of students with an ATAR”)){0}
            otherwise null),
    

    But note that in the first line, you are adding the column to the #"Added Custom" table. But at each step, you need to add the custom column to the Table generated by the preceding step, in order to preserve the preceding table.

    So #"Added Custom2" should be added to the #"Added Custom1" table; and so forth.

    So, instead: (and also note you can have all three tables in your Table.Combine)

    let
        Source1 = Excel.CurrentWorkbook(){[Name="WACE"]}[Content],
        Source2 = Excel.CurrentWorkbook(){[Name="ATAR"]}[Content],
        Source3 = Excel.CurrentWorkbook(){[Name="CERTII"]}[Content],
        combTbl = Table.Combine({Source1,Source2,Source3}),
    
        #"Grouped Rows" = Table.Group(combTbl, {"School"}, {{"Grouped", each _, type table [School=text, Data 1=nullable text, Data 2=nullable text]}}),
        #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"School", Order.Ascending}}),
    
        #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Number of eligible year 12 students", each try
                List.RemoveNulls(Table.Column([Grouped],"Number of eligible year 12 students")){0}
            otherwise null),
    
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "percent students who achieved the WACE", each try
                List.RemoveNulls(Table.Column([Grouped],"percent students who achieved the WACE")){0}
            otherwise null),
    
        #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Number of students with an ATAR", each try
                List.RemoveNulls(Table.Column([Grouped],"Number of students with an ATAR")){0}
            otherwise null),
    
        #"Added Custom3" = Table.AddColumn(#"Added Custom2", "percent of students with an ATAR", each try
                List.RemoveNulls(Table.Column([Grouped],"percent of students with an ATAR")){0}
            otherwise null),
    
        #"Added Custom4" = Table.AddColumn(#"Added Custom3", "Median ATAR", each try
                List.RemoveNulls(Table.Column([Grouped],"Median ATAR")){0}
            otherwise null),
    
        #"Added Custom5" = Table.AddColumn(#"Added Custom4", "Number completed Cert II or higher but less than four ATAR courses", each try
                List.RemoveNulls(Table.Column([Grouped],"Number completed Cert II or higher but less than four ATAR Courses")){0}
            otherwise null),
    
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom5",{"Grouped"})
    
    in
        #"Removed Columns"
    

    在此处输入图像描述

    Note Given the nature of your data, after you combine the three tables, you could do the rest from the UI by selecting:

    • GroupBy School
    • Select Advanced
    • Then add an aggregation for each column with a Sum (or other arithmetic) operation.
      • 由于每个学校/列组合只有一个条目,因此各种算术运算(但不是计数)将返回适当的值。

    聚合示例

    在此处输入图像描述

    • 请注意,由于我很懒,我按字母顺序命名了新列。
    • 然后我在高级编辑器中手动添加了几行,以便根据它们的原始名称命名它们。
    • 当然,您可以通过输入与New Column Name要聚合的列相同的名称来消除这种情况。
    • 我不知道这种方法或以前的方法是否更有效。
    let
        Source1 = Excel.CurrentWorkbook(){[Name="WACE"]}[Content],
        Source2 = Excel.CurrentWorkbook(){[Name="ATAR"]}[Content],
        Source3 = Excel.CurrentWorkbook(){[Name="CERTII"]}[Content],
    
    combTbl = Table.Combine({Source1,Source2,Source3}),
    
        #"Grouped Rows" = Table.Group(combTbl, {"School"}, {
            {"a", each List.Sum([Number of eligible year 12 students]), type nullable number}, 
            {"b", each List.Sum([percent students who achieved the WACE]), type nullable number}, 
            {"c", each List.Sum([Number of students with an ATAR]), type nullable number}, 
            {"d", each List.Sum([percent of students with an ATAR]), type nullable number}, 
            {"e", each List.Sum([Median ATAR]), type nullable number}, 
            {"f", each List.Sum([Number completed Cert II or higher but less than four ATAR Courses]), type nullable number}
                }),
        #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"School", Order.Ascending}}),
    
    //rename the aggregated columns
    newColNames = List.RemoveFirstN(Table.ColumnNames(combTbl),1),
    curColNames = List.RemoveFirstN(Table.ColumnNames(#"Grouped Rows"),1),
    renameList = List.Zip({curColNames,newColNames}),
    reName = Table.RenameColumns(#"Sorted Rows",renameList)
    
    in
        reName 
    
    • 1

相关问题

  • 带有“和”运算符的 Excel 数据透视表

  • 如何对整列使用 Excel 的 LENGTH 函数?

  • Excel 数组(2 个变量)

  • 如何从 WSL 打开 office 文件

  • VBA根据文件名重命名工作表

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve