将数据分组在Microsoft Access或Microsoft Excel中

发布于 2025-01-24 10:24:55 字数 1395 浏览 3 评论 0原文

这似乎应该很简单,但我正在努力寻找解决方案。

我有一张大表格,其中包含项目团队成员,每个项目中的每个团队成员一行。这是它的外观简化样本:

项目角色团队成员
Alpha项目经理Will Will Will
Alpha业务分析师John
Alpha业务分析师Amy
Alpha开发商Sally
Alpha开发人员Joe Alpha开发人员Joe
Alpha开发人员Pete
Beta Beta项目经理John
Robert Beta Beta业务分析师Frank
Frank Beta开发人员Beta开发人员Frank
Beta开发人员Beta Beta Develaper Bruce

如您所见,我们的项目通常每个角色都有多个团队成员,我正在尝试在每个角色的列下列出适当的名称,每个项目一行:

项目项目经理业务分析师开发人员
AlphaWillJohn
Amy
Sally
Joe
Pete
BetaRobertJohnFrank
Bruce

在我尝试在MS Access中使用Crosstab查询时,我必须选择名字或姓氏;我找不到列出所有这些方法的方法。

当我尝试MS Excel中的枢轴表时,我可以将每个角色显示为列,但是名称仍然出现在第一列中,而不是根据团队成员的角色出现在列中。

MS Access或MS 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:

ProjectRoleTeam Member
AlphaProject ManagerWill
AlphaBusiness AnalystJohn
AlphaBusiness AnalystAmy
AlphaDeveloperSally
AlphaDeveloperJoe
AlphaDeveloperPete
BetaProject ManagerRobert
BetaBusiness AnalystJohn
BetaDeveloperFrank
BetaDeveloperBruce

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:

ProjectProject ManagerBusiness AnalystDeveloper
AlphaWillJohn
Amy
Sally
Joe
Pete
BetaRobertJohnFrank
Bruce

When I try to use a crosstab query in MS Access, I have to choose either the first or last name; 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.

A solution in either MS Access or MS Excel would meet my needs.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2025-01-31 10:24:55

为了在PQ中执行此操作,您必须

  • 按项目
  • 组按每个子表进行分组,请通过角色
  • 组合使用LF字符的团队成员列表,如果您的原始表位于Excel中,则使用LF字符
  • 在角色列上使用每个subtable在角色列中

使用PQ,以使用PQ

  • 枢轴 您的原始表
  • data =>获取& transform =>从表/范围从表格中
  • 从PQ UI打开时,导航到home =>高级编辑器
  • 在代码的第2行中记下表名。
  • 在下面的 m-code 上替换现有代码
  • 将粘贴代码的第2行中的表名更改为“真实”表名称,
  • 请检查任何注释,还将应用步骤窗口,为了更好地理解算法和步骤,
  • 如果将其加载到Excel中,请确保用线馈送线上启用单词包装,以使显示正确。

如果您的原始表是来自另一个来源,则需要从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

To do this in PQ, you have to

  • group by Project
  • group each sub-table by Role
  • Combine the list of Team members using the LF character
  • Pivot each subtable on the Role column

To use PQ if your original table was in Excel

  • Select some cell in your original table
  • Data => Get&Transform => From Table/Range or From within sheet
  • When the PQ UI opens, navigate to Home => Advanced Editor
  • Make note of the Table Name in Line 2 of the code.
  • Replace the existing code with the M-Code below
  • Change the table name in line 2 of the pasted code to your "real" table name
  • Examine any comments, and also the Applied Steps window, to better understand the algorithm and steps
  • 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.

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

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

enter image description here

浅唱々樱花落 2025-01-31 10:24:55

为了完整性,这里是一个访问解决方案:

”在此处输入图像描述”

问题(突出显示)是访问只能将1个摘要放在横牌的每个单元格内,但是有多个团队成员填补了一些角色(高光线)。为了解决这个问题,我们将Teammember重新计算为:sally< br/gt; joe< br/gt; pete
幸运的是,这里已经有一个功能:
Microsoft

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

访问控件中的丰富文本格式仅适用于长文本格式。因此,首先将角色和团队成员转换为表中的长文本。不幸的是,GetList必须返回字符串,因此我们无法直接从使用GetList的查询中构建报告。取而代之的是,我们基于类似的查询来构建报告:

”“在此处输入图像说明”

https://i.sstatic.net/oxddt.png“ alt =”在此处输入图像说明”>

将适当的报告控件设置为RichText,然后替换报告(单击红色)记录源(确保其报告使用getList的查询更改的记录源。
参见 http://allenbrowne.com/ser-63.html 最终查询的联合查询(备忘录=长文本)

使用GetList计算的字段替换团队成员。

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

And for completeness here is an Access solution:

enter image description here

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). 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

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. Unfortunately, GetList must return a string so we can't build the report directly from a query that uses GetList. Instead we build the report based on a similar query:

enter image description here

enter image description here

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.
see http://allenbrowne.com/ser-63.html for a another work around involving a union query (memo= long text)

For the final query replace Team Member with a calculated field using GetList.

enter image description here

TeamMember: First(GetList("SELECT TeamMember FROM Sheet1 WHERE Project = '" & [Project] & "' AND Role = '" & [Role] & "'","<br/>","<br/>"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文