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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
leems101
Frequent Visitor

Returning Value of Table Based on Ranking

Hi all

 

just trying to figure out something basic which is racking my brain..

 

i have a simple table  eg:

 

Article  Source            Views

A           Home             10

A           Twitter             5

A           Facebook         4

A           Instagram         1

 

Basically, i want to return a table that looks like

 

Article  Total Views     Source 1 (Ranking1)                           Source 2 (ranking 2)                      Source 3 (ranking 3)

A           20                 Home 10 Views (50% of total)            Twitter 5 Views (25% of Total)          Facebook 4 views (20% of total)  etc etc

 

 

Given there will be mutiple different articles...

 

ive tried to do this using a rankx function but the rank only works if i add in the source to the table..

can anyone help.

many thanks

 

J

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @leems101 

 

You can create a calculated colum rank within partitioned by each article and another calculated column for the total views.

Rank = 
RANKX (
    FILTER ( 'Table', 'Table'[Article] = EARLIER ( 'Table'[Article] ) ),
    'Table'[ Views],
    ,
    DESC,
    DENSE
)
Total Views by Article = 
CALCULATE ( SUM ( 'Table'[ Views] ), ALLEXCEPT ( 'Table', 'Table'[Article] ) )

And a measure to return concatenated values

My Value = 
VAR _source =
    SELECTEDVALUE ( 'Table'[Source] ) & " "
VAR _views =
    SUM ( 'Table'[ Views] )
VAR _percent =
    FORMAT (
        DIVIDE (
            _views,
            CALCULATE ( SUM ( 'Table'[ Views] ), ALLEXCEPT ( 'Table', 'Table'[Source] ) )
        ),
        "0%"
    )
RETURN
    _source & FORMAT ( _views, "#,0" ) & " Views " & "(" & _percent & " of total)"

danextian_0-1741603525869.png

Note: Since Total Views and Rank are calculated columns, this approach is not fully dynamic, as calculated columns do not respond to slicer selections. For example, excluding Home using a slicer will not update these values. A more dynamic solution would require a disconnected table and more complex DAX, which involves additional development effort.

 

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

3 REPLIES 3
danextian
Super User
Super User

Hi @leems101 

 

You can create a calculated colum rank within partitioned by each article and another calculated column for the total views.

Rank = 
RANKX (
    FILTER ( 'Table', 'Table'[Article] = EARLIER ( 'Table'[Article] ) ),
    'Table'[ Views],
    ,
    DESC,
    DENSE
)
Total Views by Article = 
CALCULATE ( SUM ( 'Table'[ Views] ), ALLEXCEPT ( 'Table', 'Table'[Article] ) )

And a measure to return concatenated values

My Value = 
VAR _source =
    SELECTEDVALUE ( 'Table'[Source] ) & " "
VAR _views =
    SUM ( 'Table'[ Views] )
VAR _percent =
    FORMAT (
        DIVIDE (
            _views,
            CALCULATE ( SUM ( 'Table'[ Views] ), ALLEXCEPT ( 'Table', 'Table'[Source] ) )
        ),
        "0%"
    )
RETURN
    _source & FORMAT ( _views, "#,0" ) & " Views " & "(" & _percent & " of total)"

danextian_0-1741603525869.png

Note: Since Total Views and Rank are calculated columns, this approach is not fully dynamic, as calculated columns do not respond to slicer selections. For example, excluding Home using a slicer will not update these values. A more dynamic solution would require a disconnected table and more complex DAX, which involves additional development effort.

 

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
bhanu_gautam
Super User
Super User

@leems101 First, create a calculated column to get the total views for each article.

Total Views = CALCULATE(SUM('Table'[Views]), ALLEXCEPT('Table', 'Table'[Article]))

 

Next, create a calculated column to rank the sources based on the number of views for each article.

Rank = RANKX(FILTER('Table', 'Table'[Article] = EARLIER('Table'[Article])), 'Table'[Views], , DESC, DENSE)

 

Then, create a calculated column to calculate the percentage of total views for each source.

Percentage of Total = DIVIDE('Table'[Views], 'Table'[Total Views])

 

inally, create a pivot table to display the data in the desired format. You can use the matrix visualization in Power BI for this purpose.

Place Article in the Rows.
Place Total Views in the Values.
Place Source in the Columns.
Place Views and Percentage of Total in the Values.




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






thank you for the quick reply. the Earlier section of the rank is returning and error....Earlier /Earliest refers to an erlier row context which doesnt exist...

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.