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
YannLG
Frequent Visitor

how to combine hundreds of text rows

Hi, I have been looking for a solution all around, finding multiple ones but none that suits my needs.

I have an example of code that will combine rows of text (like the justify function in excel) by using the group function in power query.

The "key" is the first column "date", then followed by columns of text of which I combine rows respectively.

The end result gives a table with less rows but the same amount of columns with the text fo\rom each column text separated by ";".

input table:

datecolumn 3column 4column 5colum 6
2021/7AEAE
2021/7BFBF
2022/8CGCG
2022/8DHDH

 

Output table

datecolumn 3column 4column 5colum 6
2021/7A,BE,FA,BE,F
2022/8C,DG,HC,DG,H

 

"= Table.Group(#"Replaced Value", {"Date"}, {{"Column3",each Text.Combine([Column3],";")},{"Column4",each Text.Combine([Column4],"/n")},{"Column5",each Text.Combine([Column5],"/n")},{"Column6",each Text.Combine([Column6],"/n")}})."

Works great.

 

Now assume Thta I have hundreds of columns in the table (and not only 6 like in the example), how do I change the formula not to have to manually enter each column name?

something like

 " = Table.Group(#"Replaced Value", {"Date"}, Text.Combine(each column in the table),";")}

 

Thanks for your help!

1 ACCEPTED SOLUTION
edhans
Super User
Super User

See this code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ3V9JRcgRiVzgdq4Mk5QTEbnAaKmWkbwHkOgOxO5xGkXIBYg84HRsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, #"column 3" = _t, #"column 4" = _t, #"column 5" = _t, #"colum 6" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"date"}, "Attribute", "Value"),
    #"Grouped Rows" = 
        Table.Group(
            #"Unpivoted Other Columns", 
            {"date", "Attribute"}, 
            {
                {
                    "Combined Text",
                    each Text.Combine(_[Value], ", ")
                }
            }
        ),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Attribute]), "Attribute", "Combined Text")
in
    #"Pivoted Column"

It returns this:

edhans_0-1634744925831.png


What is did was:

  • Select the Date and unpivoted other columns. This will let the logic work on an undefined number of columns for you.
  • I then did a group by, and used the Text.Combine() function on the Values
  • Then I Pivoted back the attribute column and told it not to aggregate the Values column.

edhans_1-1634745051676.png

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

2 REPLIES 2
edhans
Super User
Super User

See this code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtQ3V9JRcgRiVzgdq4Mk5QTEbnAaKmWkbwHkOgOxO5xGkXIBYg84HRsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, #"column 3" = _t, #"column 4" = _t, #"column 5" = _t, #"colum 6" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"date"}, "Attribute", "Value"),
    #"Grouped Rows" = 
        Table.Group(
            #"Unpivoted Other Columns", 
            {"date", "Attribute"}, 
            {
                {
                    "Combined Text",
                    each Text.Combine(_[Value], ", ")
                }
            }
        ),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Attribute]), "Attribute", "Combined Text")
in
    #"Pivoted Column"

It returns this:

edhans_0-1634744925831.png


What is did was:

  • Select the Date and unpivoted other columns. This will let the logic work on an undefined number of columns for you.
  • I then did a group by, and used the Text.Combine() function on the Values
  • Then I Pivoted back the attribute column and told it not to aggregate the Values column.

edhans_1-1634745051676.png

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
YannLG
Frequent Visitor

Thanks for you quick help, It worked nicely!

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.

Top Solution Authors
Top Kudoed Authors