Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
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 in the "Score" column would be "Y" and all others would be "N". Could someone help me as to which M functions to use here?
Sample Data:
| Score |
| 0.9899 |
| 0.9889 |
| 0.9056 |
| 0.8463 |
| 0.8377 |
| 0.693 |
| 0.6666 |
| 0.5778 |
| 0.5602 |
| 0.489 |
| 0.3918 |
| 0.3034 |
| 0.1654 |
| 0.1012 |
| 0.0771 |
| 0.0506 |
| 0.0418 |
| 0.0053 |
| 0.0032 |
Desired result:
| Score | Is Top 10? |
| 0.9899 | Y |
| 0.9889 | Y |
| 0.9056 | Y |
| 0.8463 | Y |
| 0.8377 | Y |
| 0.693 | Y |
| 0.6666 | Y |
| 0.5778 | Y |
| 0.5602 | Y |
| 0.489 | Y |
| 0.3918 | N |
| 0.3034 | N |
| 0.1654 | N |
| 0.1012 | N |
| 0.0771 | N |
| 0.0506 | N |
| 0.0418 | N |
| 0.0053 | N |
| 0.0032 | N |
Thank you!
Solved! Go to Solution.
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"
Hi @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
Proud to be a Datanaut!
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"
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.
| User | Count |
|---|---|
| 5 | |
| 3 | |
| 3 | |
| 3 | |
| 2 |
| User | Count |
|---|---|
| 7 | |
| 5 | |
| 5 | |
| 5 | |
| 4 |