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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Using DAX variables in the filter function

Hello,

I'm trying to create a measure and at a given moment I use the following expression:

 

Top =
VAR Nth = 2
VAR Category = ALLSELECTED(Table[Category])
VAR Sales =
ADDCOLUMNS(
VALUES(Table[Category]),,"Rank",RANKX(Category,[Total Sales]))

VAR FilterNThCategory = FILTER(Sales , [Rank] = Nth)
VAR NthLineName = MINX(FilterNThLine,Table[Category])
VAR Subtop = CALCULATE([Total Sales],Table[Category] = NthLineName)

return Subtop

 

in theory the return of the variable "NthLineName" is a string, but when I use it inside the FILTER function, I don't get the expected result. However, when I replace "NthLineName" with the string "Cars", it works perfectly.

Does the FILTER function not support the use of variables or am I doing something wrong? is there any way to get around it?

thank you very much in advance

 

1 ACCEPTED SOLUTION
v-cazheng-msft
Community Support
Community Support

Hi @Anonymous 

 

Please make a little bit change to your measure.

Top =

VAR Nth = 2

VAR Category =

    ALLSELECTED ( 'Table'[Category] )

VAR Sales_ =

    ADDCOLUMNS(

        VALUES ('Table'[Category]),

        "Rank", RANKX ( Category, [Total Sales] )

    )

VAR FilterNThCategory =

    FILTER(Sales_,[Rank]=Nth)

VAR NthLineName =

    MINX(FilterNThCategory,'Table'[Category])

VAR Subtop =

    CALCULATE ( [Total Sales], 'Table'[Category] =NthLineName)

RETURN

    Subtop

 

Result should look like this:

vcazhengmsft_0-1636685613141.png

Attached the pbix file as reference.

 

Best Regards,

Community Support Team _ Caiyun

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If you still have problems on it, please feel free to let us know. Thanks a lot!

 

View solution in original post

2 REPLIES 2
v-cazheng-msft
Community Support
Community Support

Hi @Anonymous 

 

Please make a little bit change to your measure.

Top =

VAR Nth = 2

VAR Category =

    ALLSELECTED ( 'Table'[Category] )

VAR Sales_ =

    ADDCOLUMNS(

        VALUES ('Table'[Category]),

        "Rank", RANKX ( Category, [Total Sales] )

    )

VAR FilterNThCategory =

    FILTER(Sales_,[Rank]=Nth)

VAR NthLineName =

    MINX(FilterNThCategory,'Table'[Category])

VAR Subtop =

    CALCULATE ( [Total Sales], 'Table'[Category] =NthLineName)

RETURN

    Subtop

 

Result should look like this:

vcazhengmsft_0-1636685613141.png

Attached the pbix file as reference.

 

Best Regards,

Community Support Team _ Caiyun

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If you still have problems on it, please feel free to let us know. Thanks a lot!

 

AlexisOlson
Super User
Super User

Does it help if you replace VALUES ( Table[Category] ) with Category?

 

If not, then can you provide some example data and desired results to test against?

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.

Top Solution Authors