如何从 SSAS 数据透视表列中排除两项并选择其余所有项目?

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

我正在尝试过滤 SSAS 多维数据集数据透视表中的列。我只想从该列中排除两个已识别的值,并选择所有其余值。

不知何故,我的代码排除了许多其他值,只选择了少数其他值。

有人可以帮忙,告诉我我做错了什么吗?这是代码:

    ActiveSheet.PivotTables("PivotTable1").PivotFields( _
        "[MasterData Publisher Groups].[Publisher Group].[Publisher Group]"). _
        ClearAllFilters
    Range("E8").Select
    
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim includeItems As Collection
    Dim item As Variant
    Dim i As Long
    Dim excludeList As Variant
    Dim visibleItemsArray() As Variant
    Dim itemCount As Long

    ' Set your PivotTable and PivotField
    Set pt = ActiveSheet.PivotTables("PivotTable1")
    Set pf = pt.PivotFields("[MasterData Publisher Groups].[Publisher Group].[Publisher Group]")

    ' Refresh the PivotTable to ensure it has the latest data
    pt.RefreshTable

    ' Define the items to exclude
    excludeList = Array("[MasterData Publisher Groups].[Publisher Group].&[30118]", _
                        "[MasterData Publisher Groups].[Publisher Group].&[27123]")

    ' Initialize the collection to store items to include
    Set includeItems = New Collection

    ' Loop through all items in the pivot field
    For Each pi In pf.PivotItems
        ' Check if the item is in the exclude list
        Dim shouldExclude As Boolean
        shouldExclude = False
        
        For i = LBound(excludeList) To UBound(excludeList)
            If pi.Name = excludeList(i) Then
                shouldExclude = True
                Exit For
            End If
        Next i

        ' Add the item to includeItems if it should not be excluded
        If Not shouldExclude Then
            includeItems.Add pi.Name
        End If
    Next pi

    ' Convert the collection to an array
    itemCount = includeItems.count
    ReDim visibleItemsArray(1 To itemCount)
    For i = 1 To itemCount
        visibleItemsArray(i) = includeItems(i)
    Next i

    ' Clear existing filters
    pf.ClearAllFilters

    ' Set the VisibleItemsList property
    pf.visibleItemsList = visibleItemsArray

编辑:项目列表非常长,这就是为什么我无法指定在 vba 语句中选择哪些项目。一开始,当我尝试将所有可见项目放入列表中时,我收到一个运行时错误,指出该语句太长。因此,我将采用这种方式来构建排除和包含列表。

EDIT2:调试时,当我将鼠标悬停在上面的 LBound / RBound 循环中的 pi.Name 上时,该值的形式为“[MasterData Publisher Groups].[Publisher Group].&[30118]”而不是真实的我在 Excel 中看到的输出名称。正常吗?也许这就是代码无法正常工作的原因?

excel vba ssas ssas-tabular
1个回答
0
投票

您的 EDIT2 响应是 MDX 格式,也用于 Excel CUBE 公式。继续调试,但以下是有关 MDX 和 CUBE 公式的优质资源的链接,可能会有所帮助:

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.