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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
amalm
Helper III
Helper III

Merging Queries based on time of entries

Hi, 

 

I have 2 tables whose important columns I have listed below:

 

Table 1 (data captured at the beginning of the workday):

  • Date
  • Starting Time
  • Username
  • Vehicle Number
  • Starting kilometer reading 

Table 2 (data captured at the end of the workday):

  • Date 
  • Ending Time
  • Username
  • Ending kilometer reading

 

My requirement:

I need to merge both these tables so that I have a starting and ending kilometer reading for each vehicle for a particular day, in the same report. 

 

My current approach:

I made a custom column in the query editor concatenating the date & username, so that I have a unique value for each day per user, and used Merge Inner.

 

The problem:

It was working fine until now, but there are now certain users who do not work for me the entire day. They start in the morning, end after 3 hours, then start again in the evening, end after another 2 hours etc. I need to calculate their exact hours and kilometers, among other things. The tables now have to be merged with the entries matching in the correct time sequence:

 

In short I need to do this:

Screenshot_49.png

 

Any help would be much appreciated, thanks.

5 REPLIES 5
v-frfei-msft
Community Support
Community Support

Hi @amalm,

 

We can merge queries as a new one in PQ based on Vehicle Number or some other column. Afther that, to remove the duplicates to achieve our goal. Here is the M code for your reference.

 

let
    Source = Table.NestedJoin(Table1,{"Username", "Vehicle No"},Table2,{"Username", "Vehicle No"},"Table2",JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"Ending Time", "Ending kms"}, {"Table2.Ending Time", "Table2.Ending kms"}),
    #"Removed Duplicates" = Table.Distinct(#"Expanded Table2", {"Table2.Ending Time"})
in
    #"Removed Duplicates"

Capture.PNG

 

Please check the pbix as attached for more details.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

@v-frfei-msft

Thanks for the reply. However, it does not work. The "remove duplicates" step removes the wrong duplicate in the second entry. 

(It should show 2:30PM to 3:30PM)

 

 

Table1:

 

 

Table2:

 

Merged Tables:

 

 

Bump... Any help? Smiley Sad

@MarcelBeug

I saw your solution to a similar problem in another thread (https://community.powerbi.com/t5/Desktop/time-difference-between-2-rows/td-p/226755); can you please help me with mine? Thanks.

Greg_Deckler
Community Champion
Community Champion

Hmmm, maybe @ImkeF has an idea here.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 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.