pivot-tablepowerquerypowerpivotslicersexcel-2019

Link Excel pivot table to slicer with many-to-many relationship with PowerPivot or PowerQuery


I'm struggling to figure out the correct Excel data model relationship technique to link a slicer to a pivot table in a two-step process.

Here is my simple Excel file to download.

There are three fields in my example data: Person, Activity, and Cost. I want a way for users to pick an Activity (or multiple Activities) in the Activity slicer, and then show the full Cost and Activity data for any Person who does that Activity. Here is a screenshot of the data table (1.), and its full pivot table (2.). The output I desire is mocked up in (3.). It's basically a subset of (2.). The complication is that I want to show the full record (ie all Activities) for the Person, not just the Activity selected by the user in the slicer. So there's a relationship from Slicer Activity -> Person -> All Activities per person/

Screenshot of data and desired outcome

I can can easily break the process down into some simple joins and relationships, but I'm unsure how to implement the below in Power Pivot or Power Query. It must update the final table (Step 4) whenever the slicer is updated. I'm just not sure how to bridge from the Activity slicer to the Person table, and then from the Person table back to the source Data.

screenshot of data model stages

Can this be done simply in Power Pivot?


Solution

  • You are going to need to use the Data Model in Excel, with a Measure, to achieve this. Here is an overview of the steps, then followed by the detail.

    1. Load your data table into PowerQuery, then we'll duplicate it to have a new table of just the distinct Activity values. We will use the new table for the slicer only.
    2. From PowerQuery, load the data into the Data Model (only). We won't be using any relationships. We will create one measure to return the cost

    Here are the steps:

    1. Load table into PowerQuery


    2. Create measure in the Data Model

    Total Cost:=
      var persons = CALCULATETABLE(
        VALUES(Activity[Person]),
        ALL('Activity'),
        TREATAS( VALUES('Activity Slicer'[Activity]), 'Activity'[Activity] )
      )
      var result = CALCULATE(
        SUM('Activity'[Cost £k]),
        FILTER(Activity,  'Activity'[Person] IN persons  )
      )
      
      RETURN result
    

    That's it - no relationships needed. Close the Power Pivot window and return to main Excel.

    3. Create/add Pivot and Slicer

    That's it - test!

    enter image description here