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
pie333
Regular Visitor

How to rearreange a table based on a set of columns?

Hi I am trying to rearrange a table so that the value in one column is assigned to a set of columns.

As an example take this table:

farmertotal land (ac)cultivated surfaceplot A cultivated plantplot A cultivated surface (ac)plot A workersplot A yield in kgplot A expenses plot A selling price per kgplot A yield expected valueplot B cultivated plantplot B cultivated surface (ac)plot B workersplot B yield in kgplot B expenses plot B selling price per kgplot B yield expected valueplot C cultivated plantplot C cultivated surface (ac)plot C workersplot C yield in kgplot C expenses plot C selling price per kgplot C yield expected value
George126Coffee221201 200 €15 €1 800 €Banana31300700 €3 €900 €Tea1260                                            600 €                                           25.0 €1 500 €
Mark85.5Orange1050                                            100 €1.50 €75 €Apple22750                                            400 €1.51 125 €Coffee2.52100                                            160 €                                           10.0 €1 000 €
Anna2322Banana2231230                                         1 800 €89 840 €0000                                                -   €00 €0000                                                -   €                                                -   €0 €
Yuri65.5Avocado31400                                         2 000 €62 400 €Strawberry2.5490                                            500 €10900 €0000                                                -   €                                                -   €0 €

 

Is it possible to transform it with Power Query to this table? How?

farmertotal land (ac)cultivated surfaceCoffee cultivated surface (ac)coffe workerscoffee yield in kgcoffee expenses coffee selling price per kgcoffee yield expected valueOrange cultivated surface (ac)Orange workers...
George126221201 200 €15 €1 800 €  ...
Mark85.52.52100                                            160 €                                           10.0 €1 000 €1 ...
Anna2322        ...
Yuri65.5        ...



I am sorry I had to crop the "new table" because the post exceeded the limit of 20 000 characters: it would have had each "plant" with its values (workers, yield, expenses and so on) as single columns, filled only on the specific farmers who had those values in the first table (George and Mark have Coffee, Mark would have had also oranges etc... )

 

Thank you!

1 ACCEPTED SOLUTION
Jakinta
Solution Sage
Solution Sage

You can try this in blank query.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fVFNC8IwDP0rY2eVLq5uHqcHT+JBLzI8VO1ElE2KH/jvbV6KVhAZSZuXLH0vqet0Zjt3sGkvzci7kbdp1zSWEQqWkWKfkMKpEZQIJqb1n78MGeQTcAHP2Bi3lTUhj0cUHHvSA+mtfbjp1encuJMHSm96wC8tnGmFnzeu1fhBqKCiYFddLueYc4GyPCrLkoz0lzzA9Ok2ks6BkQqMqhYCidUQxZoRQTgNVTQUJj9OylwFxrH1f2BxTvL87vrmjmEjMonq3u3MvouGLfIIVKWSEsGWV2ceW+vc8y00xzYwQNH53s4fHpsX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [farmer = _t, #"total land (ac)" = _t, #"cultivated surface" = _t, #"plot A cultivated plant" = _t, #"plot A cultivated surface (ac)" = _t, #"plot A workers" = _t, #"plot A yield in kg" = _t, #"plot A expenses " = _t, #"plot A selling price per kg" = _t, #"plot A yield expected value" = _t, #"plot B cultivated plant" = _t, #"plot B cultivated surface (ac)" = _t, #"plot B workers" = _t, #"plot B yield in kg" = _t, #"plot B expenses " = _t, #"plot B selling price per kg" = _t, #"plot B yield expected value" = _t, #"plot C cultivated plant" = _t, #"plot C cultivated surface (ac)" = _t, #"plot C workers" = _t, #"plot C yield in kg" = _t, #"plot C expenses " = _t, #"plot C selling price per kg" = _t, #"plot C yield expected value" = _t]),
    Cols = Table.ColumnNames(Source),
    PlotA = Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, List.Combine({List.FirstN(Cols,3),List.Split(List.Skip(Cols,3),7){0}}))),1),
    PlotB = Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, List.Combine({List.FirstN(Cols,3),List.Split(List.Skip(Cols,3),7){1}}))),1),
    PlotC = Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, List.Combine({List.FirstN(Cols,3),List.Split(List.Skip(Cols,3),7){2}}))),1),
    Appended = Table.Combine({PlotA, PlotB, PlotC}),
    Names = Table.ReplaceValue(Table.FromColumns({List.FirstN(Cols,3)&List.FirstN(List.Skip(Cols,4),6)}),"plot A","##",Replacer.ReplaceText,{"Column1"}),
    Grouped = Table.Group(Appended, {"Column4"}, {{"Gr", each Table.SelectColumns(_,List.RemoveItems(Table.ColumnNames(_), {"Column4"})), type table }}),
    Filtered = Table.SelectRows(Grouped, each ([Column4] <> "0")),
    Renamed = Table.AddColumn(Filtered, "Custom", each Table.RenameColumns( [Gr], 
List.Zip( {  Table.ColumnNames([Gr]) ,
 Table.ReplaceValue(Names,"##",[Column4],Replacer.ReplaceText,{"Column1"})[Column1]} ))),
    Removed = Table.RemoveColumns(Renamed,{"Gr", "Column4"}),
    Expanded = Table.ExpandTableColumn(Removed, "Custom",  List.Union(List.Transform(Removed[Custom], each Table.ColumnNames(_)))  ),
    Grouped2 = Table.Group(Expanded, {"farmer", "total land (ac)", "cultivated surface"}, {{"All", each Table.FirstN(Table.FillUp(Table.SelectColumns(_,List.Skip(Table.ColumnNames(_),3)),List.Skip(Table.ColumnNames(_),3)), 1), type table }}),
    Expanded2 = Table.ExpandTableColumn(Grouped2, "All", List.Union(List.Transform(Grouped2[All], each Table.ColumnNames(_)))  ),
    FINAL = Table.ReplaceValue(Expanded2,null,"",Replacer.ReplaceValue,Table.ColumnNames(Expanded2))
in
    FINAL

 

View solution in original post

1 REPLY 1
Jakinta
Solution Sage
Solution Sage

You can try this in blank query.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fVFNC8IwDP0rY2eVLq5uHqcHT+JBLzI8VO1ElE2KH/jvbV6KVhAZSZuXLH0vqet0Zjt3sGkvzci7kbdp1zSWEQqWkWKfkMKpEZQIJqb1n78MGeQTcAHP2Bi3lTUhj0cUHHvSA+mtfbjp1encuJMHSm96wC8tnGmFnzeu1fhBqKCiYFddLueYc4GyPCrLkoz0lzzA9Ok2ks6BkQqMqhYCidUQxZoRQTgNVTQUJj9OylwFxrH1f2BxTvL87vrmjmEjMonq3u3MvouGLfIIVKWSEsGWV2ceW+vc8y00xzYwQNH53s4fHpsX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [farmer = _t, #"total land (ac)" = _t, #"cultivated surface" = _t, #"plot A cultivated plant" = _t, #"plot A cultivated surface (ac)" = _t, #"plot A workers" = _t, #"plot A yield in kg" = _t, #"plot A expenses " = _t, #"plot A selling price per kg" = _t, #"plot A yield expected value" = _t, #"plot B cultivated plant" = _t, #"plot B cultivated surface (ac)" = _t, #"plot B workers" = _t, #"plot B yield in kg" = _t, #"plot B expenses " = _t, #"plot B selling price per kg" = _t, #"plot B yield expected value" = _t, #"plot C cultivated plant" = _t, #"plot C cultivated surface (ac)" = _t, #"plot C workers" = _t, #"plot C yield in kg" = _t, #"plot C expenses " = _t, #"plot C selling price per kg" = _t, #"plot C yield expected value" = _t]),
    Cols = Table.ColumnNames(Source),
    PlotA = Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, List.Combine({List.FirstN(Cols,3),List.Split(List.Skip(Cols,3),7){0}}))),1),
    PlotB = Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, List.Combine({List.FirstN(Cols,3),List.Split(List.Skip(Cols,3),7){1}}))),1),
    PlotC = Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, List.Combine({List.FirstN(Cols,3),List.Split(List.Skip(Cols,3),7){2}}))),1),
    Appended = Table.Combine({PlotA, PlotB, PlotC}),
    Names = Table.ReplaceValue(Table.FromColumns({List.FirstN(Cols,3)&List.FirstN(List.Skip(Cols,4),6)}),"plot A","##",Replacer.ReplaceText,{"Column1"}),
    Grouped = Table.Group(Appended, {"Column4"}, {{"Gr", each Table.SelectColumns(_,List.RemoveItems(Table.ColumnNames(_), {"Column4"})), type table }}),
    Filtered = Table.SelectRows(Grouped, each ([Column4] <> "0")),
    Renamed = Table.AddColumn(Filtered, "Custom", each Table.RenameColumns( [Gr], 
List.Zip( {  Table.ColumnNames([Gr]) ,
 Table.ReplaceValue(Names,"##",[Column4],Replacer.ReplaceText,{"Column1"})[Column1]} ))),
    Removed = Table.RemoveColumns(Renamed,{"Gr", "Column4"}),
    Expanded = Table.ExpandTableColumn(Removed, "Custom",  List.Union(List.Transform(Removed[Custom], each Table.ColumnNames(_)))  ),
    Grouped2 = Table.Group(Expanded, {"farmer", "total land (ac)", "cultivated surface"}, {{"All", each Table.FirstN(Table.FillUp(Table.SelectColumns(_,List.Skip(Table.ColumnNames(_),3)),List.Skip(Table.ColumnNames(_),3)), 1), type table }}),
    Expanded2 = Table.ExpandTableColumn(Grouped2, "All", List.Union(List.Transform(Grouped2[All], each Table.ColumnNames(_)))  ),
    FINAL = Table.ReplaceValue(Expanded2,null,"",Replacer.ReplaceValue,Table.ColumnNames(Expanded2))
in
    FINAL

 

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