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
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
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!

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.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.