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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
AaronToth
Resolver I
Resolver I

How to write DAX that uses TOPN AVERAGEX and FILTER

I have a DAX measure that is able to look at a table and get me the TOPN/AVERAGEX results

 

TOPN_AVG:= AVERAGEX(TOPN(151, Table1, Table1[column], DESC), Table1[column])

 

This is working fine but fails when I tried to add expression conditions or a filter to the DAX code.  If I add the condition in the expression, or a filter, I see the following error(s)

 

#errors

TOPN_AVG:= AVERAGEX(TOPN(151, Table1, Table1[column], DESC), Table1.Column2 = 'RED')

TOPN_AVG:= AVERAGEX(TOPN(151, Table1, Table1[column], DESC), FILTER(Table1, Table1.Column2 = 'RED')

AVERAGEX cannot work with type BOOLEAN

Multiple columns cannot be converted to a scaler

How do I add a two column filter/condition to this code?  I'm trying to capture the "SELECTEDVALUE" from two slicers, and have them added as filters to the code.

1 ACCEPTED SOLUTION

Thanks for your answer, but I was looking for something like this ->
In your example, you omitted the "SELECTEDVALUE" part.  I have added that below.  Together, we have solved this.

TOPN_AVG :=
AVERAGEX (
TOPN ( 151, FILTER ( Table1, Table1[Column2] = FILTER(Table1, Table1[column2] = SELECTEDVALUE[column2])), Table1[column], DESC ),
Table1[column]
)

View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your desired outcome looks like, but please try the below.

 

TOPN_AVG :=
AVERAGEX (
    TOPN ( 151, FILTER ( Table1, Table1[Column2] = 'RED' ), Table1[column], DESC ),
    Table1[column]
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thanks for your answer, but I was looking for something like this ->
In your example, you omitted the "SELECTEDVALUE" part.  I have added that below.  Together, we have solved this.

TOPN_AVG :=
AVERAGEX (
TOPN ( 151, FILTER ( Table1, Table1[Column2] = FILTER(Table1, Table1[column2] = SELECTEDVALUE[column2])), Table1[column], DESC ),
Table1[column]
)

Hi, @AaronToth 

If your problem has been solved, please accept your reply as solution to close this thread.

So that other community members will easily find the solution when they get the same issue.

If you stiil need help, please share more details.

 

Best Regards,
Community Support Team _ Eason

I have sent a message to the person who *thinks* they solved my issue.  As you can see, they didn't entirely answer it (SELECTEDVALUE) and I did.  I was not sure about approving my own solution, but now I will.

Thanks!

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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

Users online (2,516)