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
TMason
Frequent Visitor

Dynamically change column in a filter

Hi, I need help with a DAX problem.

I have a calculate measure that has a filter.

The calculate expression needs to stay the same, however the filter needs to dynamically change. I need to change which column I am filtering on, whilst maintaining the criteria to filter.
In the simplified example table below, I'd want a measure that counts the number of products sold for Product 3 if Product 1 is also Yes. I then want to dynamically change the measure (using some form of slicer) so that it still counts the number of Product 3 sold if Product 4 is Yes.

So in the first scenario the outcome would be 3 (because it's counting the number of yes's for Product 3, so AAA is excluded because it's a No & company CCC doesn't have a yes for product 1 and therefore is also excluded), while in the second Scenario the outcome would be 2 (because only companies DDD and EEE have a yes for product 4).

 

Company Name

Product 1Product 2Product 3Product 4

AAA

YesNoNoNo
BBBYesNoYesNo
CCCNoYesYesNo
DDDYesNoYesYes
EEEYesYesYesYes

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @TMason ,

Please try below steps:

1. below is my test table

Table:

vbinbinyumsft_0-1708397522240.png

Table 2:

vbinbinyumsft_1-1708397533303.png

 

2. create a measure with below dax formula

Dynamic Product Count =
VAR SelectedProduct =
    SELECTEDVALUE ( 'Table'[Product], "Product 1" )
VAR FilterCondition =
    SWITCH (
        SelectedProduct,
        "Product 1",
            CALCULATE (
                COUNTROWS ( 'Table 2' ),
                'Table 2'[Product 1] = "Yes"
                    && 'Table 2'[Product 3] = "Yes"
            ),
        "Product 2",
            CALCULATE (
                COUNTROWS ( 'Table 2' ),
                'Table 2'[Product 2] = "Yes"
                    && 'Table 2'[Product 3] = "Yes"
            ),
        "Product 3", CALCULATE ( COUNTROWS ( 'Table 2' ), 'Table 2'[Product 3] = "Yes" ),
        "Product 4",
            CALCULATE (
                COUNTROWS ( 'Table 2' ),
                'Table 2'[Product 4] = "Yes"
                    && 'Table 2'[Product 3] = "Yes"
            ),
        CALCULATE ( COUNTROWS ( 'Table 2' ), 'Table 2'[Product 3] = "Yes" )
    )
RETURN
    FilterCondition

 

3. add a slicer with Table field, add a card visual with measure

vbinbinyumsft_2-1708397630698.png

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @TMason ,

Please try below steps:

1. below is my test table

Table:

vbinbinyumsft_0-1708397522240.png

Table 2:

vbinbinyumsft_1-1708397533303.png

 

2. create a measure with below dax formula

Dynamic Product Count =
VAR SelectedProduct =
    SELECTEDVALUE ( 'Table'[Product], "Product 1" )
VAR FilterCondition =
    SWITCH (
        SelectedProduct,
        "Product 1",
            CALCULATE (
                COUNTROWS ( 'Table 2' ),
                'Table 2'[Product 1] = "Yes"
                    && 'Table 2'[Product 3] = "Yes"
            ),
        "Product 2",
            CALCULATE (
                COUNTROWS ( 'Table 2' ),
                'Table 2'[Product 2] = "Yes"
                    && 'Table 2'[Product 3] = "Yes"
            ),
        "Product 3", CALCULATE ( COUNTROWS ( 'Table 2' ), 'Table 2'[Product 3] = "Yes" ),
        "Product 4",
            CALCULATE (
                COUNTROWS ( 'Table 2' ),
                'Table 2'[Product 4] = "Yes"
                    && 'Table 2'[Product 3] = "Yes"
            ),
        CALCULATE ( COUNTROWS ( 'Table 2' ), 'Table 2'[Product 3] = "Yes" )
    )
RETURN
    FilterCondition

 

3. add a slicer with Table field, add a card visual with measure

vbinbinyumsft_2-1708397630698.png

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
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.