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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Loubot3000
Resolver II
Resolver II

How do I rank a filtered table?

TL;DR:
I have an unpivoted table of ads data called 'Ads Shown':
Loubot3000_0-1702306524695.png

'Attribute' column is the ad name. 'Value' column is either 'Selected', 'Not selected', or 'Missing data', depending on whether that UNIQUEID has seen that ad.

 

I also have this table of new ad names called 'New Ads':

Loubot3000_2-1702306778197.png

 

I want to show the top 4 new ads based on some metric.

 

Here's my attempt so far:

The following measure returns 1 for ads that are in that new ads list, else 0.

 

New ads selection logic = 
VAR AdName = SELECTEDVALUE('Ads Shown'[Attribute])
VAR IsNewAd = 
    IF(
        CALCULATE(
            COUNTROWS('New Ads'),
            'New Ads'[Latest New Ads] = AdName
        ) > 0,
        1,
        0
    )
RETURN
    IsNewAd  // Return 1 for latest new ads, else return 0

 

I can confirm that the code is working correctly.

 

By putting the New Ads Selection measure as a filter into a graph that contains data from 'Ads Shown', and setting it equal to 1, you can filter it to just new ads. Like so:

Loubot3000_3-1702307972936.png

Those bars indeed correspond only to the new ads. In this case, 'Ad metric' is just a measure that switches between different percentage metrics depending on the 'Hayu Metrics' filter. So in this case, it's just showing the percentage of people who selected "Love it" for the ads.

 

I made the following measure to rank the ads based on the selected metric:

 

New ads rank = 
VAR New_Ads = FILTER('Ads Shown', [New ads selection logic] = 1)
RETURN RANKX(New_Ads,[Ad metric], , DESC, Dense)

 

 

When I put this in the graph, it does seem to filter it to only new ads, but not the top 4:

Loubot3000_1-1702306628503.png

 

Other variations of the rank measure I've tried seem to either not load, throw an error, or show all ads.

 

Anyone got any ideas?

1 ACCEPTED SOLUTION
Loubot3000
Resolver II
Resolver II

I seem to have found a simple solution in my case. What a relief.

 

The TOPN filter would normally take my [Ad Metric] measure to determine what values to sort by. In this case I only wanted the values for new ads, so I made a new measure that calculates that like so:

New Ads (Ad Metric) = CALCULATE([Ad metric], FILTER('Ads Shown', [New ads selection logic]))

And put it in the graph like so:

Loubot3000_0-1702314268054.png

 

It's a miracle that the filter argument 'FILTER('Ads Shown', [New ads selection logic])' actually worked. Like, it's logical that it should work, since [New ads selection logic] is just a boolian, so it sort of creates a mask to filter the table by. Maybe my mistake before was writing '[New ads selection logic] = 1'?

 

But Power BI has this amazing way of not working for very simple obvious things, or making very simple obvious things extraordinarilly difficult. So I'm shocked that it worked.

View solution in original post

3 REPLIES 3
Loubot3000
Resolver II
Resolver II

I seem to have found a simple solution in my case. What a relief.

 

The TOPN filter would normally take my [Ad Metric] measure to determine what values to sort by. In this case I only wanted the values for new ads, so I made a new measure that calculates that like so:

New Ads (Ad Metric) = CALCULATE([Ad metric], FILTER('Ads Shown', [New ads selection logic]))

And put it in the graph like so:

Loubot3000_0-1702314268054.png

 

It's a miracle that the filter argument 'FILTER('Ads Shown', [New ads selection logic])' actually worked. Like, it's logical that it should work, since [New ads selection logic] is just a boolian, so it sort of creates a mask to filter the table by. Maybe my mistake before was writing '[New ads selection logic] = 1'?

 

But Power BI has this amazing way of not working for very simple obvious things, or making very simple obvious things extraordinarilly difficult. So I'm shocked that it worked.

amitchandak
Super User
Super User

@Loubot3000 , You can create a TOPN measure based on TOPN function

 

M1= calculate([Your measure], keepfilters(topn(10, allselected(Table[Attribute]), [Your measure], desc) )

 

 

TOPN: https://youtu.be/QIVEFp-QiOk
TOPN with Numeric Parameter -https://youtu.be/cN8AO3_vmlY?t=26448


You can also use window function
Dynamic TOPN using TOPN/Window and Numeric parameter: https://youtu.be/vm2mdEioQPQ

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hey thanks for this! I'm not entirely sure how to plug the code you wrote into my situation? Which measures go where? How do I ensure it's only showing ads that are in the New Ads list?

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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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