VBA to add calculated measure to PowerPivot Datafield

nussidom

New Member
Joined
Mar 30, 2021
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

first post here. Been searching the internet for a solution to this question but to no avail.

I'm not experienced in VBA but I need to automate one task so I hacked up some code. Got pretty close but now I don't know how to use previously created calculated measures as datafields with a PowerPivot data model. Recording a macro for dragging calculated measures to 'Values' box doesn't work (macro recording turns out empty). Would really appreciate some help. VBA code snippet below.

VBA Code:
' define a new measure and use it as datafield
    Set objCubeField = objPivotTable.CubeFields.GetMeasure( _
        AttributeHierarchy:=objPivotTable.CubeFields( _
            "[" & objListObjectWithData.Name & "]." & _
            "[" & objListObjectWithData.ListColumns(18).Name & "]"), _
        Function:=xlCount, _
        Caption:="Total Clients")
    objPivotTable.AddDataField objCubeField
    objPivotTable.DataFields(1).Caption = "Total Clients"

' add calculated measures
    Set myModel = ActiveWorkbook.Model
    Set myModelTable = myModel.ModelTables.Item("Table1")
    Set myModelMeasures = myModel.ModelMeasures
    myModelMeasures.Add MeasureName:="Total Verified Clients", _
                           AssociatedTable:=myModelTable, _
                           Formula:="COUNTROWS(FILTER(Table1; Table1[Verified] = ""1""))", _
                           FormatInformation:=myModel.ModelFormatGeneral
                           
    myModelMeasures.Add MeasureName:="Verification Rate", _
                           AssociatedTable:=myModelTable, _
                           Formula:="ROUND(((COUNTROWS(FILTER(Table1; Table1[Verified] = ""1""))/COUNTROWS(Table1))*100);2)", _
                           FormatInformation:=myModel.ModelFormatGeneral

'use calculated measures as a datafield
???

Thank you and regards
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
VBA Code:
    myPivotTable.AddDataField myPivotTable.CubeFields("[Measures].[Verification Rate]")
    myPivotTable.AddDataField myPivotTable.CubeFields("[Measures].[Total Verified Clients]")
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top