Forum Discussion

vpsoini's avatar
vpsoini
Icon for Helper I rankHelper I
4 years ago
Solved

Selecting rows with condition based on other lines having common value

Hi.   I have table with sales lines, including items, item categories, locations and associated freight costs. Below is simplified example   Ordernumber Item Item category Location Quantit...
  • BA_Pete's avatar
    4 years ago

    Hi vpsoini ,

     

    You can fill down the warehouses in the data, the trick is making sure they're in an appropriate order:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvA3MDBU0lHyLEnNBdGGYF54YlFqRn5pcaoCWAwsrhSrg6rcCCQMFEdXDhY3QlEekF9ckpieCmRZGoA0KEBNNYWrMoIaagwywADVUCOooUaoyt2KUjPTM0owDLWEqzLGowrJQ8ZQuxXAnsEVAsYIDSYwDaZg9aYYrgXrMUNR75uYmYPhCBOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Ordernumber = _t, Item = _t, #"Item category" = _t, Location = _t, Quantity = _t, Sum = _t]),
        #"Replaced Value" = Table.ReplaceValue(Source," ",null,Replacer.ReplaceValue,{"Location"}),
        #"Sorted Rows" = Table.Sort(#"Replaced Value",{{"Ordernumber", Order.Ascending}, {"Item category", Order.Ascending}}),
        #"Filled Down" = Table.FillDown(#"Sorted Rows",{"Location"})
    in
        #"Filled Down"

     

    In this case I've sorted by [OrderNumber] then [Location], then used Transform tab > Fill Down on the [Location] column.

     

    Fom here, your filters/calculations etc. should be easy.

     

    Pete

  • BA_Pete's avatar
    BA_Pete
    4 years ago

    Hi vpsoini ,

     

    Try this code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvA3MDBU0lHyLEnNBdGGYF54YlFqRn5pcaoCWAwsrhSrg6rcCCQMFEdXDhY3QlEekF9ckpieCmRZGoA0KEBNNYWrMoIaagwyAKwGapIRqhq3otTM9IwSDJMs4aqM8ahC8oUx1EIFsA9wedsYocEEpsEUrN4URb0RzAIzFPW+iZk5GI4wgSsxRSiBGAj1s4VSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Ordernumber = _t, Item = _t, #"Item category" = _t, Location = _t, Quantity = _t, Sum = _t]),
        #"Replaced Value" = Table.ReplaceValue(Source," ",null,Replacer.ReplaceValue,{"Location"}),
        #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","",null,Replacer.ReplaceValue,{"Location"}),
        #"Sorted Rows" = Table.Sort(#"Replaced Value1",{{"Ordernumber", Order.Ascending}, {"Item category", Order.Ascending}}),
        #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Ordernumber"}, {{"data", each _, type table [Ordernumber=nullable text, Item=nullable text, Item category=nullable text, Location=nullable text, Quantity=nullable text, Sum=nullable text]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "fillLocation", each Table.FillDown([data], {"Location"})),
        #"Expanded fillLocation" = Table.ExpandTableColumn(#"Added Custom", "fillLocation", {"Item", "Item category", "Location", "Quantity", "Sum"}, {"Item", "Item category", "Location", "Quantity", "Sum"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded fillLocation",{"data"})
    in
        #"Removed Columns"

     

    Essentially the same as before but I've grouped on [Ordernumber] and nested All Rows, then performed the fill-down on the nested table, then expanded the new column.

     

    I get the following output:

     

     

    Pete