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
Tomcom05
New Member

Combining rows by order

I have data from our sales system that I am trying to prepare for an API connection. The orders are split into multiple rows for item detail and I am looking for a way to get the orders & items combined onto one line. I am fairly new to Power BI, but I feel like its something it can do. Any help would be great!

 

I have tried using group by with something similar to the following command, but it keeps giving errors about the wrong type of data.

#"Grouped Rows" = Table.Group(Source, {"transaction_id"}, {{"Items Detail", each Text.Combine({[line_ID],[line_description],[line__price], ", "), type text}})

Below are some examples of the way the data sits now and then below that is what we need it to look like.

 

Raw Data Example:

Raw.PNG

 

Fixed Data Example:

fixed.PNG

 

I have looked for someone who has the same issue without any luck, but if there is a thread that matches this issue please let me know.

 

-Tom

1 ACCEPTED SOLUTION

Hi,

Given the Table shared by MarkLaf, this M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"transaction", type text}, {"customer", type text}, {"line_ID", Int64.Type}, {"line_description", type text}, {"line__price", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"transaction", "customer"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(#"Unpivoted Other Columns",{"transaction", "Attribute"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
    Partition = Table.Group(#"Merged Columns", {"Merged"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"customer", "Value", "Index"}, {"customer", "Value", "Index"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Expanded Partition", "Merged", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Merged.1", "Merged.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Merged.1", Int64.Type}, {"Merged.2", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Merged.1", Order.Ascending}, {"Index", Order.Ascending}, {"Merged.2", Order.Ascending}}),
    #"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Sorted Rows", {{"Index", type text}}, "en-IN"),{"Merged.2", "Index"},Combiner.CombineTextByDelimiter("_", QuoteStyle.None),"Merged"),
    #"Pivoted Column" = Table.Pivot(#"Merged Columns1", List.Distinct(#"Merged Columns1"[Merged]), "Merged", "Value")
in
    #"Pivoted Column"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

9 REPLIES 9
MarkLaf
Solution Sage
Solution Sage

I think you have to use some M code to get what you want. The below code changes this:

transactioncustomerline_IDline_descriptionline__price
8606Cust13966Micro Sheet$11.99
1033Cust23630Dress$20.00
1033Cust26905Brand Blouse Size M$7.00
1033Cust22046White Blazer Size 14$7.00
9474Cust31274Old Fashioned Scale$26.00
9474Cust32001Toothbrush$3.87

 

to this (which I think is what you want):

transactioncustomer line_ID_1line_description_1line__price_1line_ID_2line_description_2line__price_2line_ID_3line_description_3line__price_3
8606Cust13966Micro Sheet$11.99nullnullnullnullnullnull
1033Cust23630Dress$20.006905Brand Blouse Size M$7.002046White Blazer Size 14$7.00
9474Cust31274Old Fashioned Scale$26.002001Toothbrush$3.87nullnullnull

 

Note that to get this working for your data, you'll have to change Source to point to your table, and then you'll have to replace relevant column names with your own (noted with comments in code):

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZA9C4MwFEX/SggdRV4+Gs1qSzfpYKGDOKT1QQQxkOjir29iXQp1ulw4By63bWmpQNGMXpYws5hCq1Tr4e0daSziHNuJsVxr2mUtZSDEjvOEKwExrh5DSCCHHOAfqDScY1TeTD2pRrcEJM2wIqmTVhxYHGRa87TDjNEyK/qvxeSPpmUhdy3pjG/1PvbkZoId3IQ9ad5mxG2jOtA4QLrg4dxsX34JNtEiLwvadR8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [transaction = _t, customer = _t, line_ID = _t, line_description = _t, line__price = _t]),
    GroupTrans = Table.Group(
        Source, 
        {"transaction", "customer"},  //enter all header-level columns here
        {{"transactions", each _}}
    ),
    FixTrans = Table.TransformColumns(GroupTrans,{"transactions", each 
    let
        AddIndex = Table.AddIndexColumn(_, "Index", 1, 1),
        Unpivot = Table.Unpivot(
            AddIndex, 
            {"line__price", "line_description", "line_ID"},  //enter all line-level columns here
            "Attribute", 
            "Value"
        ),
        AppendIndex = Table.ReplaceValue(Unpivot,each [Attribute],each [Attribute]&"_"&Text.From([Index]),Replacer.ReplaceText,{"Attribute"}),
        SelectCols = Table.SelectColumns(AppendIndex,{"Value", "Attribute"})
    in
        Table.Pivot(SelectCols, List.Distinct(SelectCols[Attribute]), "Attribute", "Value")
    }),
    AllCols = List.Distinct(List.Accumulate(FixTrans[transactions],{},(state,current)=> state & Table.ColumnNames(current))),
    Expand = Table.ExpandTableColumn(FixTrans, "transactions", AllCols)
in
    Expand

 

hthota
Resolver III
Resolver III

Could you please highlight the difference between the two images. Which column do you need to combine.

Its a combination of the second order. See red box for the new columns. 

 

fixed2.png


@hthota wrote:

Could you please highlight the difference between the two images. Which column do you need to combine.


 

Hi,

Given the Table shared by MarkLaf, this M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"transaction", type text}, {"customer", type text}, {"line_ID", Int64.Type}, {"line_description", type text}, {"line__price", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"transaction", "customer"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(#"Unpivoted Other Columns",{"transaction", "Attribute"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
    Partition = Table.Group(#"Merged Columns", {"Merged"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"customer", "Value", "Index"}, {"customer", "Value", "Index"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Expanded Partition", "Merged", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Merged.1", "Merged.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Merged.1", Int64.Type}, {"Merged.2", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Merged.1", Order.Ascending}, {"Index", Order.Ascending}, {"Merged.2", Order.Ascending}}),
    #"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Sorted Rows", {{"Index", type text}}, "en-IN"),{"Merged.2", "Index"},Combiner.CombineTextByDelimiter("_", QuoteStyle.None),"Merged"),
    #"Pivoted Column" = Table.Pivot(#"Merged Columns1", List.Distinct(#"Merged Columns1"[Merged]), "Merged", "Value")
in
    #"Pivoted Column"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks everyone! This is what i needed to get this project finished.

 

-Tom

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Create a Calculated Coulmn aas given below.

 

Column= TableName[line2_ID]&" "&TableName[line2_Des]&" "&TableName[line2_price]&" "&TableName[line3_ID]&" "&TableName[line2_Des]&" "&TableName[line2_price]


@hthota wrote:

Create a Calculated Coulmn aas given below.

 

Column= TableName[line2_ID]&" "&TableName[line2_Des]&" "&TableName[line2_price]&" "&TableName[line3_ID]&" "&TableName[line2_Des]&" "&TableName[line2_price]


I am trying to get a combined row per order with each order having an unknown number of lines. The [line#_id] don't exist yet so the above calculation gives me an error. Am I missing something?

 

The only similar posts I found on this are below.

https://community.powerbi.com/t5/Desktop/Combine-multiple-rows-of-User-data-into-one-row-with-additi... 

https://community.powerbi.com/t5/Desktop/Combine-values-of-multiple-rows-in-one-row/td-p/308919

 

I think it would work. Orelse please add two coulmns, later three etc in same coulmn and do testing to figure the effected column

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.