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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Combine rows and add as a new column

Hi,

 

I have a table like the following:

 

IndexIDCategory
1ID_1A
2ID_2B
3ID_2C
4ID_3A
5ID_4F
6ID_5E
7ID_5R
8ID_5T
9ID_6 
10ID_7Q
11ID_8S
12ID_8 
13ID_9Z

 

I want to add a new column and combine rows of the category with the same ID into one cell. In the new cell, the combined categories are separated by a break (new line). So, the result is this:

 

IndexIDCategoryCategory_new
1ID_1AA
2ID_2B

B

 

C

3ID_2C

B

 

C

4ID_3AA
5ID_4FF
6ID_5E

E

 

R

 

T

7ID_5R

E

 

R

 

T

8ID_5T

E

 

R

 

T

9ID_6  
10ID_7QQ
11ID_8SS
12ID_8  
13ID_9ZZ

 

 

Can someone help me do that?

Thank you very much.

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

You can create a measure or calculated column as below to get it, please find the attachment for the details.

1. Create a calculated column

Category_New = 
VAR _newcategory =
    CONCATENATEX (
        FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) ),
        'Table'[Category],
        UNICHAR ( 10 ),
        'Table'[Category]
    )
RETURN
    IF ( 'Table'[Category] = BLANK (), BLANK (), _newcategory )

2. Create a measure

Measure =
VAR _curid =
    SELECTEDVALUE ( 'Table'[ID] )
VAR _curcategory =
    SELECTEDVALUE ( 'Table'[Category] )
VAR _newcategory =
    CONCATENATEX (
        FILTER ( ALLSELECTED ( 'Table' ), 'Table'[ID] = _curid ),
        'Table'[Category],
        UNICHAR ( 10 ),
        'Table'[Category]
    )
RETURN
    IF ( _curcategory = BLANK (), BLANK (), _newcategory )

yingyinr_0-1626844425304.png

Best Regards

Community Support Team _ Rena
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

5 REPLIES 5
v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

You can create a measure or calculated column as below to get it, please find the attachment for the details.

1. Create a calculated column

Category_New = 
VAR _newcategory =
    CONCATENATEX (
        FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) ),
        'Table'[Category],
        UNICHAR ( 10 ),
        'Table'[Category]
    )
RETURN
    IF ( 'Table'[Category] = BLANK (), BLANK (), _newcategory )

2. Create a measure

Measure =
VAR _curid =
    SELECTEDVALUE ( 'Table'[ID] )
VAR _curcategory =
    SELECTEDVALUE ( 'Table'[Category] )
VAR _newcategory =
    CONCATENATEX (
        FILTER ( ALLSELECTED ( 'Table' ), 'Table'[ID] = _curid ),
        'Table'[Category],
        UNICHAR ( 10 ),
        'Table'[Category]
    )
RETURN
    IF ( _curcategory = BLANK (), BLANK (), _newcategory )

yingyinr_0-1626844425304.png

Best Regards

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

@v-yiruan-msft 

 

Thanks a lot. Works very well.

 

Just one thing:

In these equations, the order changes alphabetically.

For example, if it is going to combine three rows of R A C, the result after concatenation is A C R.

Is there any modification to these equations so that it keeps the original order when concatenating? 

 

Thanks.

Hi @Anonymous ,

You can make a little change on the calculated column or measure to remove the fifth arugment orderBy_expression ",'Table'[Category]" in the function CONCATENATEX. Please find the attachment for the details.

yingyinr_1-1626852030986.png

Best Regards

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

@Anonymous , try like

 

concatenatex(Filter(allselected(Table), Table[ID] = max(Table[ID])), Table[Category], unichar(10))

Anonymous
Not applicable

Thanks @amitchandak 

 

Are you sure it is working?

 

I get this result!

 

mah65_0-1626683525594.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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