Forum Discussion
Anonymous
4 years agoNot applicable
Add new column based on whether another column's value is top N
Hey guys, I've got a column of floating-point values, sorted in descending order, called "Score". I'm trying to add a new column called "Is Top 10?" where the value for the top 10 largest values ...
- 4 years ago
In a custom column, put following
= if List.Contains(List.FirstN(List.Sort(List.Distinct(#"Changed Type"[Score]),Order.Descending),10),[Score]) then "Y" else "N"See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Pc3LCcBACATQXvYcwhj/tSzpv40sRPX0UGbce+HOyFzv9TOGUCuGGDfZvWjZSztTVPdoGp6iTCsn9Z3BUiTTIahTcKemoj9ApgFQHvKJvR8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Score = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Score", type number}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Is Top 10?", each if List.Contains(List.FirstN(List.Sort(List.Distinct(#"Changed Type"[Score]),Order.Descending),10),[Score]) then "Y" else "N", type text) in #"Added Custom"
BA_Pete
Super User
4 years agoHi Anonymous ,
Try this:
if
List.Contains(
List.FirstN(
List.Sort(chgTypes[Score], Order.Descending),
10
),
[Score]
)
then "Y" else "N"
Full query:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Pc3LCcBACATQXvYcwhj/tSzpv40sRPX0UGbce+HOyFzv9TOGUCuGGDfZvWjZSztTVPdoGp6iTCsn9Z3BUiTTIahTcKemoj9ApgFQHvKJvR8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Score = _t]),
chgTypes = Table.TransformColumnTypes(Source,{{"Score", type number}}),
addIsTop10 = Table.AddColumn(chgTypes, "isTop10", each if
List.Contains(
List.FirstN(
List.Sort(chgTypes[Score], Order.Descending),
10
),
[Score]
)
then "Y" else "N")
in
addIsTop10
Output:
Pete
- Anonymous4 years agoNot applicable
Thank you, BA_Pete!