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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
vpsoini
Helper I
Helper I

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

 

OrdernumberItemItem categoryLocationQuantitySum
PO001Item1101Warehouse 1110
PO001Item2110Warehouse 1212
PO001Postage900 15
PO002Item3200Warehouse 2225
PO002Freight900 19
PO003Freight900 110
PO003Item 10101Warehouse 1130
PO004Item 5105Warehouse 2116
PO004Mail900 14
      

 

My goal would be getting warehouse 1 lines filtered so, that it would include shipping costs (category 900) in those cases, where they belong to shipments from warehouse 1.  Unfortunately my data does not include Location info for those shipping costs lines, so the condition should be:

 

"include all lines where location code is "Warehouse 1" OR is empty AND ordernumber is same with lines, where location code = Warehouse 1"

 

The shipping cost line may have different "item names" (postage, freight etc.) but it has the same category (900) and is the only one with empty location info. The shiping cost line may be in any order (first , last, in the middle) compared to other lines in same order (with same PO-number), so it's not always e.g. last. 

 

My previous step in transforming data is "Removed columns" and the beginnig of the query could be:

 

= Table.SelectRows(#"Removed columns", each ([Location_Code] = "Warehouse 1" or [Location_Code] = "" and *insert magic here* ))

 

Could you help me figuring out how to gain this result after filtering:

 

 

OrdernumberItemItem categoryLocationQuantitySum
PO001Item1101Warehouse 1110
PO001Item2110Warehouse 1212
PO001Postage900 15
PO003Freight900 110
PO003Item 10101Warehouse 1130
      

 

A big thanks in advance for your help 😊

2 ACCEPTED SOLUTIONS
BA_Pete
Super User
Super User

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



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

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:

BA_Pete_0-1639152190399.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

3 REPLIES 3
BA_Pete
Super User
Super User

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



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Hello @BA_Pete 

 

There was a twist on this case, which is described in the picture below.  I can see, that the original solution was based on replacing " " with "null", but now I would need to be able to fill down only those cells with common order number with upper cell?

 

In picture below empty values in column "Location_Code" need to filled down, if there's line with same orderno having the location code.  So order numbers 300085,..88 and..93 should be left empty, since rows having both that number AND location code "VARAOSAV" do not exist and only rows below ordernumbers 300091 and ...94  should be filled, because they do have line(s) having both the same ordernumber AND location code present.

 

How that could be accomplished?

 

fill_down.jpg

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:

BA_Pete_0-1639152190399.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors