Enable "Display labels from the next field in the same column (compact form) - excel

Is there a way to enable the option "Display labels from the next field in the same column (compact form) in VBA?
This only enables the "Show item labels in outline form"
ActiveSheet.PivotTables("Name of pivot table").PivotFields("Name of pivot field").LayoutForm = xlOutline

The syntax should be:
ActiveSheet.PivotTables("Name of pivot table").PivotFields("Name of pivot field").LayoutCompactRow = True

Related

How do you change the data Color of a line chart in Power BI based on Slicer Selection

I have a table of selections that has no relationship and only serves to be a selector for a filter:
I then have this feed a slicer on my page so that when the user selectes a value, the following measure will evaluate (works great):
Metric Toggle = SWITCH(TRUE(),
SELECTEDVALUE(Metrics[Metric]) = "Current Ratio", MAX('FinancialData'[CurrentRatio])
,SELECTEDVALUE(Metrics[Metric]) = "Debt to Tangible Net Worth", MAX('FinancialData'[DebtToTNW])
,SELECTEDVALUE(Metrics[Metric]) = "Gross Sales", MAX('FinancialData'[GrossSales])
,SELECTEDVALUE(Metrics[Metric]) = "NetSales", MAX('FinancialData'[NetSales])
,SELECTEDVALUE(Metrics[Metric]) = "Current Portion of Long Term Debt", MAX('FinancialData'[CPLTD])
,SELECTEDVALUE(Metrics[Metric]) = "EBITDA", MAX('FinancialData'[EBITDA])
,0)
This measure drives a line chart. Is it possible to conditionally format the data line color on the chart for each selection? i.e If user selected "EBITDA", then line colour = Blue, If User Selected "Current Ratio", then Line Color = Red... etc
Deselect your Slicer value as below
Now add Metric to the Line Chart's Legend property and go to the Chart's property Data Color and you will find a list like below-
Now, select your specific colors for specific Metric value. Once you complete all the above setup, you will get your expected color for Line based on the Slicer selection.

Excel Pivot - Calculate average for values > XX

Data table simplified - read only:
<table border=1>
<tr><th>Date</th><th>Hours</th></tr>
<tr><td>Jan. 1</td><td>6.5</td></tr>
<tr><td>Jan. 2</td><td>8.5</td></tr>
<tr><td>Jan. 3</td><td>7.5</td></tr>
<tr><td>Jan. 4</td><td>9.0</td></tr>
</table>
Now I would like a pivot table that can calculate the average number of hours - only taking into account the days where the number of hours are > 8.0. Ie. for the above data the pivot should return 8.75.
I've tried with calculated field: =If(Hours > 8, Hours, NULL) - or blanks instead of null or similar.
Please help.
You don't need a Pivot table. Just use =AverageIf([Range of Hours],">8")
Edit:
To achieve through Pivot Table, I can only think of using "measure". But it could be quite slow for 120k rows as the Filter() function need to go through each row one by one.
create a the Pivot Table with the checkbox "Add this data to the Data Model" clicked;
Right-click on the "Range" (or the table name if your data is a table) in the Field List and choose "Add Measure"
Enter the measure's info and use =SUMX(Range, if(Range[Hour]>8, Range[Hour],0))/ COUNTROWS(FILTER(ALL(Range), Range[Hour]>8)) as formula (replace "Range" with Table name if source data is a table). And click "OK".
Then, you can use this measure to get the average value you want.

How can I filter a slicer for one item through VBA?

My slicer contains over 100 cities and I'd like to filter the slicer through VBA on one city at a time (in order to print the region of the chart into a Powerpoint). The trouble is that I think I would have to list every other city and show them as "Selected = False". I need a code block where I only specify the city/cities I and the rest of the slicer options are "Selected = False" by default.
I've recorded the macro code below and I've searched the net for a solution to this problem but have come up empty-handed.
With ActiveWorkbook.SlicerCaches("Slicer_City")
.SlicerItems("New York").Selected = True
.SlicerItems("Chicago").Selected = False
.SlicerItems("Trenton").Selected = False
.SlicerItems("Atlanta").Selected = False
.SlicerItems("Houston").Selected = False
.SlicerItems("Los Angeles").Selected = False
' there are 100 more cities to list if I just wanted "New York" to be selected
'in the slicer
End With
I need code where I should only specify the selection (New York) and not declare every other city as "Selected = False".
The quickest way is to make the pivotfield of interest a pagefield, and then iterate through the items and set the .CurrentPage property of the PivotField to each in turn, as this automatically turns the other items to FALSE. If you don't want the PivotField in the Page Fields area, then you can still do this quickly by setting up a 'Master' PivotTable somewhere out of sight, putting the field of interest in the master as a PageField, connecting the Master PivotTable to PivotTable1 ('Slave') with a Slicer, and then changing the .CurrentPage property of the Master, which will then instantly filter the .Slave via the Slicer.
See my answer at VBA code to filter a pivot table based on the value in a Cell that has some efficiency tips for this kind of thing, as well as links to other answers containing code.

how to change the name of calculated field in pivot table using vba

I have used below code. But still its not updating the name of calculated field.
ActiveSheet.PivotTables(PivotName).CalculatedFields.Add "basic_amt", _
"=((amount + other_charges) - discount_amt)", True
ActiveSheet.PivotTables(PivotName).PivotFields("basic_amt").Orientation = xlDataField
ActiveSheet.PivotTables(PivotName).PivotFields("basic_amt").Name = "Basic Amount"
ActiveSheet.PivotTables(PivotName).PivotFields("basic_amt").NumberFormat = "###0.00"
You can update all pivot tables using the following code:
'Refresh All Pivot Tables
ActiveWorkbook.RefreshAll
To update a specific pivot table within the sheet:
'Refresh A Single Pivot Table
ActiveSheet.PivotTables("YourPivotTable").PivotCache.Refresh
If you want to update a specific pivot table in another sheet you can use:
'Refresh A Single Pivot Table
ThisWorkbook.Sheets("YourSheet").PivotTables("YourPivotTable").PivotCache.Refresh

Moving date period filter in Excel 2010 pivot-table

Is it possible to set some kind of filter for a moving date period?
For example one of the DB views I'm trying to replicate in my pivot-table has this filter :
DATEDIFF(day, dateColumn, GETDATE()) <= 90
So basically I always want to display the last 90 days of whataver data there is in the cube table.
Is this possible?
The answer to this question is here :
http://blogs.socha.com/2010/05/sliding-date-ranges-with-excel-2010.html
Example for a moving period of 30 days :
Select a cell inside a pivot table bound to the cube so that the PivotTable tools are available
Click the Options tab on the ribbon under the PivotTable Tools section:
Click the Fields, Items & Sets drop-down in the Calculations section of this ribbon tab
Click Manage Sets… in the drop-down
Click New… and then Create Set using MDX…
Enter a name for this set in the Set name text box
Enter the MDX expression that defines the date range
Click OK
Filter(
[Date].[Date].[Date],
[Date].[Date].CurrentMember.Member_Value < Now()
AND [Date].[Date].CurrentMember.Member_Value >= DateAdd("d", -30, VBA![Date]())
)

Resources