Thursday, January 23, 2014

(SOLVED) Slicer Selection in Chart Title without VBA!!!

So, I've been looking ALL OVER the internet to find a solution to this problem... Everywhere I look I read something like, "No, it's not possible" or "In a word, no".  Well, that's just not good enough for me.  I specialize in interim solutions and work arounds... SOoooo, after much research and trial and error, I've come up with a way to do just what we're all asking about - "How do I show the slicers selected in my Power Pivot chart?"  Since we upload our files to SharePoint and the end users are using Excel Services because they're working with Excel 2007, we can't use VBA for any of our solutions that have a data model.

Here's what we'll need (Remember, I'm using Excel 2013, NOT 2010 with the Add-In):
Data in the Data Model
Pivot Table
Pivot Chart
Slicers (Connected to the Pivot Table)

Formulas:
Cubeset(Connection, Set Expression,[Caption],[Sort Order], [Sort By])
CubeRankedMember(Set)
IfError(Value, Value if Error)

Data:  I'm using World population Data found at: http://www.census.gov/population/international/data/worldpop/table_population.php

I added a Decade column to assist me in quickly grouping the data by decade.


Now, I'm adding this table to the data model simply by:

  • Highlight the dataset
  • Click the PowerPivot Tab at the top
  • In the Ribbon, click "Add to Data Model"
There are multiple ways to add data to the model.  Typically I will connect directly to a data source instead of adding what I already have in the current spreadsheet, but that can be a discussion for another day.

Now that we have our data in the data model, let's stop and discuss a few very important details about the following steps.  It is imperative that we create a pivot table linked to the data model and NOT just a pivot chart.  The issue with linking a chart title or axis label to a cell can be bypassed simply by generating a pivot chart from a pivot table because it is then not directly linked to the data model.  This was probably the BIGGEST issue with the whole process.. I discovered the issue when I right-clicked and selected "Pivot Chart Options" for both a chart based on pivot table and a chart based on the model.  If you do this side-by-side, you will see that the chart connected to the data model has a window titled "PivotChart Options".  However, if you do this with the chart based on the Pivot Table, you will see "PivotTable Options".  Excel sees these charts differently.  To simplify and summarize, you can do all things to a chart based on a pivot table that you could do with the chart based on a dataset in a spreadsheet.  You cannot do this with a chart based on a data model.  

Back to the DataModel:

Within the Data Model window click the following:
  • Home Tab
  • PivotTable > Pivot Table
  • New WorkSheet > OK
Create your pivot table as you need it.  If you're following me using the WorldPop data, I'm using:

  • Rows = Decade
  • Values = Average of Annual Growth Rate (%)
This is what we have:


Let's Add our Slicer. On the Ribbon Click:


  • Insert
  • Slicer (Found in the Filters Group)
If asked, link this to your data model.  Select "Decade" from the column list under the table and click OK


Your slicer will look similar to this:

Link the slicer to the Pivot table by:
  • Right-Click the Slicer
  • Report Connections
  • Check the Pivot Table you want the slicer to control
  • Click OK
Now, Let's create our Pivot Chart:
  • Click somewhere on your Pivot Table
  • Under "PIVOTTABLE TOOLS" On the Ribbon, click "Pivot Chart"
  • Let's choose a Clustered Column Chart
  • Click OK
We now have:

I've selected a few of the slicers to demonstrate that the Slicer is working properly.

Now, to the fun part!  How do we get the slicers selected to show on the chart?

Formulas:
CUBESET(Connection, Set Expression,[Caption],[Sort Order], [Sort By])
CUBERANKEDMEMBER(connection, set_expression, rank, [caption])
IFERROR(Value, Value if Error)

When I build scorecards and dashboards I prefer to have a control sheet that is hidden that contains formulas that I don't want other's to booger up.  So I'm inserting a new sheet titled "ControlSheet" From here, the cell references will be in reference to sheet "ControlSheet"

**The formulas that follow differ slightly if you are using Excel 2010 (PowerPivot v1 and v2)- The "Connection" referred to in the Cubeset formula will always be "PowerPivot Data". "Connection" for Excel2013 is "ThisWorkbookDataModel"

In Cell A1, type the following:
  • =CUBESET("ThisWorkbookDataModel",Slicer_Decade, "Decade")
I simply leave the sort options blank. This will populate Cell A1 with a linked reference to the DOS Quarter Slicer.

Next, In Cell A2, type the following:
  • =IFERROR(CUBERANKEDMEMBER("ThisWorkbookDataModel",$A$1,ROW(Sheet1!A1)),"") - Remember, "Sheet1!" is the name of whatever sheet you're working in
The formula does the following:
  • If no slicers are selected, an error would be returned, so "IFERROR" returns a blank value.
  • Otherwise, if all slicers are selected (no filter) the value will be "ALL"
  • Otherwise, if one or more slicers are selected, the first value in the list of slicers will be returned
Now, since we have  10 decades in our slicer, let's use this formula 9 times (since 10 = "ALL").
  • Validate that your formula in cell A2 has the dollar signs around A1 ($A$1).
  • Copy the formula down to cell A10 (or Auto-Fill).
  • I typically use the "All Borders" Border around the range to indicate that the cells are being used even if they are blank.
Next, test your Slicer by Selecting Multiple Values and see what is returned by the formulas you've just created.

 Now that we are "Catching" our slicers, we can Create a label like "Filters Used: " , concatenate the values and link to our chart. 

Let's do it!

In Cell A12 on ControlSheet, Type "Filters Used - " 

In Cell A13 Copy/Paste the following formula
  • =A2&IF(A3="","",", "&A3)&IF(A4="","",", "&A4)&IF(A5="","",", "&A5)&IF(A6="","",", "&A6)&IF(A7="","",", "&A7)&IF(A8="","",", "&A8)&IF(A9="","",", "&A9)&IF(A10="","",", "&A10)
This formula will always return the value in Cell A2, but will check to see if the following cells are blank before returning the value.  If it's blank, then it returns a blank value.  If it's not, it adds a comma and then the value of the cell.

In cell A14 copy/paste the following formula:
  • =A12&TRIM($A$1)&": "&TRIM($A$13)
You should see:

In cell A15 concatenate your Chart title with the value in Cell A14..  I prefer to add a line between (carriage return).  My formula looks something like this:

="Avg World Population by Decade "&char(10)&A14

*Char(10) will add the carriage return we need

Now, let's return to Sheet1 and our chart...

Click on the chart title and in the formula bar type "=ControlSheet!$A$15"

You will see the this:

As you change the filters in the slicer, the title will change with it!

For the longest time I thought "Let's put the filters in the Title" was the best idea EVER!.. However, once I did it, I realized it wasn't a great Idea.  If you have multiple slicers you've incorporated into your title it becomes WAY oversized and unruly.  You see, when you link the title to the cell, ALL of the text is the same font size - you can't make the filter text smaller than the title text.  For me, the title should be the focus and the filters just there if you need them.

Let's solve this: ADD A HORIZONTAL AXIS LABEL! - Since the data in this case is very self explanatory we don't need the axis label to describe the axis.
  • Click on chart
  • On Ribbon, Click "Design" in PivotChart Tools
  • Click Add Chart Element
  • Axis Titles > Primary Horizontal
  • Click on the Axis Label on the chart
  • Paste "=A12&TRIM($A$1)&": "&TRIM($A$13)" into the formula Bar
  • Change the font size to "8"
  • Change your Chart Title by simply typing the title in "Avg World Population Growth Rate (%) By Decade"
You should now see this:


I hope you all enjoy this!  It's been a royal pain trying to work without this solution, so if you share it, please share some of the credit with me if you reference this.

Thanks!

15 comments:

  1. Great technique! Thanks for the detailed article. I like how you made use of the axis title to display the filter values. You could also move the axis title up under the chart title to use it as a sub title.

    ReplyDelete
  2. Thanks Jon! I appreciate your feedback

    ReplyDelete
  3. I followed your method but the cubeset function isn't working. i wonder can you share the raw data with me

    ReplyDelete
  4. Hi Cat, Thanks for asking. Quick question: Which version of Excel are you using?

    ReplyDelete
  5. What is the connection for Excel 2010? If it is even possible..

    ReplyDelete
  6. Please note the statement in the article: "**The formulas that follow differ slightly if you are using Excel 2010 (PowerPivot v1 and v2)- The "Connection" referred to in the Cubeset formula will always be "PowerPivot Data". "Connection" for Excel2013 is "ThisWorkbookDataModel"

    Please let me know if this doesn't work - I don't use 2010 and I'd rather not pass on info that would be inaccurate.

    Thanks!

    ReplyDelete
  7. This is exactly what I was looking for - it worked as described.
    Thank you!!

    ReplyDelete
  8. Thank you for this post, has made my report 10x more user friendly.

    ReplyDelete
  9. I Am not able to add formula to axis title. Pls guide me

    ReplyDelete
  10. Thanks for reaching out! Have you followed every step exactly? Without more information, I can only guess as to what the problem is: Did you create the chart from the pivot table and NOT the data model? If you create the chart directly from the data model, this method will not be possible.

    Please review each step to make sure it was followed. Also, what version of Excel are you using?

    ReplyDelete
  11. Thanks for reaching out! Have you followed every step exactly? Without more information, I can only guess as to what the problem is: Did you create the chart from the pivot table and NOT the data model? If you create the chart directly from the data model, this method will not be possible.

    Please review each step to make sure it was followed. Also, what version of Excel are you using?

    ReplyDelete
  12. I just found this while looking for a similar solution. This is excellent! Thank you so much for sharing.

    Even though this is a couple of years old, I'm hoping you may still be able to answer a new question. I'm looking for how I can utilize this with a "timeline" slicer. Do you know how that might be accomplished? I am using Excel 2016.

    Thank you.

    ReplyDelete
    Replies
    1. I haven't really used a lot of "Timelines" for slicing, but testing the idea, I used the following:

      =CUBESET("ThisWorkbookDataModel",Timeline_Calendar_Date,"Timeline")

      This is how to reference a Timeline instead of a slicer - "Timeline_Calendar_Date" is the name of the timeline you added (yours might be named a bit different).

      Using the method in the article above, I am able to return the start date in the range. I haven't played too much with it, but determined if you copy down the cuberankedmember() formula, you'll get all of the values selected. Note: This could potentially be a very large list.

      If a large list is not an issue (example: you store it on a hidden sheet), then you could simply do a min and max on the list and you'll have your date range.

      Hope this helps!

      Delete
    2. Thank you! I was able to reference the timeline this way and the range returned all values in my dataset (around 400 at this time). I guess that I would need to do further calculations to make it return the month(s) or quarter(s) of the returned data. I may look at doing a min/max on this data and simply include the date range instead.

      When selecting a timeline slicer, it changes to indicate what period is selected, for example Q1 2017 or a month or month range, or a year, etc. I was hoping to use this value in the chart header but haven't been able to determine how to directly refer to that value. If I find something, I'll be sure to share.

      I was able to use your method to to include the selected fiscal quarter in the header by adding a new column to the spreadsheet which calculates the quarter using the formula below:

      ="Q"&ROUNDUP(MONTH([@[Request Received]])/3,0)&" "&YEAR([@[Request Received]])

      The result returns the quarter and year for the selected date. I created a new slicer on this column and using your formula I was able to include this in the chart title.

      Thanks again!

      Delete
    3. Absolutely! I mainly put this blog together so I wouldn't forget how I did things, but it's also nice to collaborate and work together with other people once in a while. Nice work!

      Delete