Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
To retrieve the top 5 values from each category based on a specific field in Power BI, I used below measure but it giving me all negative records.
Top5Negative Records =
CALCULATE(
[sum_of_values],
FILTER(
ALL(data[overall_feedback]),
data[overall_feedback] = "negative"
),
TOPN(5, data, data[compound], DESC)
)
smaple data:
ID | values | result |
1 | -6 | negative |
2 | -5 | negative |
3 | -4 | negative |
4 | -3 | negative |
5 | -2 | negative |
6 | -2 | negative |
7 | -1 | negative |
8 | -1 | negative |
9 | 1.75 | positive |
10 | 1.75 | positive |
11 | 1.777 | positive |
12 | 1.8162 | positive |
13 | 1.8832 | positive |
14 | 1.8908 | positive |
15 | 1.892 | positive |
16 | 1.9348 | positive |
17 | 1.9348 | positive |
18 | 2.1009 | positive |
19 | 2.5443 | positive |
20 | 3.0544 | positive |
21 | 0 | neutral |
22 | 0 | neutral |
23 | 0.1 | neutral |
24 | 0.2 | neutral |
expected output: The desired output is to show all tied values when calculating the top 5 records based on a values field.
ID | values | result |
1 | -6 | negative |
2 | -5 | negative |
3 | -4 | negative |
4 | -3 | negative |
5 | -2 | negative |
6 | -2 | negative |
14 | 1.892 | positive |
15 | 1.9348 | positive |
16 | 1.9348 | positive |
17 | 2.1009 | positive |
18 | 2.5443 | positive |
19 | 3.0544 | positive |
@rubayatyasmin@Vijay_A_Verma
Solved! Go to Solution.
Let's work on getting the desired output where all tied values are included when calculating the top 5 records based on the values field.
Here's how you can achieve this in M Query:
Load Your Data Source:
Sort the Table:
Identify the Top 5 Values:
Filter the Table:
Here's a sample M Query script to achieve this:
let // Step 1: Load your data source Source = Excel.Workbook(File.Contents("YourFilePath.xlsx"), null, true), Data = Source{[Name="YourSheetName"]}[Data], // Step 2: Sort the table by the "values" column in descending order SortedTable = Table.Sort(Data, {{"values", Order.Descending}}), // Step 3: Identify the unique top 5 values Top5Values = List.FirstN(List.Distinct(List.Sort(Table.Column(SortedTable, "values"), Order.Descending)), 5), // Step 4: Filter the table to include all rows with the top 5 values FilteredTable = Table.SelectRows(SortedTable, each List.Contains(Top5Values, [values])) in FilteredTable
Replace "YourFilePath.xlsx" and "YourSheetName" with your actual file path and sheet name. This script will sort the table by the values column, identify the unique top 5 values, and filter the table to include all rows with those values.
Hi,
File is blank.
for value .99 it gives diiferent ranking
what should be?
did you try to change DESC to ASC
yes, I did but no luck.
show what the result should be
I did round(values,2) and its working.
can't be.
check again
now file is not available. Can you upload it
getting this error while creating measure A single value for column 'values' in table 'data' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.
@atpostdata Hey, I gave you the exact solution you asked for. I wrote the measures also. And attached a screenshot of the result. Did you follow my steps? Do you need pbix?
Proud to be a Super User!
Hi ,
I followed the same steps, its working for negative overall_feedback value but not for postive as postive values rank is comming greater than 5.
overall_feedback contains three values postive, negative and neutral.
need top 5 records from each category based on values field.
Hi, @atpostdata thanks for reaching out.
I have tried with your demo data and achieved the result. The way I did it is by creating 3 calculated columns.
1. Rank ASC column
2. Rank DSC column
3. to check whether or not these columns fall under top5
then used a visual level filter where the third columns value is true
sample code for 3 calculated column
1.
Proud to be a Super User!
Share sample pbix file to help you.
@Ahmedx @rubayatyasmin : sharing the sample data and .pbix file on below link.
powerbi
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
143 | |
85 | |
66 | |
51 | |
45 |
User | Count |
---|---|
217 | |
89 | |
82 | |
66 | |
57 |