简体   繁体   English

在 Microsoft Access 或 Microsoft Excel 中分组数据

[英]Grouping data within Microsoft Access or Microsoft Excel

This seems like it should be really simple but I am struggling to find a solution.这看起来应该很简单,但我正在努力寻找解决方案。

I have a large table containing project team members by role, one row for each team member on each project.我有一个大表,其中包含按角色分类的项目团队成员,每个项目的每个团队成员一行。 Here is a simplified sample of what it looks like:这是它的外观的简化示例:

Project项目 Role角色 Team Member队员
Alpha Α Project Manager专案经理 Will将要
Alpha Α Business Analyst业务分析师 John约翰
Alpha Α Business Analyst业务分析师 Amy艾米
Alpha Α Developer开发商 Sally莎莉
Alpha Α Developer开发商 Joe
Alpha Α Developer开发商 Pete皮特
Beta测试版 Project Manager专案经理 Robert罗伯特
Beta测试版 Business Analyst业务分析师 John约翰
Beta测试版 Developer开发商 Frank坦率
Beta测试版 Developer开发商 Bruce布鲁斯

As you can see, our projects often have multiple team members per role, and I'm trying list the appropriate names under a column for each role, one row per project:如您所见,我们的项目通常每个角色都有多个团队成员,我正在尝试在每个角色的列下列出适当的名称,每个项目一行:

Project项目 Project Manager专案经理 Business Analyst业务分析师 Developer开发商
Alpha Α Will将要 John约翰
Amy艾米
Sally莎莉
Joe
Pete皮特
Beta测试版 Robert罗伯特 John约翰 Frank坦率
Bruce布鲁斯

When I try to use a crosstab query in MS Access, I have to choose either the first or last name;当我尝试在 MS Access 中使用交叉表查询时,我必须选择名字或姓氏; I cannot find a way to list all of them.我找不到列出所有这些的方法。

When I try a pivot table in MS Excel, I can get each role to appear as a column, but the names still appear in the first column rather than within the column based on the team member's role.当我在 MS Excel 中尝试 pivot 表时,我可以让每个角色显示为一列,但名称仍然出现在第一列中,而不是根据团队成员的角色出现在列中。

A solution in either MS Access or MS Excel would meet my needs. MS Access 或 MS Excel 中的解决方案可以满足我的需要。

Thank you!谢谢!

To do this in PQ, you have to要在 PQ 中执行此操作,您必须

  • group by Project按项目分组
  • group each sub-table by Role按角色对每个子表进行分组
  • Combine the list of Team members using the LF character使用 LF 字符组合团队成员列表
  • Pivot each subtable on the Role column Role 列上的每个子表 Pivot

To use PQ if your original table was in Excel如果您的原始表位于 Excel 中,则使用 PQ

  • Select some cell in your original table Select 原始表格中的一些单元格
  • Data => Get&Transform => From Table/Range or From within sheet Data => Get&Transform => From Table/RangeFrom within sheet
  • When the PQ UI opens, navigate to Home => Advanced Editor当 PQ UI 打开时,导航到Home => Advanced Editor
  • Make note of the Table Name in Line 2 of the code.记下代码第 2 行中的表名称。
  • Replace the existing code with the M-Code below用下面的M代码替换现有代码
  • Change the table name in line 2 of the pasted code to your "real" table name将粘贴代码第 2 行中的表名更改为您的“真实”表名
  • Examine any comments, and also the Applied Steps window, to better understand the algorithm and steps检查任何评论以及Applied Steps window,以更好地理解算法和步骤
  • If you are loading this back to Excel, be sure to enable word-wrap on the cells with the line feeds in order to make display properly.如果您要将其加载回 Excel,请确保使用换行符在单元格上启用自动换行,以便正确显示。

If your original table is from another source, you will need to access that source from PQ -- there are numerous connectors you can substitute for the first line in the code below如果您的原始表格来自其他来源,您将需要从 PQ 访问该来源——有许多连接器可以替换下面代码中的第一行

let
    Source = Excel.CurrentWorkbook(){[Name="TeamMembers"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Project", type text}, {"Role", type text}, {"Team Member", type text}}),

//Group by Project
//  Sub group by Role
//  Pivot on Role
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Project"}, {
        {"roles", each Table.Pivot(
        Table.Group(_,"Role",{
            {"role", each Text.Combine([Team Member],"#(lf)")}
            }),List.Distinct([Role]),"Role","role")}
        }),

//Expand all columns
    roles = List.Distinct(#"Changed Type"[Role]),
    #"Expanded roles" = Table.ExpandTableColumn(#"Grouped Rows", "roles", roles,roles),

//set data types
    typeIt = Table.TransformColumnTypes(#"Expanded roles", List.Transform(Table.ColumnNames(#"Expanded roles"), each {_, type text}))
in
    typeIt

在此处输入图像描述

And for completeness here is an Access solution:为了完整起见,这里有一个 Access 解决方案:

在此处输入图像描述

The problem (highlighted) is Access can only put 1 summary inside each cell of the cross-tab but there are multiple Team Members filling some roles (high-lighted).问题(突出显示)是 Access 只能在交叉表的每个单元格内放置 1 个摘要,但有多个团队成员担任某些角色(突出显示)。 To get around this we recalculate TeamMember to be a rich text list like: Sally<br/>Joe<br/>Pete Fortunately there is already a function for doing this here: Microsoft Access condense multiple lines in a table为了解决这个问题,我们将 TeamMember 重新计算为富文本列表,例如: Sally<br/>Joe<br/>Pete幸运的是,这里已经有一个 function 用于执行此操作: Microsoft Access condense multiple lines in a table

Public Function GetList(SQL As String _
                            , Optional ColumnDelimeter As String = ", " _
                            , Optional RowDelimeter As String = vbCrLf) As String
                           
' make sure to add a reference to the latest Microsoft ActiveX Data Objects x.x Library For me that was 6.1
Const PROCNAME = "GetList"
Const adClipString = 2
Dim oConn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim sResult As String

On Error GoTo ProcErr

Set oConn = CurrentProject.Connection
Set oRS = oConn.Execute(SQL)
sResult = oRS.GetString(adClipString, -1, ColumnDelimeter, RowDelimeter)
If Right(sResult, Len(RowDelimeter)) = RowDelimeter Then
    sResult = Mid$(sResult, 1, Len(sResult) - Len(RowDelimeter))
End If

GetList = sResult
oRS.Close
oConn.Close

CleanUp:
    Set oRS = Nothing
    Set oConn = Nothing

Exit Function
ProcErr:
    ' insert error handler
    Resume CleanUp

End Function

The rich text format in access controls only works with the long text format.访问控制中的富文本格式仅适用于长文本格式。 So start by converting Role and TeamMember to long text in the table.因此,首先将 Role 和 TeamMember 转换为表中的长文本。 Unfortunately, GetList must return a string so we can't build the report directly from a query that uses GetList.遗憾的是,GetList 必须返回一个字符串,因此我们无法直接从使用 GetList 的查询构建报告。 Instead we build the report based on a similar query:相反,我们基于类似的查询构建报告:

在此处输入图像描述

在此处输入图像描述

set the appropriate report controls to richtext then replace the reports(click on red) record source(make sure its reports record source that is changed) with the query that uses getlist.将适当的报告控件设置为富文本,然后将报告(单击红色)记录源(确保其报告记录源已更改)替换为使用 getlist 的查询。 see http://allenbrowne.com/ser-63.html for a another work around involving a union query (memo= long text)请参阅http://allenbrowne.com/ser-63.html了解涉及联合查询的另一项工作(备忘录=长文本)

For the final query replace Team Member with a calculated field using GetList.对于最终查询,使用 GetList 将 Team Member 替换为计算字段。

在此处输入图像描述

TeamMember: First(GetList("SELECT TeamMember FROM Sheet1 WHERE Project = '" & [Project] & "' AND Role = '" & [Role] & "'","<br/>","<br/>"))

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

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