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

Convert first few rows into new columns to flatten the data

 Hi Community, I get reports for each departments month wise (2 reports per dept per month) in the following structure:

 

dc_1820_0-1594818868569.png

 

As Power BI needs data in flat-file format, I would have to convert the data into:

 

dc_1820_1-1594818926624.png

 

> I need to take the folder where the reports are present (new reports in excel get added regularly)

> Combine & Transform all of the reports to be visualized into a Power BI report/dashboard

 

Transformation:

> I think the stpes can be applied on the 'Transform Sample File' and the same will be applied for every sheet and new sheet added into the folder.

> Need to take the first 5 rows and make them as columns next to the main data.

> I have tried using transpose, pivot, unpivot in the Power Query editor of Power BI, followed couple of videos and community solutions, but was not able to get the required solution. Can you please help me out with the detailed steps to achieve the same.

 

Data 

Thanks much in advance👍🏼

1 ACCEPTED SOLUTION
ziying35
Impactful Individual
Impactful Individual

Hi, @dc_1820 

let
    Source = Table.FromRecords(Json.Document(Binary.Decompress(Binary.FromText("tdJNa8MwDAbgv2J8bsF2mn7dsuYSRruQHccOplFHWGIHRymUsf++tN2ovMNGm/RmyfC8Qujlg69s2VZG8iWPodYOKzDIR99t1bVTZ/N2iyyGPZS29v4DvjRtWf6UE78Mz+Xn6FcKsiT2IqJYKvGU9mbX2ryB8+mHVS9WIzC7YxnU1vl7EXIsZmMllOgTkIIrbP5HRHiKYGiZDM9Vjzj6r/zyhtkfs8i/FFsYbOh4/Bk1tpdWp3av6nhDl2Zn8wwq7d4b7gckCJWkEQG1E7OH5nSvbAOQQ86vGf5oK2pPh7UDaqth7Qm1F96+NRbNTm/RusPVbEhZKQZzp3dyZ9QNh13xnNrzwUZe3PEqpKC4t+ZN9J/2+gU=",BinaryEncoding.Base64),Compression.Deflate))),
    firstn = Table.FirstN(Source[[Column1],[Column2]],5),
    pb_rec = Record.FromList(firstn[Column1],firstn[Column2]),
    main_tbl = Table.PromoteHeaders(Table.RemoveFirstN(Source,6)),
    recs = Table.TransformRows(main_tbl,each pb_rec&_),
    result = Table.FromRecords(recs)
in
    result

 

If my code solves your problem, mark it as a solution

View solution in original post

8 REPLIES 8
ziying35
Impactful Individual
Impactful Individual

Hi, @dc_1820 

let
    Source = Table.FromRecords(Json.Document(Binary.Decompress(Binary.FromText("tdJNa8MwDAbgv2J8bsF2mn7dsuYSRruQHccOplFHWGIHRymUsf++tN2ovMNGm/RmyfC8Qujlg69s2VZG8iWPodYOKzDIR99t1bVTZ/N2iyyGPZS29v4DvjRtWf6UE78Mz+Xn6FcKsiT2IqJYKvGU9mbX2ryB8+mHVS9WIzC7YxnU1vl7EXIsZmMllOgTkIIrbP5HRHiKYGiZDM9Vjzj6r/zyhtkfs8i/FFsYbOh4/Bk1tpdWp3av6nhDl2Zn8wwq7d4b7gckCJWkEQG1E7OH5nSvbAOQQ86vGf5oK2pPh7UDaqth7Qm1F96+NRbNTm/RusPVbEhZKQZzp3dyZ9QNh13xnNrzwUZe3PEqpKC4t+ZN9J/2+gU=",BinaryEncoding.Base64),Compression.Deflate))),
    firstn = Table.FirstN(Source[[Column1],[Column2]],5),
    pb_rec = Record.FromList(firstn[Column1],firstn[Column2]),
    main_tbl = Table.PromoteHeaders(Table.RemoveFirstN(Source,6)),
    recs = Table.TransformRows(main_tbl,each pb_rec&_),
    result = Table.FromRecords(recs)
in
    result

 

If my code solves your problem, mark it as a solution

camargos88
Community Champion
Community Champion

Hi @dc_1820 ,

 

Can you post this data here ? So we can copy it.



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

Proud to be a Super User!



Here is the link to the data - Data 

Hi @dc_1820 ,

 

You can replicate this m code, if you are using folder connection use it on Transformation Sample File:

let
    Source = Excel.Workbook(File.Contents("D:\Downloads\Data from Dept.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Department", type text}, {"Product Development", type any}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}}),
    Result = Table.Combine({Table.PromoteHeaders(Table.Skip(#"Changed Type", 5)), Table.PromoteHeaders(Table.FirstN(Table.Transpose(Table.FirstN(#"Changed Type", 4)),2))}),
    #"Filled Up" = Table.FillUp(Result,{"Dept ID","Dept Manager", "Date of Report", "Period of Report"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Up", each ([KRA] <> null)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows",{{"KRA", type text}, {"Points", Int64.Type}, {"Status", type text}, {"Comments", type text}, {"Remarks", type text}, {"Dept ID", type text}, {"Dept Manager", type text}, {"Date of Report", type date}, {"Period of Report", type text}})
in
    #"Changed Type1"

 

Capture.PNG

 

 



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

Proud to be a Super User!



@camargos88 Thank you. But the end result is missing the Department Column Can you please explain me what is happening at the Result step. I am able to pretty ,uch understand the reamaining. I want to try this using GUI once so that I easily replicate it. Thank You.

Hi @dc_1820 ,

 

Try this one, I missed that column.

let
    Source = Excel.Workbook(File.Contents("D:\Downloads\Data from Dept.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Department", type text}, {"Product Development", type any}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}}),
    Result = Table.Combine({Table.PromoteHeaders(Table.Skip(#"Changed Type", 5)), Table.PromoteHeaders(Table.FirstN(Table.Transpose(Table.DemoteHeaders(Table.FirstN(#"Changed Type", 4))),2))}),
    #"Filled Up" = Table.FillUp(Result,{"Dept ID","Dept Manager", "Date of Report", "Period of Report", "Department"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Up", each ([KRA] <> null)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows",{{"KRA", type text}, {"Points", Int64.Type}, {"Status", type text}, {"Comments", type text}, {"Remarks", type text}, {"Department", type text}, {"Dept ID", type text}, {"Dept Manager", type text}, {"Date of Report", type date}, {"Period of Report", type text}})
in
    #"Changed Type1"


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

Proud to be a Super User!



@camargos88 @ziying35 @Anonymous Thank you all for the responses. I have accepted @ziying35 response as solution as it was easy to understand to my knowledge.

Anonymous
Not applicable

you could get your final table only using GUI:

1) Select TOP part and transpose and add zero-based index:

image.png

1) Select BOTTOM part and add zero-based index:

image.png

 

3) then merge on index TOP and BOTTOM and fill-down:

 

image.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.

Top Solution Authors