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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
robmarsh
Frequent Visitor

Conditional filtering on unrelated table

I am looking for a way to conditionally summarise and filter a table based on slicer selections in another, unrelated table.

 

My Sales fact table that stores transactions looks like the simplified data set below:

 

DateCategorySub-CategoryValue
01/01/2024   SportsFootball1000
01/01/2024SportsFootball2000
02/01/2024SportsRugby500
03/01/2024GamesBackgammon350
03/01/2024SportsTennis600
03/01/2024GamesBackgammon1250
04/01/2024GamesChess750
04/01/2024GamesBackgammon500
05/01/2024SportsFootball300

 

I then have two slicers setup on the Category and Sub-Category columns in the Sales fact table.

 

I also have an unrelated table called Targets that look like below:

 

CategorySub-CategoryTarget Value
SportsFootball1000
SportsRugby1500
SportsTennis750
GamesBackgammon1250
GamesChess500

 

What I need is a measure that conditionalliy summarises the Target Value on the Targets table based on the slicer selections applied to the Sales table. So, for example, if no selections were made on the slicers, the measure would be return the sum of all the row values in the Targets table = 5000. If the user selected "Sports" in the Category slicer, the measure would return a total value = 3250. If the user removed the slicer on the Catgory, but selected "Backgammon" on the Sub-Category slicer then the measure would return a value = 1250.

 

I got as far as creating the measure that used variables to store the selected values on the slicers, and then calculate the vales based on those selections like this:

 

 

measure = 

var SelectedCategory = SELECTEDVALUE('Sales'[Category])
var SelectedSubCategory = SELECTEDVALUE('Sales'[Sub-Category])

var TargetValue = CALCULATE(
    SUM('Targets'[Target Value]),
    FILTER(
        'Targets',
        'Targets'[Catgeory] = SelectedCategory &&
        'Targets'[Sub-Category] = SelectedSubCategory 
    )
)

RETURN TargetValue 

 

 

This works, but obviously only if there are selected values in both slicers. If possible, I want to try and avoid doing multiple nested IF expressions based on whether r not each slicer has a selected value, my actual scenario is more complex with more slicers, so would require more permutations and combinations to nest! My background is database programming, so I instantly thought of writing dynamic DAX statements, but after a bit of searching it appears there isn't an equivalent in DAX. I am also thinking I might be missing a really simple way to do this, but my head is stuck in overthinking mode, so any help greatly appreciated!

3 REPLIES 3
robmarsh
Frequent Visitor

Thanks both for your replies. After doing some testing, both suggestions work, although the ALLSELECTED() function in place of SELECTEDVALUE() in combination with using IN() with the CALCULATE() function is closer to what I was hoping to achieve.

 

There was also another way too which was to create two dimension tables with one column for Category and Sub-Category. Then in the model, create relationships between the Sales fact table and also the Targets table too. Then change the slicers to run from these Category and Sub-Category dimension tables. and the filters propagate through to both the Sales and Targets table. This also has the advantage of my slicer options not jumping around depending on previous slicer selections (I think from reading previous posts, slicers on dimension tables is better practice than slicers on fact tables, partly for this reason).

 

Thanks for the help though 🙂

talespin
Solution Sage
Solution Sage

hi @robmarsh 
 
Create this measure:
 
Summ =
VAR _SelectedCat = ALLSELECTED('Fact'[Category])
VAR _SelectedSubCat = ALLSELECTED('Fact'[SubCategory])

RETURN CALCULATE(SUM(Summary[Target Value]), Summary[Category] IN _SelectedCat && Summary[SubCategory] IN _SelectedSubCat)
 
talespin_0-1708593992957.pngtalespin_1-1708594031695.png

 

lbendlin
Super User
Super User

read about TREATAS and VALUES.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.