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
petermb72
Helper III
Helper III

Formatting Data in Power Query Editor when it is wrapped in formatting

I have an excel report that gets generated that has 4 departments that need to get combined and it wraps the data as well.  How do I unwrap the data.  Here is what I have and what I am looking for as an end result:

 

  1/7/20231/8/20231/9/20231/10/20231/11/20231/12/20231/13/2023 
Unit 1Volume100110100100101100100 
 Hours1011109101010 
  1/7/20231/8/20231/9/20231/10/20231/11/20231/12/20231/13/2023 
Unit 2Volume75757576767676 
 Hours88812121210 
  1/14/20231/15/20231/16/20231/17/20231/18/20231/19/20231/20/2023 
Unit 1Volume100100100100100100100 
 Hours10101010101010 
  1/14/20231/15/20231/16/20231/17/20231/18/20231/19/20231/20/2023 
Unit 2Volume75757576767676 
 Hours10101010101010 
  1/21/2023nullnullnullnullnullnull 
Unit 1Volume100nullnullnullnullnullnull 
 Hours10nullnullnullnullnullnull 
Unit 2Volume75nullnullnullnullnullnull 
 Hours10nullnullnullnullnullnull 
          
          
          
          
          
          

End Result I would like Would Be:

DateVolume(Unit1 and Unit 2 added together)Hours(Unit 1 and Unit 2 added together)
1/7/202317518
1/8/202318519
1/9/202317518
1/10/202317621
1/11/202317722
1/12/202317622
1/13/202317620
1/14/202317520
1/15/202317520
1/16/202317520
1/17/202317620
1/18/202317620
1/19/202317620
1/20/202317620
1/21/202317520

There is a few other columns in my data but I know how to handle those.  My main issue is getting the data unwrapped from the columns so I can sort the data out into a usefull state.  I will get a new one of these files everyday that would include the next day (so the 22nd in my example) and it would drop the first day(the 7th in my example).  Basicly the report would be a rolling 30 days. 

 

Any help on unwrapping this mess so it is usalbe would be awesome!

 

Thanks, Peter

2 ACCEPTED SOLUTIONS
v-yueyunzh-msft
Community Support
Community Support

Hi , @petermb72 

According to your description, you want to use Power Query to format your data .

For your need , We need to make sure your file is in the same format every time. And the format like this in my understanding:

vyueyunzhmsft_0-1689303991982.png

If this , we can connect the excel and we can create a blank query and put this M code in Power Query Editor:

let
    Source = Excel.Workbook(File.Contents("C:\Users\v-yueyzhang\Downloads\case\20230714.xlsx"), null, true),
    Tabelle1_Sheet = Source{[Item="Tabelle1",Kind="Sheet"]}[Data],
    #"Added Index" = Table.AddIndexColumn(Tabelle1_Sheet, "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", (x)=>if x[Column1]=null then try if Table.SelectRows(#"Added Index",(y)=> y[Index]=x[Index]-1 ){0}[Column1]=null then "Date" else Table.SelectRows(#"Added Index",(y)=> y[Index]=x[Index]-1 ){0}[Column1] otherwise "Date"  else x[Column1]            ),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Custom", "Column1", "Column2", "Index", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Column1"}),
    #"Added Conditional Column" = Table.AddColumn(#"Removed Columns", "Custom.1", each if [Index] >= 13 then 5 else if [Index] >= 10 then 4 else if [Index] >= 7 then 3 else if [Index] >= 4 then 2 else 1,  Int64.Type),
    #"Reordered Columns1" = Table.ReorderColumns(#"Added Conditional Column",{"Custom", "Column2", "Custom.1", "Index", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns1",{{"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type text}}),
    Custom2 = Table.Group(#"Changed Type",{"Custom","Custom.1"},{"test",(x)=>Table.RemoveColumns(x,{ "Custom", "Custom.1", "Index"})}),
    #"Added Index1" = Table.AddIndexColumn(Custom2, "Index", 1, 1, Int64.Type),
    #"Added Custom1" = Table.AddColumn(#"Added Index1", "Custom.2", (x)=>
if x[Custom]="Date" then null else Table.PromoteHeaders(Table.Sort(Table.SelectRows(#"Added Index1",(y)=>y[Index]<x[Index] and y[Custom]="Date" ),{"Index",1}){0}[test], [PromoteAllScalars=true])
  ),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.3", each List.Zip( { Table.ColumnNames([test]), Table.ColumnNames([Custom.2]) } )        ),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom.4", each 
Table.SelectRows(Table.UnpivotOtherColumns(Table.RenameColumns([test],[Custom.3]), {"Column1"}, "Attribute", "Value")
, each ([Value] <> "null"))
),
    #"Removed Errors" = Table.RemoveRowsWithErrors(#"Added Custom3", {"Custom.4"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Removed Errors",{"Custom.1", "test", "Index", "Custom.2", "Custom.3"}),
    #"Expanded Custom.4" = Table.ExpandTableColumn(#"Removed Columns1", "Custom.4", {"Column1", "Attribute", "Value"}, {"Column1", "Attribute", "Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom.4",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Attribute", "Date"}, {"Custom", "Unit"}, {"Column1", "Type"}}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Renamed Columns",{{"Value", Int64.Type}})
in
    #"Changed Type2"

You can update the file location in your side .

Then we can get this table:

vyueyunzhmsft_1-1689304081573.png

And we can put the fields on the visual and we can get this:

 vyueyunzhmsft_2-1689304099227.png

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

Thank You so much for your help!

View solution in original post

3 REPLIES 3
v-yueyunzh-msft
Community Support
Community Support

Hi , @petermb72 

According to your description, you want to use Power Query to format your data .

For your need , We need to make sure your file is in the same format every time. And the format like this in my understanding:

vyueyunzhmsft_0-1689303991982.png

If this , we can connect the excel and we can create a blank query and put this M code in Power Query Editor:

let
    Source = Excel.Workbook(File.Contents("C:\Users\v-yueyzhang\Downloads\case\20230714.xlsx"), null, true),
    Tabelle1_Sheet = Source{[Item="Tabelle1",Kind="Sheet"]}[Data],
    #"Added Index" = Table.AddIndexColumn(Tabelle1_Sheet, "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", (x)=>if x[Column1]=null then try if Table.SelectRows(#"Added Index",(y)=> y[Index]=x[Index]-1 ){0}[Column1]=null then "Date" else Table.SelectRows(#"Added Index",(y)=> y[Index]=x[Index]-1 ){0}[Column1] otherwise "Date"  else x[Column1]            ),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Custom", "Column1", "Column2", "Index", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Column1"}),
    #"Added Conditional Column" = Table.AddColumn(#"Removed Columns", "Custom.1", each if [Index] >= 13 then 5 else if [Index] >= 10 then 4 else if [Index] >= 7 then 3 else if [Index] >= 4 then 2 else 1,  Int64.Type),
    #"Reordered Columns1" = Table.ReorderColumns(#"Added Conditional Column",{"Custom", "Column2", "Custom.1", "Index", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns1",{{"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type text}}),
    Custom2 = Table.Group(#"Changed Type",{"Custom","Custom.1"},{"test",(x)=>Table.RemoveColumns(x,{ "Custom", "Custom.1", "Index"})}),
    #"Added Index1" = Table.AddIndexColumn(Custom2, "Index", 1, 1, Int64.Type),
    #"Added Custom1" = Table.AddColumn(#"Added Index1", "Custom.2", (x)=>
if x[Custom]="Date" then null else Table.PromoteHeaders(Table.Sort(Table.SelectRows(#"Added Index1",(y)=>y[Index]<x[Index] and y[Custom]="Date" ),{"Index",1}){0}[test], [PromoteAllScalars=true])
  ),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.3", each List.Zip( { Table.ColumnNames([test]), Table.ColumnNames([Custom.2]) } )        ),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom.4", each 
Table.SelectRows(Table.UnpivotOtherColumns(Table.RenameColumns([test],[Custom.3]), {"Column1"}, "Attribute", "Value")
, each ([Value] <> "null"))
),
    #"Removed Errors" = Table.RemoveRowsWithErrors(#"Added Custom3", {"Custom.4"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Removed Errors",{"Custom.1", "test", "Index", "Custom.2", "Custom.3"}),
    #"Expanded Custom.4" = Table.ExpandTableColumn(#"Removed Columns1", "Custom.4", {"Column1", "Attribute", "Value"}, {"Column1", "Attribute", "Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom.4",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Attribute", "Date"}, {"Custom", "Unit"}, {"Column1", "Type"}}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Renamed Columns",{{"Value", Int64.Type}})
in
    #"Changed Type2"

You can update the file location in your side .

Then we can get this table:

vyueyunzhmsft_1-1689304081573.png

And we can put the fields on the visual and we can get this:

 vyueyunzhmsft_2-1689304099227.png

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

 

 

 

Thank You so much for your help!

Thank you so much!  

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.