Forum Discussion

apoje's avatar
apoje
Helper II
6 years ago
Solved

Sorting by multiple columns and creating a distinct count helper column

Hi,

 

I have a dataset, which has multiple order IDs, some orders only contain one product and therefore occupy only one row. Some orders have multiple products and have multiple rows assigned to them. 

 

Order.id

item.id

Quantity

Item-Group

10

id_1

20

Group-A

11

id_6

5

Group-B

12

id_1

1

Group-A

12

id_5

20

Group-D

13

id_3

10

Group-C

14

id_1

1

Group-A

14

id_3

1

Group-C

15

id_1

15

Group-A

 

This datasheet is than printed and submitted in the warehouse with the information what to pack. For the warehouse to be efficient it would make sense that all the identical product would be sorted together. But the problem is that if the order contain multiple products and than has multiple rows those need to be side by side - because they need to be packed together. 

 

I want to filter by products so the sheet has ordered products but also taken in the account if the order contains multiple products those should not be separated.

 

I think the solution should be done by:

  1. creating a helper column which counts how many times the unique order.id appears in the dataset. 
  2. than sort by:

   2.1. Item.id

   2.2. helper counter column

   2.3. Order.Id

 

those 3 layered filter would probably create a the desired result.  The reuslt I am looking for:

 

Order.id

Helper.Count

item.id

Quantity

Item-Group

10

1

id_1

20

Group-A

15

1

id_1

15

Group-A

13

1

id_3

10

Group-C

11

1

id_6

5

Group-B

12

2

id_1

1

Group-A

12

2

id_5

20

Group-D

14

2

id_1

1

Group-A

14

2

id_3

1

Group-C

 

Below is the current code which does not entail the sorting function, because I do not know how to implement the helper count distinct column. 

 

 

let
    Source = Csv.Document(File.Contents("C:\Us...ta\I.csv"),[Delimiter=";", Columns=15, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order.id-Ihre.Referenz", Int64.Type}, {"Vorname_firstName", type text}, {"Nachname_lastName", type text}, {"Firma_companyName", type text}, {"Strasse_street", type text}, {"Hausnummer_houseNumber", Int64.Type}, {"Ansprechperson_address3", type text}, {"Ort_town", type text}, {"PLZ_zip", Int64.Type}, {"Land-country", type text}, {"email", type text}, {"phone", type text}, {"Variation.number", type text}, {"Variation.name", type text}, {"Variation.quantity", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Strasse_street", "Hausnummer_houseNumber", "Ansprechperson_address3", "Ort_town", "PLZ_zip", "Land-country", "email", "phone"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Variation.number] <> "")),
    #"Filtered Rows1" = Table.SelectRows(#"Filtered Rows", each not Text.Contains([Variation.number], "-")),
    #"Filtered Rows2" = Table.SelectRows(#"Filtered Rows1", each not Text.Contains([Variation.number], "_")),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows2",{"Vorname_firstName", "Firma_companyName"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Nachname_lastName", "LastName"}, {"Order.id-Ihre.Referenz", "Order.id"}, {"Variation.quantity", "Quantity"}}),
    #"Grouped Rows" = Table.Group(#"Renamed Columns", {"Order.id", "LastName", "Variation.number", "Variation.name"}, {{"Quantity", each List.Sum([Quantity]), type number}}),
    #"Replaced Value" = Table.ReplaceValue(#"Grouped Rows","ö","ö",Replacer.ReplaceText,{"LastName"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","ß","ß",Replacer.ReplaceText,{"LastName"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","ü","ü",Replacer.ReplaceText,{"LastName"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value2",{{"Quantity", Int64.Type}}),
    #"Replaced Value3" = Table.ReplaceValue(#"Changed Type1","ä","ä",Replacer.ReplaceText,{"LastName"}),
    #"Replaced Value4" = Table.ReplaceValue(#"Replaced Value3","Ö","Ö",Replacer.ReplaceText,{"LastName"}),
    #"Filtered - aluprofil" = Table.SelectRows(#"Replaced Value4", each not Text.Contains([Variation.name], "Aluprofil")),
    #"Filtered - alu" = Table.SelectRows(#"Filtered - aluprofil", each not Text.Contains([Variation.name], "ALU"))
in
    #"Filtered - alu"

 

 

Thanks for the help!

Andraz

  • Hi apoje ,

     

    Try this one:

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRQ0lHKTIk3BFJGILZ7UX5pga6jUqwOUNIQImkGpEzhck4QOSOERkN0fVA5U1RDXSCSxhBJEGWIkHSGSJrgMdUESSO6PlMkfabIGmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Order.id = _t, item.id = _t, Quantity = _t, #"Item-Group" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order.id", Int64.Type}, {"item.id", type text}, {"Quantity", Int64.Type}, {"Item-Group", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "HelperCounter", each let _order_id = [Order.id] in
    List.Count(
    List.Select(#"Changed Type"[Order.id],
    each _ = _order_id))),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"HelperCounter", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"HelperCounter", Order.Ascending}, {"Order.id", Order.Ascending}, {"item.id", Order.Ascending}})
    in
    #"Sorted Rows"

  • Hi apoje ,

     

    You could use Table.Group() function to get count numbers then merge it with the original table.

    Here is the codes for your reference:

    let
        Source = Excel.Workbook(File.Contents("C:\User......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",{{"Order.id", Int64.Type}, {"item.id", type text}, {"Quantity", Int64.Type}, {"Item-Group", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Order.id"}, {{"HelperCounter", each Table.RowCount(_), Int64.Type}}),
        #"Merged Queries" = Table.NestedJoin(#"Grouped Rows", {"Order.id"}, #"Changed Type", {"Order.id"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"item.id", "Quantity", "Item-Group"}, {"Grouped Rows.item.id", "Grouped Rows.Quantity", "Grouped Rows.Item-Group"}),
        #"Sorted Rows" = Table.Sort(#"Expanded Grouped Rows",{{"HelperCounter", Order.Ascending}, {"Order.id", Order.Ascending}, {"Grouped Rows.item.id", Order.Ascending}})
    in
        #"Sorted Rows"

     

2 Replies

  • camargos88's avatar
    camargos88
    Community Champion

    Hi apoje ,

     

    Try this one:

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRQ0lHKTIk3BFJGILZ7UX5pga6jUqwOUNIQImkGpEzhck4QOSOERkN0fVA5U1RDXSCSxhBJEGWIkHSGSJrgMdUESSO6PlMkfabIGmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Order.id = _t, item.id = _t, Quantity = _t, #"Item-Group" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order.id", Int64.Type}, {"item.id", type text}, {"Quantity", Int64.Type}, {"Item-Group", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "HelperCounter", each let _order_id = [Order.id] in
    List.Count(
    List.Select(#"Changed Type"[Order.id],
    each _ = _order_id))),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"HelperCounter", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"HelperCounter", Order.Ascending}, {"Order.id", Order.Ascending}, {"item.id", Order.Ascending}})
    in
    #"Sorted Rows"

  • v-eachen-msft's avatar
    v-eachen-msft
    Community Support

    Hi apoje ,

     

    You could use Table.Group() function to get count numbers then merge it with the original table.

    Here is the codes for your reference:

    let
        Source = Excel.Workbook(File.Contents("C:\User......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",{{"Order.id", Int64.Type}, {"item.id", type text}, {"Quantity", Int64.Type}, {"Item-Group", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Order.id"}, {{"HelperCounter", each Table.RowCount(_), Int64.Type}}),
        #"Merged Queries" = Table.NestedJoin(#"Grouped Rows", {"Order.id"}, #"Changed Type", {"Order.id"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"item.id", "Quantity", "Item-Group"}, {"Grouped Rows.item.id", "Grouped Rows.Quantity", "Grouped Rows.Item-Group"}),
        #"Sorted Rows" = Table.Sort(#"Expanded Grouped Rows",{{"HelperCounter", Order.Ascending}, {"Order.id", Order.Ascending}, {"Grouped Rows.item.id", Order.Ascending}})
    in
        #"Sorted Rows"