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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
zkrl
Frequent Visitor

DAX Concatenate values in rows with same ID and country name by top 3 values

Hi,

 

I have a table 

 

Project ID     Country          Values        CountryCode

 

P111             America          126            A

P111             America          222            A 

P111             UK                  572             U

P111             UK                  36               U

P111             Brazil              665             B

P111             France            13               F

P222             America          987             A

P222             Spain              65               S

P222             Italy                77               I

P222             Italy                364             I

P222             Germany        15               G

P333             UK                  872             U

P333             UK                  562             U

P333             Spain              212             S

 

 

And I need this

 

Project ID      Country         Sum of Values        Codes

 

P111              Brazil             665                         B U A 

P111              UK                 608                         B U A

P111              America         348                         B U A

P222              America         987                         A I S 

P222              Italy               441                         A I S

P222              Spain             65                           A I S

P333              UK                 1434                       U S

P333              Spain             212                         US

 

 

I tried use this DAX code

 

Codes = CONCATENATEX (
FILTER (
SUMMARIZE ( 'Table1'; 'Table1'[Project ID]; [CountryCode] );
[Project ID] = EARLIER ( 'Table1'[Project ID] )
);
'Table1'[CountryCode];
" "
)

 

 

but it concatenates all codes not only top 3.

 

Thanks for help I would appreciate any Idea.

zkrl

2 ACCEPTED SOLUTIONS
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @zkrl,

 

There are two solutions as a calculated column and a measure. Please give it a try.

Calculated Column =
CALCULATE (
    CONCATENATEX (
        TOPN (
            3,
            SUMMARIZE (
                'Table1',
                Table1[Project ID],
                Table1[Country],
                'Table1'[CountryCode],
                "total", SUM ( Table1[Values] )
            ),
            [total], DESC
        ),
        [CountryCode],
        "-",
        [total], DESC
    ),
    ALLEXCEPT ( Table1, Table1[Project ID] )
)
Measure Codes =
CALCULATE (
    CONCATENATEX (
        TOPN (
            3,
            SUMMARIZE (
                'Table1',
                Table1[Project ID],
                Table1[Country],
                'Table1'[CountryCode],
                "total", SUM ( Table1[Values] )
            ),
            [total], DESC
        ),
        [CountryCode],
        "-",
        [total], DESC
    )
)

DAX_Concatenate_values_in_rows_with_same_ID_and_country_name_by_top_3_values

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Hi @zkrl,

 

Try this formula please. And check out the attached demo.

One precondition: One country only has one CountryCode.

Measure Codes =
CALCULATE (
    CONCATENATEX (
        TOPN (
            3;
            SUMMARIZE (
                'Table1';
                Table1[Project ID];
                Table1[Country];
                "CountryCode"; IF (
                    ISBLANK ( MIN ( 'Table1'[CountryCode] ) );
                    "-";
                    MIN ( 'Table1'[CountryCode] )
                );
                "total"; SUM ( Table1[Values] )
            );
            [total]; DESC
        );
        [CountryCode];
        " ";
        [total]; DESC
    )
)

DAX_Concatenate_values_in_rows_with_same_ID_and_country_name_by_top_3_values2

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
zkrl
Frequent Visitor

Thank you

 

 

 

 

Project ID     Country          Values        CountryCode

 

P111             America          126             A

P111             America          222             A

P111             UK                  572             U

P111             UK                  36               U

P111             Brazil              665             

P111             France            13               F

 

Should be like this

 

Project ID      Country         Sum of Values        Codes

 

P111              Brazil             665                         - U A 

P111              UK                 608                         - U A

P111              America         348                         - U A

 

 

But shows me this

 

Project ID      Country         Sum of Values        Codes

 

P111              UK                  608                         U A F

P111              America         348                         U A F

P111              France            13                           U A F

 

 

 

Thanks for help

zkrl

Hi @zkrl,

 

Try this formula please. And check out the attached demo.

One precondition: One country only has one CountryCode.

Measure Codes =
CALCULATE (
    CONCATENATEX (
        TOPN (
            3;
            SUMMARIZE (
                'Table1';
                Table1[Project ID];
                Table1[Country];
                "CountryCode"; IF (
                    ISBLANK ( MIN ( 'Table1'[CountryCode] ) );
                    "-";
                    MIN ( 'Table1'[CountryCode] )
                );
                "total"; SUM ( Table1[Values] )
            );
            [total]; DESC
        );
        [CountryCode];
        " ";
        [total]; DESC
    )
)

DAX_Concatenate_values_in_rows_with_same_ID_and_country_name_by_top_3_values2

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you for the help.

 

zkrl

v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @zkrl,

 

There are two solutions as a calculated column and a measure. Please give it a try.

Calculated Column =
CALCULATE (
    CONCATENATEX (
        TOPN (
            3,
            SUMMARIZE (
                'Table1',
                Table1[Project ID],
                Table1[Country],
                'Table1'[CountryCode],
                "total", SUM ( Table1[Values] )
            ),
            [total], DESC
        ),
        [CountryCode],
        "-",
        [total], DESC
    ),
    ALLEXCEPT ( Table1, Table1[Project ID] )
)
Measure Codes =
CALCULATE (
    CONCATENATEX (
        TOPN (
            3,
            SUMMARIZE (
                'Table1',
                Table1[Project ID],
                Table1[Country],
                'Table1'[CountryCode],
                "total", SUM ( Table1[Values] )
            ),
            [total], DESC
        ),
        [CountryCode],
        "-",
        [total], DESC
    )
)

DAX_Concatenate_values_in_rows_with_same_ID_and_country_name_by_top_3_values

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.