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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
akarasick1
Frequent Visitor

Lookup the smallest value and return name associated with that value

I have a dataset that looks like the table below. I'm trying to build a measure that will let me calculate the smallest difference between an amount and any other amount in that table. I'd then like to return the name that generated the smallest value.

 

NameAmount
A1
B4
C9
D16

 

For example:

  For Name A, the minimum difference calculated is 3 (4-1; 9-1; 16-1) and that corresponds with B.

  For Name C, the minimum difference calculated is -8 (1-9; 4-9; 16-9) which corresponds with A.

 

Any idea on how I can create a measure that does the amount difference calculation iterating across the entire table (all other amounts less current row amount) as well as a measure that returns the corresponding name for that minimum difference?

 

Thank you in advance

3 REPLIES 3
mahoneypat
Microsoft Employee
Microsoft Employee

I believe this measure will do what you are looking for.  I made a table called 'Data' with your values.  Also included comments on each line to explain how it works.

 

Min Letter = var currentletter = SELECTEDVALUE(Data[Name]) // store current letter/Name
var currentlettervalue = SELECTEDVALUE(Data[Amount]) // store the current Amount for this letter
var differences = FILTER(ADDCOLUMNS(All(Data[Name], Data[Amount]), "Difference", Data[Amount]-currentlettervalue), Data[Name]<>currentletter) // make a table with all data and the difference from current value; Filter out the row for current letter
var minletter = CALCULATE(MIN(Data[Name]), TOPN(1, differences,[Difference],ASC)) // get the Name value where difference is the lowest
 
return minletter




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


harshnathani
Community Champion
Community Champion

Hi @akarasick1 ,

 

 

Its quite a tricky problem and requires to be solved via relationship.

 

Your Original Table:

 

Table2Table2

 

1. Create a new Table:

 

New Table = ADDCOLUMNS(
ADDCOLUMNS(
SUMMARIZE(
Table2,
Table2[Name],
Table2[Amount]
),
"Minimim",
If(MIN(Table2[Amount]) = Table2[Amount],
CALCULATE(Min(Table2[Amount]),FILTER(Table2,Table2[Amount]>EARLIER(Table2[Amount]))),
CALCULATE(MIN(Table2[Amount]),FILTER(Table2,Table2[Amount]<=EARLIER(Table2[Amount]))))
),
 
"Difference", [Minimim]-Table2[Amount])

 

2.JPG

 

 

3. Create a Summary Table:

 

Summary Table = SUMMARIZE('New Table','New Table'[Amount],'New Table'[Name])
 
3.JPG
 
 
4. Manage Relationship
 
123.JPG
 
 
5. Visualization
 
4.JPG
 
 
Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!!
 

Thank you for all of your effort! I'm worried that joining based on amount will cause some issues if the amounts are the same for more than one value. Any thoughts on that?

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

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.