在数据透视表中对相似的项目进行分组

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

任务 我正在用 Excel 制作一个研究项目跟踪器,我的团队需要填写它。

为了使跟踪器更容易填写,我为某些列提供了下拉多选功能,这意味着某些单元格中将有多个响应(例如,方法)。 这就是原始数据的样子:

Example Raw Data

我想查看全年开展的不同类型研究项目的详细信息。

为此,我创建了一些数据透视表并将它们链接到图表。

但是,我遇到了一个问题,数据透视表显示多选响应组合的细分,而不是计算每个单独项目的频率。

例如,当按研究方法分解数据时,我当前的数据透视表如下所示:

研究方法
采访 1
调查 1
卡片分类 1
采访、调查 2

相反,我希望将访谈、调查组合分开,并分别计算每种方法。所以它看起来像这样:

研究方法
采访 3
调查 3
卡片分类 1

要求

我的团队将定期使用此跟踪器,因此我希望我创建的数据透视表能够在人们添加到跟踪器时自动更新。这应该是一个动态且易于使用的文档!

跟踪器中的原始数据至少包含 5 列,我希望这些列具有此功能,并且可能有最多 20 个不同的多选选项。

问题

有人知道如何处理我的请求吗?如果这在 Excel 上也可行的话!

任何帮助将不胜感激!

我的尝试

  1. 我尝试过使用 countif 和通配符语句,但它们不适合我正在做的事情,因为我需要使用与我创建的数据透视表关联的过滤器(例如,我需要能够按日期过滤,如果我使用 COUNTIF 它不会捕获日期)。

  2. 我认为 Power 查询不合适,因为有太多的列和潜在的选择,以至于转换不切实际(不过我可能是错的!)。

  3. 我认为我最好的选择是更新 VBA 以影响数据透视表处理数据的方式,指示它对具有相似名称的类别进行分组。 假设我需要为工作表上的每个数据透视表编写单独的代码,我编写了一些代码,包括以下参数......

(工作表名称=project_summary_data;数据透视表名称=PivotTable14;数据透视表字段=research_method。

这是我粘贴到VBA中的project_summary_data表的代码;但是它一直给我一个 400 错误代码。

Sub GroupSimilarItemsInPivotTable()
    ' Set the worksheet containing the pivot table
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Worksheets("project_summary_data")
    On Error GoTo 0
    
    If ws Is Nothing Then
        MsgBox "Worksheet 'project_summary_data' not found."
        Exit Sub
    End If
    
    ' Set the pivot table
    Dim pt As PivotTable
    On Error Resume Next
    Set pt = ws.PivotTables("PivotTable14")
    On Error GoTo 0
    
    If pt Is Nothing Then
        MsgBox "PivotTable 'PivotTable14' not found."
        Exit Sub
    End If
    
    ' Set the pivot field containing the multi-select options
    Dim pf As PivotField
    On Error Resume Next
    Set pf = pt.PivotFields("research_method")
    On Error GoTo 0
    
    If pf Is Nothing Then
        MsgBox "PivotField 'research_method' not found."
        Exit Sub
    End If
    
    ' Initialize a collection to store grouped items
    Dim groupedItems As Collection
    Set groupedItems = New Collection
    
    ' Loop through each item in the pivot field
    Dim item As PivotItem
    For Each item In pf.PivotItems
        ' Check if the item has already been grouped
        If Not IsInCollection(groupedItems, item.Name) Then
            ' Add the item to the grouped items collection
            groupedItems.Add item.Name
            
            ' Loop through the remaining items to find similar names
            Dim i As Integer
            For i = item.Position + 1 To pf.PivotItems.Count
                If InStr(pf.PivotItems(i).Name, item.Name) > 0 Then
                    ' Update the item name to include the additional option
                    item.Name = item.Name & ", " & pf.PivotItems(i).Name
                    ' Add the count of the additional item to the original item
                    item.DataRange.Value = item.DataRange.Value + pf.PivotItems(i).DataRange.Value
                    ' Delete the additional item
                    pf.PivotItems(i).Visible = False
                End If
            Next i
        End If
    Next item
    
    MsgBox "Code execution completed successfully."
End Sub

Function IsInCollection(col As Collection, key As Variant) As Boolean
    On Error Resume Next
    IsInCollection = Not col(key) Is Nothing
    On Error GoTo 0
End Function
excel vba pivot
1个回答
0
投票

我会使用 PowerQuery 来完成这项工作。

let
    Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
    changeType1 = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"Project Name", type text}, {"Product area", type text}, {"Researcher", type text}, {"Research Methodology", type text}}),
    separate = Table.ExpandListColumn(Table.TransformColumns(changeType1, {{"Research Methodology", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Research Methodology"),
    changeType2 = Table.TransformColumnTypes(separate,{{"Research Methodology", type text}}),
    properCase = Table.TransformColumns(changeType2,{{"Research Methodology", Text.Proper, type text}}),
    group = Table.Group(properCase, {"Research Methodology"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in
    group

结果是

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