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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
MaryF
Advocate II
Advocate II

Help needed to transform data via M

Hi all

 

I'm hoping someone can help me get past a big hurdle.. I have the following issue

 

My data is coming in as a single column

Raw DataRaw Data

 

 

.I need it to become this...I need it to become this...

Can anyone let me know if this is doable etc.

 

Many thanks in advance

 

Mary

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

I don't know if this is the optimal solution, but I did it this way:

 

  1. Added a custom column that repeats a sequence of the numbers 1 - 8 down the series so there is something to pivot on
  2. Repeated the account name or whatever you call that first row in each set in your sample
  3. Removed those first rows
  4. Removed the index column because it will be meaningless after this
  5. Pivoted on the column header number thing from step 2
  6. The next steps would probably be to rename all these columns and maybe add a new index column, but I didn't bother.

 

let
    Source = Excel.Workbook(File.Contents("M:\Reports\test.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Index", Int64.Type}}),
    #"Added Sorter" = Table.AddColumn(
		#"Changed Type",
		"Sorter",
		each Number.From(
			List.Min(
				List.Range(
					List.Repeat(
						{1, 2, 3, 4, 5, 6, 7, 8},
						Int32.From(
							(List.Count(#"Changed Type"[Index]) / 8),
							null,
							RoundingMode.Up
						)		
					),
					[Index] - 1,
					1
				)
			)
		)
	),
    #"Added Name" = Table.AddColumn(
		#"Added Sorter",
		"Name",
		each Text.From(
			List.Last(
				List.FirstN(
					#"Added Sorter"[Column1],
					[Index] - ([Sorter] - 1)
				),
				1
			)
		)
	),
    #"Reordered Columns" = Table.ReorderColumns(
		#"Added Name",
		{"Index", "Sorter", "Name", "Column1"}
	),
    #"Removed Rows" = Table.SelectRows(
		#"Reordered Columns",
		each [Sorter] > 1
	),
    #"Removed Columns" = Table.RemoveColumns(#"Removed Rows",{"Index"}),
    #"Pivoted Column" = Table.Pivot(
		Table.TransformColumnTypes(
			#"Removed Columns",
			{{"Sorter", type text}},
			"en-US"
		),
		List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Sorter", type text}}, "en-US")[Sorter]),
		"Sorter",
		"Column1"
	)
in
    #"Pivoted Column"

View solution in original post

ImkeF
Community Champion
Community Champion

This is very similar, but a bit shorter:

 

let
    Source = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1),
    RowNumber = Table.AddColumn(#"Added Index", "Custom", each Number.RoundUp([Index]/8)),
    PivotCols = Table.AddColumn(RowNumber, "Pivot", each Number.Mod([Index], 8)),
    RemoveIndex = Table.RemoveColumns(PivotCols,{"Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(RemoveIndex, {{"Pivot", type text}}, "de-DE"), List.Distinct(Table.TransformColumnTypes(RemoveIndex, {{"Pivot", type text}}, "de-DE")[Pivot]), "Pivot", "Spalte1")
in
    #"Pivoted Column"

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

I don't know if this is the optimal solution, but I did it this way:

 

  1. Added a custom column that repeats a sequence of the numbers 1 - 8 down the series so there is something to pivot on
  2. Repeated the account name or whatever you call that first row in each set in your sample
  3. Removed those first rows
  4. Removed the index column because it will be meaningless after this
  5. Pivoted on the column header number thing from step 2
  6. The next steps would probably be to rename all these columns and maybe add a new index column, but I didn't bother.

 

let
    Source = Excel.Workbook(File.Contents("M:\Reports\test.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Index", Int64.Type}}),
    #"Added Sorter" = Table.AddColumn(
		#"Changed Type",
		"Sorter",
		each Number.From(
			List.Min(
				List.Range(
					List.Repeat(
						{1, 2, 3, 4, 5, 6, 7, 8},
						Int32.From(
							(List.Count(#"Changed Type"[Index]) / 8),
							null,
							RoundingMode.Up
						)		
					),
					[Index] - 1,
					1
				)
			)
		)
	),
    #"Added Name" = Table.AddColumn(
		#"Added Sorter",
		"Name",
		each Text.From(
			List.Last(
				List.FirstN(
					#"Added Sorter"[Column1],
					[Index] - ([Sorter] - 1)
				),
				1
			)
		)
	),
    #"Reordered Columns" = Table.ReorderColumns(
		#"Added Name",
		{"Index", "Sorter", "Name", "Column1"}
	),
    #"Removed Rows" = Table.SelectRows(
		#"Reordered Columns",
		each [Sorter] > 1
	),
    #"Removed Columns" = Table.RemoveColumns(#"Removed Rows",{"Index"}),
    #"Pivoted Column" = Table.Pivot(
		Table.TransformColumnTypes(
			#"Removed Columns",
			{{"Sorter", type text}},
			"en-US"
		),
		List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Sorter", type text}}, "en-US")[Sorter]),
		"Sorter",
		"Column1"
	)
in
    #"Pivoted Column"

@Anonymous Thanks LOADS!!! Very much appriate your input. Mary 🙂

Anonymous
Not applicable

Thanks. It was a fun problem to tackle. I'm sure there's a more efficient way to do the Sorter column, but that was the best I could think of. I'd be interested to see if someone else has a different solution to this.

ImkeF
Community Champion
Community Champion

This is very similar, but a bit shorter:

 

let
    Source = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1),
    RowNumber = Table.AddColumn(#"Added Index", "Custom", each Number.RoundUp([Index]/8)),
    PivotCols = Table.AddColumn(RowNumber, "Pivot", each Number.Mod([Index], 8)),
    RemoveIndex = Table.RemoveColumns(PivotCols,{"Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(RemoveIndex, {{"Pivot", type text}}, "de-DE"), List.Distinct(Table.TransformColumnTypes(RemoveIndex, {{"Pivot", type text}}, "de-DE")[Pivot]), "Pivot", "Spalte1")
in
    #"Pivoted Column"

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

@ImkeF I knew there had to be an easier way to get the 1-8 count. Math, of course! Nicely done.

 

@MaryF you're welcome. I learned a lot here too.

Thanks loads @ImkeF. I am so pleased to have a solution plus I learnt alot too. 

Sorry I should have said thank you @Anonymous & @ImkeF as I have two solutions to learn new stuff from.

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 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.