Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
otto-user101
Frequent Visitor

Create a slicer that filters data dynamically

I submitted a question yesterday for this with some helpful solutions, but I'm taking another jab to explain my situation better. The dataset includes Primary and Alternate (sub) records with attributes populated for all records. I'm looking for a solution that allows the user to see how the overall composition changes by way of visuals (pie chart/bar charts) if a single primary is replaced with an alternative (sub). The solution to my first question allows me to create a measure that populates a field within a table/matrix that achieves this. However, a measure can't be used as a slicer so here's my second go at this.
 

Base table

ID

 

Name

KeyAttribute/ColorIf Primary - Has SubOn Sale
1LuluPrimaryRedYesYes
1CaitlinSubBlueNoNo
1AsheSubYellowNoYes
2GarenPrimaryOrangeNoNo
3PoppyPrimaryBlackNoNo
4VeigarPrimaryWhiteYesYes
4LuxSubRedNoYes
5SorakaPrimaryBlueNoYes
6PoroPrimaryYellowYesNo
6AkshanSubOrangeNoNo
6MundoSubBlackNoNo

 

Disconnected Table = calculatetable(summarize('Primary Table','Primary Table'[Name]),filter('Primary Table','Primary Table'[If Primary - Has Sub]="Yes"))

Name
Lulu
Veigar
Poro

 

Slicer1 = 'Disconnected Table'[Name]
Desired Slicer = Displays Names where ID matches that of Slicer1 selection e.g., If Slicer1 = "Lulu" then 'Lulu', 'Caitlin', 'Ashe' is available for selection

Desired Outcome: 1) populates selected record in Desired Slicer excluding all others records where ID matches that of Slicer1 selection , 2)populate all records where Key = 'Primary' where ID does not match Slicer1 selection and excludes all Sub where ID does not match Slicer1 selection

If Slicer1 = 'Lulu' and Desired Slicer='Caitlin', populates table to include only Caitlin where ID = 1, else if key = 'Primary' include, else exclude

 

Filtered Primary Table if Slicer1 = "Lulu" & Desired Slicer = 'Caitlin'

IDNameKeyAttribute/ColorIf Primary - Has SubOn Sale
1CaitlinSubBlueNoNo
2GarenPrimaryOrangeNoNo
3PoppyPrimaryBlackNoNo
4VeigarPrimaryWhiteYesYes
5SorakaPrimaryBlueNoYes
6PoroPrimaryYellowYesNo

 

Filtered Primary Table if Slicer1 = "Lulu" & Desired Slicer = 'Lulu'

IDNameKeyAttribute/ColorIf Primary - Has SubOn Sale
1LuluPrimaryRedYesYes
2GarenPrimaryOrangeNoNo
3PoppyPrimaryBlackNoNo
4VeigarPrimaryWhiteYesYes
5SorakaPrimaryBlueNoYes
6PoroPrimaryYellowYesNo

 

Filtered Primary Table if Slicer1 = "Lulu" & Desired Slicer = 'Ashe'

IDNameKeyAttribute/ColorIf Primary - Has SubOn Sale
1AsheSubYellowNoYes
2GarenPrimaryOrangeNoNo
3PoppyPrimaryBlackNoNo
4VeigarPrimaryWhiteYesYes
5SorakaPrimaryBlueNoYes
6PoroPrimaryYellowYesNo

 

1 ACCEPTED SOLUTION
otto-user101
Frequent Visitor

For anyone looking to achieve the same in Power BI, I used crossjoin to create a copy of the base table then created a custom column to serve as my slicer field.

View solution in original post

3 REPLIES 3
v-junyant-msft
Community Support
Community Support

Hi @otto-user101 ,

Please try this way:
First I use this DAX to create a new table to create Desired Slicer:

 

Desired Slicer = SELECTCOLUMNS (
    'Primary Table',
    "ID", 'Primary Table'[ID],
    "Name", 'Primary Table'[Name]
)

 

vjunyantmsft_0-1704077475049.png


Then I create a measure by using this DAX in table Desired Slicer:

 

Measure = 
VAR A = CALCULATE(
    MAX('Desired Slicer'[ID]),
    FILTER(
        ALLSELECTED('Desired Slicer'),
        'Desired Slicer'[Name] = SELECTEDVALUE('Disconnected Table'[Name])
    )
)
RETURN

IF(
    ISFILTERED('Desired Slicer'), 
    IF(
        SELECTEDVALUE('Desired Slicer'[ID]) = A,
        1,
        0
    ),
    0
)

 

vjunyantmsft_1-1704077594513.png

And the Desired Slicer is created, sorry I didn't change the name, you can change it yourself:

vjunyantmsft_2-1704077622149.png

 

Then I create a measure by using DAX below in Primary Table:

 

Measure 2 = 
VAR A = CALCULATE(
    MAX('Primary Table'[ID]),
    FILTER(
        ALLSELECTED('Primary Table'),
        'Primary Table'[Name] = SELECTEDVALUE('Disconnected Table'[Name])
    )
)
RETURN
IF(
    ISFILTERED('Desired Slicer'[Name]) && ISFILTERED('Disconnected Table'[Name]),
    IF(
        SELECTEDVALUE('Primary Table'[Name]) = SELECTEDVALUE('Desired Slicer'[Name]),
        1,
        IF(
            SELECTEDVALUE('Primary Table'[ID]) <> A && SELECTEDVALUE('Primary Table'[Key]) = "Primary",
            1,
            0
        )
    ),
    0
)

 

 

vjunyantmsft_3-1704077800779.png

The final result is shown below:

vjunyantmsft_4-1704077858729.png

 

vjunyantmsft_5-1704077866233.png

 

vjunyantmsft_6-1704077873209.png


Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

otto-user101
Frequent Visitor

For anyone looking to achieve the same in Power BI, I used crossjoin to create a copy of the base table then created a custom column to serve as my slicer field.

AmiraBedh
Resident Rockstar
Resident Rockstar

DAX does not allow direct filtering between tables without a relationship, the logic says that you need to establish a connection.
One way to do this is by creating a column in your primary table that matches the values in the disconnected table.

You can create a new column in your primary table that identifies if the record is related to one of the names in the disconnected table. This can be a simple boolean flag (True/False).

Then, ceate measures that dynamically filter the primary table based on the selection in Slicer1 and the Desired Slicer.

Use these measures in your visuals to reflect the changes based on the slicer selections.

I don't have your model so I am imagining something like this :

Connected Column =
IF(
'Primary Table'[Name] IN VALUES('Disconnected Table'[Name]),
TRUE(),
FALSE()
)

Filtered Data Measure =
VAR selectedPrimary = SELECTEDVALUE('Disconnected Table'[Name])
VAR selectedSub = SELECTEDVALUE('Primary Table'[Name], "All")
RETURN
CALCULATE(
COUNTROWS('Primary Table'),
FILTER(
'Primary Table',
IF(
'Primary Table'[Key] = "Primary" && 'Primary Table'[Connected Column] = TRUE(),
'Primary Table'[Name] = selectedPrimary || selectedSub = "All",
'Primary Table'[Key] = "Primary"
)
)
)

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.