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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

How to get a column value from the first row in a sorted table?

Hi,

 

I am trying to get the first non-blank value from a table that has been sorted in the query editor. The sorting is crucial to which row is first in the table.

 

I have searched many posts and understand that FIRSTNONBLANK follows this rule. "It's important to remember that the order you see the data in the tables does not really matter " when using FIRSTNONBLANK, it acts as a MIN in the measurement context.

 

Data:

ResourceDateTime

SL38 TimeDifference

SL3811/12/2019 12:30:00 PM

5

SL3811/12/2019 11:48:00 PM10
SL3811/11/2019 6:12:00 PM0
SL3811/11/2019 1:57:00 AM 50
SL40........
.... ........
 

Proposed Solution #1 Measurement:

 

SL38 Recent Time = 
CALCULATE(
    FIRSTNONBLANK('Molding'[SL38 TimeDifference], 'Molding'[SL38 TimeDifference]), 
    // Sort the rows by descending DateTime
    Filter('Molding', 'Molding'[Resource] = "SL38")
    )

 

Results: 0

Desired Results: 5 (The most recent DateTime)

 

First Proposed Method: Use FIRSTNONBLANK and somehow sort the columns in the measurement by DateTime.

Second Proposed Method: Use TOPN to retrieve the first row and somehow get the Time Difference column from it.

Third Proposed Method: Use LOOKUPVALUE to search by the most recent date.  The most recent date happens multiple times in the DateTime column so it somehow needs to filter by Resource.


Starter Code for #2:

SL38 Recent Time = 
CALCULATE(
    TOPN(1, 'Molding', 'Molding'[DateTime], 0), // Get the value for the SL38 Time Difference column
    Filter('Molding', 'Molding'[Resource] = "SL38")
    )

 

Starter Code for #3:

 

SL38 Recent Time = 
CALCULATE(
    LOOKUPVALUE(Molding[SL38 TimeDifference], Molding[DateTime], LASTNONBLANK('Molding'[DateTime], 'Molding'[DateTime])) // Filter by Resource, 
    Filter('Molding', 'Molding'[Resource] = "SL38")
    )

 

 

 

Any help is appreciated.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

@parry2k I solved the question by doing the following code. For some reason, even though LASTNONBLANK and MAX on DateTime returned the exact same date, MAX was the one that worked correctly.

 

SL38 Recent Time = 
CALCULATE(
    MAX('Molding'[SL38 TimeDifference]), 
    FILTER('Molding', 'Molding'[DateTime] = MAX('Molding'[DateTime])),
    FILTER('Molding', 'Molding'[Resource] = "SL38")
    )

 

View solution in original post

6 REPLIES 6
parry2k
Super User
Super User

@Anonymous once you sorted your data in power query, you should add a index column and then from there it will be much easier to get the value of first row.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

@parry2k I have an index column. However, since my SQL query is connecting 6 different resources, only 1 resource has the 0th index.

 

Do you know of a method to get the column value (TimeDifference) from the row with the minimum index of that resource?

@Anonymous is it possible to share sample data in excel sheet and I will get you the solution. I think that will be much quicker.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Anonymous
Not applicable

@parry2k I solved the question by doing the following code. For some reason, even though LASTNONBLANK and MAX on DateTime returned the exact same date, MAX was the one that worked correctly.

 

SL38 Recent Time = 
CALCULATE(
    MAX('Molding'[SL38 TimeDifference]), 
    FILTER('Molding', 'Molding'[DateTime] = MAX('Molding'[DateTime])),
    FILTER('Molding', 'Molding'[Resource] = "SL38")
    )

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.