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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Anonymous
Not applicable

Simplifying a code using the "earlier" function

Hello,


I would like to filter the departure and arrival times of the cranes. I have a database with the times of the different cranes mixed together and with this code it calculates the intermediate time i.e. arrival time of the 1st crane - next departure time for the 1st crane.

The code sorts the filtered times by bridges but if I have a lot of lines this code does not work anymore because of the "earlier" function which is very greedy for the pc, how to simplify this code please?

 

CALCULATE(MIN('Bridge Hall 3 M'[DateHour_Origin]),FILTER('bridge Hall 3 M','bridge Hall 3 M'[Number_bridge]=EARLIER('bridge Hall 3 M'[Number_Pont])&&'bridge Hall 3 M'[DateHour_Origin]>EARLIER('bridge Hall 3 M'[DateHour_Origin])))-'bridge Hall 3 M'[Datehour_Arrival]
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous , Sorry, Try like

 

CALCULATE(MIN('Bridge Hall 3 M'[DateHour_Origin]),FILTER('bridge Hall 3 M','bridge Hall 3 M'[Number_bridge]=EARLIER('bridge Hall 3 M'[Number_Pont])
&&'bridge Hall 3 M'[Date Rank] = EARLIER('bridge Hall 3 M'[Date Rank]) +1 ))-'bridge Hall 3 M'[Datehour_Arrival])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

6 REPLIES 6
amitchandak
Super User
Super User

@Anonymous , Sorry, Try like

 

CALCULATE(MIN('Bridge Hall 3 M'[DateHour_Origin]),FILTER('bridge Hall 3 M','bridge Hall 3 M'[Number_bridge]=EARLIER('bridge Hall 3 M'[Number_Pont])
&&'bridge Hall 3 M'[Date Rank] = EARLIER('bridge Hall 3 M'[Date Rank]) +1 ))-'bridge Hall 3 M'[Datehour_Arrival])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

@amitchandak 

 

I fiddled around a bit and ended up with the values I wanted, great, thanks a lot!

😉😊

Anonymous
Not applicable

It doesn't give me the desired values, isn't there a way to have a code similar to my original code by removing "earlier"?

Anonymous
Not applicable

I just passed but I don't get the right values...

An example of the application I need:

Deck 2 datetime departure: 20/07/22 01:06:23 dtatetime arrival: 20/07/22 01:07:23
Deck 3 datetime departure: 20/07/22 03:00:26 datetime arrival: 20/07/22 03:07:26
Deck 1 start time: 20/07/22 02:00:00 finish time: 20/07/22 02:04:05
Deck 2 datetime departure : 20/07/22 03:08:00 dtatetime arrival : 20/07/22 03:09:55

The code must take into account the bridge number, the departure time after 03:08:00 minus the arrival time before 01:07:23 = 2:00:37

amitchandak
Super User
Super User

@Anonymous , Create two columns

 

Date Rank = rankx(Filter('bridge Hall 3 M' , [Number_bridge] = EARLIER('bridge Hall 3 M'[Number_Pont]) ), 'bridge Hall 3 M'[DateHour_Origin],,asc, dense)

 

Col2 =

CALCULATE(MIN('Bridge Hall 3 M'[DateHour_Origin]),FILTER('bridge Hall 3 M','bridge Hall 3 M'[Number_bridge]=EARLIER('bridge Hall 3 M'[Number_Pont])
&&'bridge Hall 3 M'[Date Rank] = EARLIER('bridge Hall 3 M'[Date Rank] +1 )))-'bridge Hall 3 M'[Datehour_Arrival])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

Hi @amitchandak,

 

I get an error message "the first argument of earlier is not a valid column reference in the previous row context." for EARLIER('bridge Hall 3 M'[Date Rank] +1 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

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.