Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
Hei,
I need some help with this table. For each Trip, I need to define its start and end time by:
Lets take a unique Trip number and when the second column is Loading, take the Depart time as the start of a trip and write it into a new column, when the second column is Discharing, take the Arrive time as the end of a trip and write it into a new column. If there are several rows of Loading for one Trip number, take the first of all Depart time and If there are several rows of Discharing for one Trip number, take the last of all Arrive time. I wish to have a table with only three columns, Trip number, Start time and end time of this trip. Can someone help me with this? Thanks in advance.
Trip number | Reason for call | Depart | Arrive |
01901 | Loading | 1/23/2019 13:00 | 1/20/2019 11:10 |
01901 | Discharging | 2/10/2019 13:30 | 2/5/2019 0:01 |
01902 | Loading | ||
01902 | Loading | 5/4/2019 1:00 | 4/23/2019 16:00 |
01902 | Discharging | 5/15/2019 16:48 | 5/13/2019 8:36 |
01903 | Loading | 6/9/2019 8:06 | 6/5/2019 11:30 |
01903 | Discharging | ||
01903 | Discharging | 8/3/2019 8:34 | 8/2/2019 2:12 |
01904 | Loading | 8/22/2019 22:30 | 8/19/2019 3:36 |
01904 | Loading | 8/14/2019 16:35 | 8/12/2019 7:58 |
01904 | Loading | ||
01904 | Discharging | 10/7/2019 16:48 | 9/27/2019 21:00 |
01904 | Discharging | ||
01905 | Loading | ||
01905 | Loading | 12/20/2019 11:00 | 12/16/2019 17:30 |
01905 | Discharging | 1/4/2020 9:00 | 1/2/2020 1:00 |
02001 | Loading | 1/12/2020 12:24 | 1/9/2020 19:30 |
02001 | Loading | 1/19/2020 21:00 | 1/18/2020 10:10 |
02001 | Discharging | 2/5/2020 17:00 | 2/1/2020 16:30 |
02001 | Discharging | 2/10/2020 12:12 | 2/6/2020 9:30 |
02001 | Discharging | 3/1/2020 3:00 | 2/11/2020 19:30 |
01901 | Loading | 8/23/2019 7:30 | 8/18/2019 18:06 |
01901 | Discharging | 9/8/2019 17:30 | 9/6/2019 8:06 |
01902 | Loading | 9/14/2019 12:48 | 9/8/2019 17:30 |
01902 | Discharging | 10/15/2019 9:12 | 10/4/2019 16:48 |
01903 | Loading | ||
01903 | Loading | 10/27/2019 1:30 | 10/21/2019 12:00 |
01903 | Discharging | 11/9/2019 21:42 | 11/5/2019 21:30 |
01904 | Loading | 12/4/2019 19:30 | 11/29/2019 7:30 |
01904 | Discharging | 12/19/2019 7:00 | 12/14/2019 16:00 |
01905 | Loading | 1/5/2020 20:28 | 1/2/2020 10:00 |
01905 | Discharging | ||
01905 | Loading | 1/10/2020 10:00 | 1/6/2020 20:00 |
01905 | Discharging | 2/25/2020 13:48 | 2/9/2020 21:00 |
01905 | Discharging | 3/4/2020 16:00 | 2/27/2020 14:42 |
Solved! Go to Solution.
Alright, give this a try:
I begin with this table:
Create a duplicate of this table, now we have Table 1 & Table 2.
In Table 1, filter reason for call on "Loading":
In Table 1, highlight [Trip number] and [Unit Number] then press "Group By". Use the same setting as the picture below:
Now we move onto Table 2. Filter the table on discharging then do anoth "Group By" with these settings:
Finally we want to merge the two tables. Press "Merge as New" then use the following settings (Ctrl click to highlight more columns):
Finally expand the "End" column:
Should give you the following result, which i hope is correct:
Br,
J
@Anonymous
Try a new table like
summarize(table, table[Trip number],"Depart",firstnonblank(Table[Depart],blank()),"Arrive",lastnonblank(Table[Arrive],blank()))
or
summarize(table, table[Trip number],"Depart",min(Table[Depart]),"Arrive",max(Table[Arrive]))
Is this what you're looking for?
If that's the case i used these two calculated columns:
Start = CALCULATE( MIN('Table (2)'[Depart]) ; ALL('Table (2)') ; 'Table (2)'[Reason for call] = "Loading" ; 'Table (2)'[Trip number] = EARLIER('Table (2)'[Trip number]))
End = CALCULATE( MAX('Table (2)'[Arrive]) ; ALL('Table (2)') ; 'Table (2)'[Reason for call] = "Discharging" ; 'Table (2)'[Trip number] = EARLIER('Table (2)'[Trip number]))
Br,
J
Hei, yes, this is almost what i want. I just need one row for a unique Trip number and remove the three columns in the middle...
However, you syntax does not work on my pc. I changed my table name to Table (2), as I guess that is your table name and in Data - > Modelling -> Add a new column and put in your line into the formula area... but i got error message... is it becuase my dates have hierachy? but I cant remove it...
@tex628 , Please help. @Anonymous , share error.
Hi,
Now I am able to make the two extra colomns Start and End in the Data tab. However, since I need to remove the three columns in the middle and duplicated rows, I need to go to the Data Query and do that. But once in Data Query, the two new calculated columns are not there. Why and what I can do?
The reason that the two columns are not present is that they are calculated columns, they depend on the datamodel as it is when it's loaded and they cant actually exist before the load!
What we want to do instead is write a table statement, i'd say we want to do something like this:
New Table = SUMMARIZE( 'Table (2)' ; [Trip Number] ; "Start" ; MIN([Start]) ; "End" ; MAX([End]))
Let me know how it goes!
Br,
J
and this summerise table does not consider that "Reason for call" variable... while you New column method does... so I am not sure this summerise table gives the same results...
The summarized table uses the calculated columns which both consider the reason for call column, so the result should be correct if I'm understanding this correctly..
If you want to have these columns in power query you also need to make them there. But i can tell you for a fact that it's alot more complicated to do the same transformation there.
Could you take a picture and show me where the summarized table is producing incorrect values? If possible show both the initial data and the sumamrized result for a specific trip number that you have identified as incorrect.
Br,
J
Hei,
I see what you mean... you did the job in 2 steps... when the second table is based on the calculated columns in the first table, it takes "reason to call" into consideration. Yes, I just eyeballed the numbers and did not find anything wrong... i was thinking i only gave out a small sample and there might be wrongs in the full data if your syntax did not consider "reason for call"... but now I see you have considered it!
yes, I thought about doing it in query editor, but seems mission impossible for me... so what is the difference between doing data washing in Data view and in Query Editor? Do you have a general summary? Thanks again!
From what I've been told it's preferable to do all table transformations in Power Query from a performance standpoint. There are also some differences in what you can achieve since it's two different programming languages.
Generally, if i can create a column in power query i always do that before creating it in data view.
On another note, upon thinking about it a little more i actually believe there is a quite simple way to do this in Power Query. If you want we could go through that method aswell 🙂
Br,
J
Hi, Sorry again. I think i really need to do it in Query now... I made this and lots of other variables in the Data view and they don't come along into the query view where I need to do some further data washing...
Plus, I need to add a column Unit_number. each Unit_number might have several Trip numbers, but each Trip number can only have one unique Unit_number, I just want this Unit_number to be carried into the final table as a column. is that possible?
Alright!
When you say you need to add unit number, what exactly do you mean? Is the column already present in the original table?
Br,
J
Alright, give this a try:
I begin with this table:
Create a duplicate of this table, now we have Table 1 & Table 2.
In Table 1, filter reason for call on "Loading":
In Table 1, highlight [Trip number] and [Unit Number] then press "Group By". Use the same setting as the picture below:
Now we move onto Table 2. Filter the table on discharging then do anoth "Group By" with these settings:
Finally we want to merge the two tables. Press "Merge as New" then use the following settings (Ctrl click to highlight more columns):
Finally expand the "End" column:
Should give you the following result, which i hope is correct:
Br,
J
This works! Such a smart solution! Thank you!
The only problem left is, when I click Accept as solution, I got error message... 😄
No, it is not. It is an extra column...
I dont know why I can not paste table now
Yes, i prefer doing all data washing in Query editor rather than doing it partly here and partly there...
Yes, I am interested! But please only do it if you have time...
Hi, Sorry I dont quite understand what you meant, i just want these calculated columns appear in Query Editor... What can I do to achieve that?
But I did make the table you recommended... However, it, also, does not show up in Query Editor...
What should I do if I want to be able to do some editing in Query Editor?
dont worry. let me try it first... i think i have a clue... the computer setting, yours is ; and mine is ,
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
104 | |
69 | |
49 | |
41 | |
34 |
User | Count |
---|---|
164 | |
111 | |
62 | |
53 | |
38 |