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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Syndicate_Admin
Administrator
Administrator

Split column into different columns by different elements

Good morning

I have a table with the following format:

IDNUMBERTYPE ACTIVITYOneFebMarAprMayJunJulAgoSepOctNovDec

The table has the hours of each month in each of the activities, each id has hours in the different activities and the accumulated hours that go.

In type of activity there are 4 different types with completely different names (they have spaces between the words, I can not divide by delimiters), I would like to obtain a table with a column for each type of activity and as data the sum of the hours of the last three months.

IDNUMBERActivity 1Activity 2Activity 3Activity 4

I guess it's possible to do it, someone to guide me with the solution?

Thanks a lot

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Syndicate_Admin ,

 

I suggest you to try code as below in advanced editor.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jVNBbsMgEPwLZx8Ca3B6dLFSqVV76DXy/79RBgNmlkSp5LVZe2Zndwz3u7GXi5nMLcWawqZIlzselh5mnwBH9s3wAepyHHisP1K8E0KgekU+q4xZa1PAmznfUMIvOmVepO6g4d5wu6qMWdspJeeyN2VUcSjngRE/prY2cBBR9ec0Q11ZDB1KUPi1tA2frNOJlTqaiCLGx0J9k0yABUsDLpr7n+pdbwccL37L1HN2H5+DjCmpVNraFx97Y3CsVriA8eyYEg3/6LO0JqFtjGVMqVMmo0FfIqi1NF2ZFS2O+6seI9/2jyZtDy2nmaD4VWZqR+yFLczM260dsxfWMDWaqTtqT41h0vbUibbc9z8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, NUMBER = _t, #"TYPE ACTIVITY" = _t, Jul = _t, Ago = _t, Sep = _t, Oct = _t, Nov = _t, Dec = _t, One = _t, Feb = _t, Mar = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"NUMBER", type text}, {"TYPE ACTIVITY", type text}, {"Jul", Int64.Type}, {"Ago", Int64.Type}, {"Sep", Int64.Type}, {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}, {"One", Int64.Type}, {"Feb", Int64.Type}, {"Mar", Int64.Type}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",null,0,Replacer.ReplaceValue,{"Jul", "Ago", "Sep", "Oct", "Nov", "Dec", "One", "Feb", "Mar"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Replaced Value", {"ID", "NUMBER", "TYPE ACTIVITY"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "YearMonth", each if [Attribute] = "Jul" then 202207
else if [Attribute] = "Ago" then 202208
else if [Attribute] = "Sep" then 202209
else if [Attribute] = "Oct" then 202210
else if [Attribute] = "Nov" then 202211
else if [Attribute] = "Dec" then 202212
else if [Attribute] = "One" then 202301
else if [Attribute] = "Feb" then 202202
else if [Attribute] = "Mar" then 202203
else null),
    #"Grouped Rows1" = Table.Group(#"Added Custom", {"ID", "NUMBER", "TYPE ACTIVITY", "Attribute", "YearMonth"}, {{"Sum1", each List.Sum([Value]), type number}}),
    #"Sorted Rows" = Table.Sort(#"Grouped Rows1",{{"NUMBER", Order.Ascending}, {"TYPE ACTIVITY", Order.Ascending}, {"ID", Order.Ascending}, {"YearMonth", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"ID", "NUMBER", "TYPE ACTIVITY"}, {{"Count", each _, type table [ID=nullable number, NUMBER=nullable text, TYPE ACTIVITY=nullable text, Attribute=text, Value=number, YearMonth=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Index",1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom1", "Custom", {"Attribute", "YearMonth", "Sum1", "Index"}, {"Custom.Attribute", "Custom.YearMonth", "Custom.Sum1", "Custom.Index"}),
    #"Added Custom2" = Table.AddColumn(#"Expanded Custom", "Sum2", each let 
_LastMonth = List.Max(Table.SelectRows(#"Expanded Custom",
each [ID] = _ID and[NUMBER] = _NUMBER and [TYPE ACTIVITY] = _TYPE
)[Custom.Index]),
_ID =[ID],
_NUMBER = [NUMBER],
_TYPE = [TYPE ACTIVITY],
_Index = [Custom.Index]
in
if [Custom.Index] = _LastMonth
then
List.Sum(Table.SelectRows(#"Expanded Custom",
each [ID] = _ID and[NUMBER] = _NUMBER and [TYPE ACTIVITY] = _TYPE and [Custom.Index] >=_Index -3)[Custom.Sum1])
else
null),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom2", each ([Sum2] <> null and [Sum2] <> 0)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Count", "Custom.Attribute", "Custom.YearMonth", "Custom.Sum1", "Custom.Index"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
    #"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Index", 1, 1, Int64.Type),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"ID", "NUMBER", "Index", "TYPE ACTIVITY", "Sum2"}),
    #"Pivoted Column" = Table.Pivot(#"Reordered Columns", List.Distinct(#"Reordered Columns"[#"TYPE ACTIVITY"]), "TYPE ACTIVITY", "Sum2", List.Sum)
in
    #"Pivoted Column"

Result is as below.

vrzhoumsft_0-1687255626330.png

Best Regards,
Rico Zhou

 

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

3 REPLIES 3
Syndicate_Admin
Administrator
Administrator

Original data table:

IDNUMBERTYPE ACTIVITYJulAgoSepOctNovDecOneFebMar
100FA1 2 1 1
101M A1 1 1 22
102G B 1 3081430814
102G A 2424157424157
102GC1 4292842928
102GD 23 21 2
102GC2451352451351 4
103NB 21836
103NA40124012132833
103NC 245135
103ND7 7
103NC 4012
104RB442063442063
104R A 7 7
104RC282631282631
105JB362927362927442063
105JA556556363134
105JC21 21 153514
105JD
106KB814 282631282631
106KA4157 362927362927
106KC2928 556556
106KD21 21 21

Expected table (sum of the hours of the last three months of each id and activity):

IDNUMBERABCD
100F1 2
101M 4
102G 50,9
102G 139,9 2
102G1 61,19
102G 23
102G23,851,34,6
103N 55
103N73
103N 109
103N
103N 16,351
104R
104R 6,952
104R
105J 127
105J101
105J 62,244
105J
106K 85
106K94
106K 16,552
106K3,1117

Anonymous
Not applicable

Hi @Syndicate_Admin ,

 

I suggest you to try code as below in advanced editor.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jVNBbsMgEPwLZx8Ca3B6dLFSqVV76DXy/79RBgNmlkSp5LVZe2Zndwz3u7GXi5nMLcWawqZIlzselh5mnwBH9s3wAepyHHisP1K8E0KgekU+q4xZa1PAmznfUMIvOmVepO6g4d5wu6qMWdspJeeyN2VUcSjngRE/prY2cBBR9ec0Q11ZDB1KUPi1tA2frNOJlTqaiCLGx0J9k0yABUsDLpr7n+pdbwccL37L1HN2H5+DjCmpVNraFx97Y3CsVriA8eyYEg3/6LO0JqFtjGVMqVMmo0FfIqi1NF2ZFS2O+6seI9/2jyZtDy2nmaD4VWZqR+yFLczM260dsxfWMDWaqTtqT41h0vbUibbc9z8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, NUMBER = _t, #"TYPE ACTIVITY" = _t, Jul = _t, Ago = _t, Sep = _t, Oct = _t, Nov = _t, Dec = _t, One = _t, Feb = _t, Mar = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"NUMBER", type text}, {"TYPE ACTIVITY", type text}, {"Jul", Int64.Type}, {"Ago", Int64.Type}, {"Sep", Int64.Type}, {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}, {"One", Int64.Type}, {"Feb", Int64.Type}, {"Mar", Int64.Type}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",null,0,Replacer.ReplaceValue,{"Jul", "Ago", "Sep", "Oct", "Nov", "Dec", "One", "Feb", "Mar"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Replaced Value", {"ID", "NUMBER", "TYPE ACTIVITY"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "YearMonth", each if [Attribute] = "Jul" then 202207
else if [Attribute] = "Ago" then 202208
else if [Attribute] = "Sep" then 202209
else if [Attribute] = "Oct" then 202210
else if [Attribute] = "Nov" then 202211
else if [Attribute] = "Dec" then 202212
else if [Attribute] = "One" then 202301
else if [Attribute] = "Feb" then 202202
else if [Attribute] = "Mar" then 202203
else null),
    #"Grouped Rows1" = Table.Group(#"Added Custom", {"ID", "NUMBER", "TYPE ACTIVITY", "Attribute", "YearMonth"}, {{"Sum1", each List.Sum([Value]), type number}}),
    #"Sorted Rows" = Table.Sort(#"Grouped Rows1",{{"NUMBER", Order.Ascending}, {"TYPE ACTIVITY", Order.Ascending}, {"ID", Order.Ascending}, {"YearMonth", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"ID", "NUMBER", "TYPE ACTIVITY"}, {{"Count", each _, type table [ID=nullable number, NUMBER=nullable text, TYPE ACTIVITY=nullable text, Attribute=text, Value=number, YearMonth=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Index",1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom1", "Custom", {"Attribute", "YearMonth", "Sum1", "Index"}, {"Custom.Attribute", "Custom.YearMonth", "Custom.Sum1", "Custom.Index"}),
    #"Added Custom2" = Table.AddColumn(#"Expanded Custom", "Sum2", each let 
_LastMonth = List.Max(Table.SelectRows(#"Expanded Custom",
each [ID] = _ID and[NUMBER] = _NUMBER and [TYPE ACTIVITY] = _TYPE
)[Custom.Index]),
_ID =[ID],
_NUMBER = [NUMBER],
_TYPE = [TYPE ACTIVITY],
_Index = [Custom.Index]
in
if [Custom.Index] = _LastMonth
then
List.Sum(Table.SelectRows(#"Expanded Custom",
each [ID] = _ID and[NUMBER] = _NUMBER and [TYPE ACTIVITY] = _TYPE and [Custom.Index] >=_Index -3)[Custom.Sum1])
else
null),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom2", each ([Sum2] <> null and [Sum2] <> 0)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Count", "Custom.Attribute", "Custom.YearMonth", "Custom.Sum1", "Custom.Index"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
    #"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Index", 1, 1, Int64.Type),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"ID", "NUMBER", "Index", "TYPE ACTIVITY", "Sum2"}),
    #"Pivoted Column" = Table.Pivot(#"Reordered Columns", List.Distinct(#"Reordered Columns"[#"TYPE ACTIVITY"]), "TYPE ACTIVITY", "Sum2", List.Sum)
in
    #"Pivoted Column"

Result is as below.

vrzhoumsft_0-1687255626330.png

Best Regards,
Rico Zhou

 

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

Mahesh0016
Super User
Super User

@Syndicate_Admin Please Share Your Dummy Data for better understanding.THANK YOU!!

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.