Forum Discussion

Chrisjr's avatar
Chrisjr
Icon for Helper IV rankHelper IV
3 years ago
Solved

grouping values based on a reoccuring row

Hello All, 

 

I have the following dataset: 

BrandValue
Nike 
Adidas  
Average 

Reebok

 

 

Decat

 
triatl 

 

I would like to be able to categorize all the brands above the line "Average" as "Direct" and all the ones below "Average" as "Indirect" in a new column. 

The row where "Average" is, is not always the same. Sometimes, Average can be on row 4, sometimes on row 5 or 2, etc...

 

 

The end result would look like this:

BrandValueDirect/Indirect
Nike Direct
Adidas  Direct
Average Average
Reebok Indirect

Decat

 Indirect
triatl Indirect

 

How can I achieve this? 

 

Thanks for the help

  • let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs5PTlTSUTJVitWJVgouKMosSQVyTcBct8S8EoSkf1FiXnpmHkjAHCzgWJZalJgOUm4A1Z0I1+qcWFySA5IyA3NdgPrySuBcp9TUNLgxXqUFcG3hmXmpcAnX4pLEEoQZzhmJpSlp+UAnQRQZK8XGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Brand = _t, Price = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Brand", type text}, {"Price", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "RTM", each let i =  Table.SelectRows(#"Added Index",each [Price]=0){0}[Index]
    in if [Index]<i then "Direct" else if [Index]=i then "" else "Indirect")
    in
        #"Added Custom"

    How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

8 Replies