Pages

Wednesday, November 21, 2012

Measure Selection using Slicers in PowerPivot

As a Business Intelligence professional, I find it really fascin(/frustr)ating working with Excel users. You get exposed to a completely different way of thinking when you work with them, not to say that they are extremely smart and demanding. From the moment I started working with PowerPivot and interacting with Excel users, I knew it was not going to be easy tackling their questions and requirements. You simply cant escape by saying that the required feature is not available because they will push you till you find a workaround. This was the case when one of my users came back to me asking for a slicer which will dynamically help them to select the measures. I replied saying that the slicers are not intended for that purpose and should be used for filtering the data (and not for just selecting or deselecting values like Measures, which can be easily done from the Field List pane). However, the user pushed back saying that he would like to create a dashboard for the top management and didn’t want them to be using the Field List pane. That is when I had to put on my thinking cap.

1 Measure selection using slicers in powerpivot

For the purpose of this post, I am going to use some simple mocked up data which involves three measures (Sales, Quantity and Profit) for country and year.

2 Data model and sample data

Follow the steps below to recreate a slicer which can be used to select or deselect the measures in a pivot table:-

1) Create a table which will have the list of measure names needed in the slicer in the MeasureName column and their representative integer ids in the MsrId column. Import this table into PowerPivot using Linked Tables and name it as Msr.

3 Create Linked table in powerpivot

There is no need to make any relations with the existing tables, and Msr table should be a standalone table.

2) Now make a new measure called MsrValue with the formula given below

MsrValue:=switch (TRUE,
Min ( Msr[MsrId] ) = 1,
Sum ( Fct[Sales] ),
Min ( Msr[MsrId] ) = 2,
Sum ( Fct[Quantity] ),
Min ( Msr[MsrId] ) = 3,
Sum ( Fct[Profit] )
)

4 New measure

This measure will check the value of the MsrId column, and then use the appropriate measure. However, it should be noted that when there are more than one value of MsrId (or if more than one measures are selected), only the measure having minimum value of MsrId would be displayed. So how will we display more than one measures on the pivot table? Read on.

3) With this very simple setup, we are ready to view the results in the Pivot table. Drag and drop MeasureName column into the Slicer, Year into the Column Labels and Country in the Row Labels. Then drag and drop MsrValue in the Values and we should have something similar to the image below. I have also included the fact rows above the pivot so that we can compare.

5 Drag column to pivot table and insert slicer

Now there are a couple of problems with this pivot table. You would have noticed that the pivot table works correctly if only one measure is selected, but if there are more than one selections for the measure name, it will only show the measure with the minimum of the MsrId (eg, Sales in the image above). Also, we would like to see the name of the measure in the column above for clarity. To solve both of these problems, follow the next step.

4) Just drag and drop the MeasureName column in the Column Labels above the Year.

7 Field list pane

Now you can see that multiple measures are displayed and also that the measure names are displayed in the column above.

6 Measure getting selected in pivot table as per the slicer selection

You can also change the formatting of the measures if needed

MsrValue:=switch (TRUE,
Min ( Msr[MsrId] ) = 1,
FORMAT ( Sum ( Fct[Sales] ), "$#,#0" ),
Min ( Msr[MsrId] ) = 2,
format ( Sum ( Fct[Quantity] ), "#,#0" ),
Min ( Msr[MsrId] ) = 3,
format ( Sum ( Fct[Profit] ), "$#,#0" )
)

8 formatted measures

Luckily, that wasn’t as hard as I thought (which means that I could still use my evening free time to focus on more important problems like whether the chicken or egg came first into the world!). If you don’t have such important problems to focus on like me, you can download the completed file from this link and test it out on your own!

9 comments:

  1. Technique does not seem to be working when file converted to Excel 2013.

    ReplyDelete
    Replies
    1. Check out this post - http://www.sqljason.com/2012/11/changing-pivottable-names-in-excel-2013.html
      You must be using the Excel Preview version like me, and looks like it doesn't handle the FORMAT function inside the measure.

      Delete
  2. @png,

    Which part is giving you problems? I just recreated it in 2013 and it works fine as far as I can see.

    ReplyDelete
  3. Used this technique on a PowerPivot table with < 200 rows (but a lot of columns). Included 17 measures in the SWITCH function. When added to a slicer, one selection worked fine but with a multiple selection, it choked on my 32-bit system. Apparently, the memory requirements for this are very high.

    ReplyDelete
  4. Does anyone know if there is an alternative if using PowerPivot 2008R2? The switch command does not seem to be available. We are currently stuck on PowerPivot 2008R2 until we can get SharePoint updated. I would appreciate thoughts- If I get an answer I'll loop everyone in!

    ReplyDelete
  5. Looks like I just answered my own question. I have the case where I only need to have one measure or the other, without having to worry about 2 concurrent selections. This works for me:

    IF (Min ( Msr[MsrId] ) = 1,Sum ( Fct[Sales] ),IF(Min ( Msr[MsrId] ) = 2,Sum ( Fct[Quantity] ),Sum ( Fct[Profit] )))

    ReplyDelete
  6. I am extremely interested in using this technique to help make dashboards able to display a number of different measures in Fixed amount of space. I have done my best to use your code to create a "slicer driven" measure selection in a model that contains a number of measures all displaying different calculations of web site "hits" (I use the term Impressions.

    My disconnected measure table is called MSR -- here's what it contains:
    Measures Msrid
    Available Imps 1
    Billable Imps 2
    CPM 3
    Direct Sold Imps 1P DFP 4
    Direct Sold Imps 3P 5
    House Imps 1P DFP 6
    Ordered Imps 7
    Remnant Imps 1P DFP 8
    Remnant Imps 3P 9
    Reported Imps 3P 10
    Stated Imps 1P 11
    Billable Imps Prv Period 12
    Billable Imps Same Prd Prev Yr 13



    Here is the code for my calculated measure: Note that I am simply reusing measures that I've created already instead of re-writing each measure calculated measure in the formula.


    =Switch (TRUE,
    Min( Msr[Msrid] )=1,
    [Available Imps],
    Min( Msr[Msrid] )=2,
    [Billable Imps],
    Min( Msr[Msrid] )=3,
    [CPM],
    Min( Msr[Msrid] )=4,
    [Direct Sold Imps 1P DFP],
    Min( Msr[Msrid] )=5,
    [Direct Sold Imps 3P],
    Min( Msr[Msrid] )=6,
    [House Imps 1P DFP],
    Min( Msr[Msrid] )=7,
    [Ordered Imps],
    Min( Msr[Msrid] )=8,
    [Remnant Imps 1P DFP],
    Min( Msr[Msrid] )=9,
    [Remnant Imps 3P],
    Min( Msr[Msrid] )=10,
    [Reported Imps 3P],
    Min( Msr[Msrid] )=11,
    [Stated Imps 1P],
    Min( Msr[Msrid] )=12,
    [Billable Imps Prv Period],
    Min( Msr[Msrid] )=13,
    [Billable Imps Same Prd Prev Yr])

    My issue is that the actual numbers displayed by the measures shown using the "selected formula" noted above, is different from what those same measures display when they are used independently. Worse, the independently placed measures show considerably more data. For example, I have values of the "independent measures for each month of the year. But, the measure values displayed through the selector process only shows 4 or 5 months.

    The "measure driven" values are always higher than the independent measures by themselves, but I can't see any other pattern. I have tried two different version of the formula above -- one used the calculated measure names themselves. The other used the formulas themselves.

    Also, none of the calculations are complex. THey are all simply sums or averages.

    Any Suggestions as I REALLY want to use this process.

    Peter Wickwire:
    prwindenver@gmail.com

    ReplyDelete
    Replies
    1. Would it be possible to share your powerpivot file to jason143@gmail.com so that I can take a look?

      Delete
  7. This worked great thanks!

    One small question is when I use this it doesnt filter other criteria down to those that have values. For example I am showing Gr Hrs by plant, filtered by year from another slicer. When I use the regular measure, it will only show plants that have hours for the given year, but when i use the switch formula, it shows all plants that have ever had hours, with the value being blank.

    Any way around this?

    ReplyDelete