I’m using the TOPN DAX function in a measure that shows the correct result when it is placed in a table.
When i place the measure in a matrix and add a column it shows all values (Including blankls) how would i apply the filter context to the column as well?
Not looking for a solution as such but any information so I can read and apply would be appreciated.
As always, you come with interesting questions and problems.
This one is tricky to debug – I’ve attached a solution, but if you want to work through it on your own, the hints I would give would be to check the context of your Top2 table, and also to look at the IN operator (SQL BI has an excellent article on this).
I’m assuming this is the outcome you’re looking for:
Well, I’ll not distract you by posting the code since you want to try the solution on your own first.
But as @BrianJ suggested, I can give you a hint that you can adopt the Ranking method more efficiently than using the TOPN logic. And if you don’t want to write any of the code than you can also achieve the same results using the Filter Pane as well.
I’m also attaching the working of the PBIX file for the reference purposes, in case you want to review the code.
Well, sure - if you want to do this in 30 seconds without writing two dozen lines of complex virtual table DAX…but where’s the fun in that?
Also, really like your alternative approach of using RankX rather than TopN. These two posts together make for a very interesting combination/comparison of the difference between the two, but how they often can be used to get to the end result.
Great stuff! Thanks very much for posting these alternative approaches - I think the ones with multiple solutioins are often the best and most informative forum threads.
@brian
Thank you for the hint and i nearly figured it out with your hint.
@Harsh
Thank you for ranking solution. I initially started off using the RANKX function but ran into the same issue. So decided to first try out something simipler with TOPN (You have no idea how many times i’ve scrubbed through Sam’s RANKX & TOPN videos )
Whilst the filter pane is a goode idea, I wanted to write and understand it using DAX. This makes it easier to also incorporate parameters and make the values dynamic.