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
Perberos
New Member

Create visual with column name of top n values

Hi, everyone. I need some help.

I have a database called "Base" with some columns:
1 - ID
2 - Company: Company name 
3 - Origin: List of origin cities
4 - Destination: List of destination cities
5 - Amount(kg): Amount of Kg transported

Perberos_2-1699416638142.png

(Example - Base database)

I want to create a table as follows:

  • In the first column, it should contain the names of the companies.
  • The second column should be called "Top 1" and should specify the name of the destination with the highest sum of transported Kg
  • The third column, called "Top 2," should specify the name of the destination with the SECOND highest sum of transported Kg.
  • The fourth, fifth, and so on should follow the same logic.

Perberos_3-1699416680080.png

(Desired visual - Example)

I have tried the code below, but without success.

Destino Mais Utilizado = 
VAR DestinoMaisUtilizado =
    TOPN(1,
        SUMMARIZE('Base', 'Base'[Destination], "Total KG Carga Paga", SUM('Base'[Amount (kg)])),[Total KG Carga Paga], DESC)

RETURN
    SELECTCOLUMNS(DestinoMaisUtilizado, "Destino Mais Utilizado", 'Base'[Destination])

 

Thanks!

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi, I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.

Jihwan_Kim_1-1699420986206.png

 

 

Destination topN table = 
VAR _rowcount =
    ADDCOLUMNS (
        VALUES ( Base[Company] ),
        "@rowcount", CALCULATE ( COUNTROWS ( Base ) )
    )
VAR _maxrowcount =
    MAXX ( _rowcount, [@rowcount] )
VAR _createNtable =
    ADDCOLUMNS (
        ADDCOLUMNS ( GENERATESERIES ( 1, _maxrowcount, 1 ), "@Top", "Top " ),
        "@metricsname", [@Top] & [Value]
    )
VAR _addcompanyname =
    GENERATE ( Base, _createNtable )
VAR _addresultcolumn =
    FILTER (
        ADDCOLUMNS (
            _addcompanyname,
            "@result",
                CALCULATE (
                    SUM ( Base[Amount (kg)] ),
                    WINDOW (
                        [Value],
                        ABS,
                        [Value],
                        ABS,
                        FILTER ( Base, Base[Company] = EARLIER ( Base[Company] ) ),
                        ORDERBY ( CALCULATE ( SUM ( Base[Amount (kg)] ) ), DESC ),
                        ,
                        ,
                        MATCHBY ( Base[ID] )
                    )
                )
        ),
        Base[Amount (kg)] = [@result]
    )
RETURN
    SUMMARIZE (
        _addresultcolumn,
        Base[ID],
        Base[Company],
        Base[Destination],
        [@metricsname],
        [@result]
    )

 

Jihwan_Kim_3-1699421030213.png

 


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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

1 REPLY 1
Jihwan_Kim
Super User
Super User

Hi, I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.

Jihwan_Kim_1-1699420986206.png

 

 

Destination topN table = 
VAR _rowcount =
    ADDCOLUMNS (
        VALUES ( Base[Company] ),
        "@rowcount", CALCULATE ( COUNTROWS ( Base ) )
    )
VAR _maxrowcount =
    MAXX ( _rowcount, [@rowcount] )
VAR _createNtable =
    ADDCOLUMNS (
        ADDCOLUMNS ( GENERATESERIES ( 1, _maxrowcount, 1 ), "@Top", "Top " ),
        "@metricsname", [@Top] & [Value]
    )
VAR _addcompanyname =
    GENERATE ( Base, _createNtable )
VAR _addresultcolumn =
    FILTER (
        ADDCOLUMNS (
            _addcompanyname,
            "@result",
                CALCULATE (
                    SUM ( Base[Amount (kg)] ),
                    WINDOW (
                        [Value],
                        ABS,
                        [Value],
                        ABS,
                        FILTER ( Base, Base[Company] = EARLIER ( Base[Company] ) ),
                        ORDERBY ( CALCULATE ( SUM ( Base[Amount (kg)] ) ), DESC ),
                        ,
                        ,
                        MATCHBY ( Base[ID] )
                    )
                )
        ),
        Base[Amount (kg)] = [@result]
    )
RETURN
    SUMMARIZE (
        _addresultcolumn,
        Base[ID],
        Base[Company],
        Base[Destination],
        [@metricsname],
        [@result]
    )

 

Jihwan_Kim_3-1699421030213.png

 


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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

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!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

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.