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

Concat rows in PowerQuery

Hi Good people, 

 

I'm new to the Power BI world and I'm working and learning at the same time. I want to concatenate the first two rows while keeping the other columns intact. This is easy to do in Excel, but I want to achieve this in PowerQuery so that I can use it on a larger scale with a refresh. I have attached the table here.

What I want to do is concatenate the first two rows of columns D, E, and F. I would expect the values would be like this: Q1 AA, Q2 FG, Q3 HL in first row.

Can anyone please guide me on achieving this in PowerQuery in Power BI?

 

Thanks in advance.

 

My current data format:

 

Start DateEnd DateIDQ1Q2Q3Current Page
Start DateEnd DateIDAAFGHLCurrent Page
1/05/20241/05/20241232510Brand


My expected format is this:

 

Start DateEnd DateIDQ1 AAQ2 FGQ3 HLCurrent Page
1/05/20241/05/20241232510Brand

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Added Custom" = Record.ToTable(Table.AddColumn(Source, "Custom", each _){0}),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Name] <> "Custom")),
    #"Added Custom1" = Table.AddColumn(#"Filtered Rows", "Custom", each if Text.StartsWith([Name],"Q",Comparer.OrdinalIgnoreCase) then [Name]&" "&[Value] else [Name]),
    Custom1 = Table.FromRows(Table.ToRows(Table.Skip(Source,1)),#"Added Custom1"[Custom])
in
    Custom1

Hope this helps.

Ashish_Mathur_0-1729049820475.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

7 REPLIES 7
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Added Custom" = Record.ToTable(Table.AddColumn(Source, "Custom", each _){0}),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Name] <> "Custom")),
    #"Added Custom1" = Table.AddColumn(#"Filtered Rows", "Custom", each if Text.StartsWith([Name],"Q",Comparer.OrdinalIgnoreCase) then [Name]&" "&[Value] else [Name]),
    Custom1 = Table.FromRows(Table.ToRows(Table.Skip(Source,1)),#"Added Custom1"[Custom])
in
    Custom1

Hope this helps.

Ashish_Mathur_0-1729049820475.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you so much, it works. 

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-zhengdxu-msft
Community Support
Community Support

Hi @TanzilHasan 

 

Please try this:

Here's the sample data:

vzhengdxumsft_0-1729043947114.png

First of all, Use the headers as first row:

vzhengdxumsft_1-1729044047513.pngvzhengdxumsft_2-1729044058806.png

Then select all column and click the Transpose in the Transform pane:

vzhengdxumsft_3-1729044274885.pngvzhengdxumsft_4-1729044285040.png

 

Next add a custom column:

vzhengdxumsft_6-1729044364024.png

 

 

if [Column1]=[Column2] then [Column1] else Text.Combine({[Column1], [Column2]}, " ")

 

vzhengdxumsft_5-1729044353217.png

Remove the Column1 and Column2, select the Custom column and click Pivot Column:

vzhengdxumsft_8-1729044487951.png

vzhengdxumsft_9-1729044521819.png

The result is as follow:

vzhengdxumsft_10-1729044538471.png

 

 

Best Regards

Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks for providing step by step instruction, this method also works. 

ryan_mayu
Super User
Super User

could you pls provide more sample data? How many rows that you need to merge? The results of other rows are the same?





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




lbendlin
Super User
Super User

Having columns like this is more of an Excel thing, and not something you want to do in Power BI. You will want to unpivot your source data, and likely split it into two sources, one for the quarter attributes (AA, FG, HL) and the other for the numeric data.

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! Prices go up Feb. 11th.

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.

Jan NL Carousel

Fabric Community Update - January 2025

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