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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
nicoleddss
New Member

Select and compare the 2 most recent results in a category

Hello,

 

I have a table that is structured, at the most basic level, like this (dummy data)

nicoleddss_0-1754244965007.png

 

I have a report to allow the user to select an analyte (example: Glucose). From there, I would like it to present the two most resent lab results, list, compare and be able to say what the difference is. the result would look similar to these two examples:

 

nicoleddss_1-1754245056253.png

 

and this:

nicoleddss_2-1754245166521.png

(column names are long to help explain the expected values). Any idea how to return only the results I want?

 

Thanks for any help you can offer!

2 ACCEPTED SOLUTIONS
burakkaragoz
Community Champion
Community Champion

Hi @nicoleddss ,

 

Looking at your data and desired output, you need measures that find the top 2 most recent records for the selected analyte and calculate the difference.

Here's what you need:

Most Recent Date:

Most Recent Date = 
CALCULATE(
    MAX(YourTable[Date]),
    ALLEXCEPT(YourTable, YourTable[Analyte])
)

Most Recent Amount:

Most Recent Amount = 
CALCULATE(
    MAX(YourTable[Amount]),
    YourTable[Date] = [Most Recent Date]
)

2nd Most Recent Date:

2nd Most Recent Date = 
VAR MostRecentDate = [Most Recent Date]
RETURN
CALCULATE(
    MAX(YourTable[Date]),
    YourTable[Date] < MostRecentDate,
    ALLEXCEPT(YourTable, YourTable[Analyte])
)

2nd Most Recent Amount:

2nd Most Recent Amount = 
CALCULATE(
    MAX(YourTable[Amount]),
    YourTable[Date] = [2nd Most Recent Date]
)

Difference:

Difference = [Most Recent Amount] - [2nd Most Recent Amount]

For your table visual: Create a disconnected table with the row labels:

RowLabels = DATATABLE("Row", STRING, {{"Most Recent"}, {"2nd Most Recent"}, {"Difference"}})

Then create a dynamic measure that returns the right value based on which row is selected:

Dynamic Value = 
SWITCH(
    SELECTEDVALUE(RowLabels[Row]),
    "Most Recent", [Most Recent Amount],
    "2nd Most Recent", [2nd Most Recent Amount], 
    "Difference", [Difference],
    BLANK()
)

Use RowLabels[Row] on rows and your Dynamic Value measure in the values section. This approach gives you exactly the layout shown in your screenshots.


If my response resolved your query, kindly mark it as the Accepted Solution to assist others. Additionally, I would be grateful for a 'Kudos' if you found my response helpful.
This response was assisted by AI for translation and formatting purposes.

View solution in original post

lbendlin
Super User
Super User

The fastest way to do that is to use visual calculations

 

lbendlin_0-1754250781018.png

 

No need to limit yourself to the last two values.

 

lbendlin_2-1754250979310.png

 

 

 

View solution in original post

6 REPLIES 6
v-venuppu
Community Support
Community Support

Hi @nicoleddss ,

Thank you for reaching out to Microsoft Fabric Community.

Thank you @burakkaragoz @lbendlin for the prompt response.

Can you please post the solution here that worked out for you.This will be helpful for other community members who have similar problems to solve it faster.

Hello,

I wanted to note that I accepted both solutions. One solution was a quick way to get what I needed at the moment. The second was more specific to a longer-term solution. I used one on each report. Thank you to both responders!

lbendlin
Super User
Super User

The fastest way to do that is to use visual calculations

 

lbendlin_0-1754250781018.png

 

No need to limit yourself to the last two values.

 

lbendlin_2-1754250979310.png

 

 

 

Yes. That was fast! lol. Thank you!😁

burakkaragoz
Community Champion
Community Champion

Hi @nicoleddss ,

 

Looking at your data and desired output, you need measures that find the top 2 most recent records for the selected analyte and calculate the difference.

Here's what you need:

Most Recent Date:

Most Recent Date = 
CALCULATE(
    MAX(YourTable[Date]),
    ALLEXCEPT(YourTable, YourTable[Analyte])
)

Most Recent Amount:

Most Recent Amount = 
CALCULATE(
    MAX(YourTable[Amount]),
    YourTable[Date] = [Most Recent Date]
)

2nd Most Recent Date:

2nd Most Recent Date = 
VAR MostRecentDate = [Most Recent Date]
RETURN
CALCULATE(
    MAX(YourTable[Date]),
    YourTable[Date] < MostRecentDate,
    ALLEXCEPT(YourTable, YourTable[Analyte])
)

2nd Most Recent Amount:

2nd Most Recent Amount = 
CALCULATE(
    MAX(YourTable[Amount]),
    YourTable[Date] = [2nd Most Recent Date]
)

Difference:

Difference = [Most Recent Amount] - [2nd Most Recent Amount]

For your table visual: Create a disconnected table with the row labels:

RowLabels = DATATABLE("Row", STRING, {{"Most Recent"}, {"2nd Most Recent"}, {"Difference"}})

Then create a dynamic measure that returns the right value based on which row is selected:

Dynamic Value = 
SWITCH(
    SELECTEDVALUE(RowLabels[Row]),
    "Most Recent", [Most Recent Amount],
    "2nd Most Recent", [2nd Most Recent Amount], 
    "Difference", [Difference],
    BLANK()
)

Use RowLabels[Row] on rows and your Dynamic Value measure in the values section. This approach gives you exactly the layout shown in your screenshots.


If my response resolved your query, kindly mark it as the Accepted Solution to assist others. Additionally, I would be grateful for a 'Kudos' if you found my response helpful.
This response was assisted by AI for translation and formatting purposes.

Thank you very kindly burakkaragoz! It looks like the right approach for sure. I didn't try it yet because the other solution will work right now. But I will save it and let you know. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.