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

Don'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.

Reply
Anonymous
Not applicable

Modify table tricks needed

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 numberReason for callDepartArrive
01901Loading1/23/2019 13:001/20/2019 11:10
01901Discharging2/10/2019 13:302/5/2019 0:01
01902Loading  
01902Loading5/4/2019 1:004/23/2019 16:00
01902Discharging5/15/2019 16:485/13/2019 8:36
01903Loading6/9/2019 8:066/5/2019 11:30
01903Discharging  
01903Discharging8/3/2019 8:348/2/2019 2:12
01904Loading8/22/2019 22:308/19/2019 3:36
01904Loading8/14/2019 16:358/12/2019 7:58
01904Loading  
01904Discharging10/7/2019 16:489/27/2019 21:00
01904Discharging  
01905Loading  
01905Loading12/20/2019 11:0012/16/2019 17:30
01905Discharging1/4/2020 9:001/2/2020 1:00
02001Loading1/12/2020 12:241/9/2020 19:30
02001Loading1/19/2020 21:001/18/2020 10:10
02001Discharging2/5/2020 17:002/1/2020 16:30
02001Discharging2/10/2020 12:122/6/2020 9:30
02001Discharging3/1/2020 3:002/11/2020 19:30
01901Loading8/23/2019 7:308/18/2019 18:06
01901Discharging9/8/2019 17:309/6/2019 8:06
01902Loading9/14/2019 12:489/8/2019 17:30
01902Discharging10/15/2019 9:1210/4/2019 16:48
01903Loading  
01903Loading10/27/2019 1:3010/21/2019 12:00
01903Discharging11/9/2019 21:4211/5/2019 21:30
01904Loading12/4/2019 19:3011/29/2019 7:30
01904Discharging12/19/2019 7:0012/14/2019 16:00
01905Loading1/5/2020 20:281/2/2020 10:00
01905Discharging  
01905Loading1/10/2020 10:001/6/2020 20:00
01905Discharging2/25/2020 13:482/9/2020 21:00
01905Discharging3/4/2020 16:002/27/2020 14:42

 

2 ACCEPTED SOLUTIONS
tex628
Community Champion
Community Champion

Alright, give this a try:

I begin with this table:
image.png

Create a duplicate of this table, now we have Table 1 & Table 2. 
image.png

In Table 1, filter reason for call on "Loading":
image.png

In Table 1, highlight [Trip number] and [Unit Number] then press "Group By". Use the same setting as the picture below:
image.png

Now we move onto Table 2. Filter the table on discharging then do anoth "Group By" with these settings:
image.png

Finally we want to merge the two tables. Press "Merge as New" then use the following settings (Ctrl click to highlight more columns):
image.png

Finally expand the "End" column:
image.png

Should give you the following result, which i hope is correct:
image.png

Br,
J


 


Connect on LinkedIn

View solution in original post

tex628
Community Champion
Community Champion

Happy to hear! I can actually help you out with that 😉

Br,
J

 


Connect on LinkedIn

View solution in original post

21 REPLIES 21
amitchandak
Super User
Super User

@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]))

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
tex628
Community Champion
Community Champion

Is this what you're looking for? 

image.png

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





Connect on LinkedIn
Anonymous
Not applicable

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.

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Anonymous
Not applicable

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?

tex628
Community Champion
Community Champion

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]))

image.png 

Let me know how it goes!

Br,
J


Connect on LinkedIn
Anonymous
Not applicable

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... 

tex628
Community Champion
Community Champion

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


Connect on LinkedIn
Anonymous
Not applicable

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!

tex628
Community Champion
Community Champion

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


Connect on LinkedIn
Anonymous
Not applicable

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?

 

tex628
Community Champion
Community Champion

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

 


Connect on LinkedIn
Anonymous
Not applicable

Capture.PNG

 

tex628
Community Champion
Community Champion

Alright, give this a try:

I begin with this table:
image.png

Create a duplicate of this table, now we have Table 1 & Table 2. 
image.png

In Table 1, filter reason for call on "Loading":
image.png

In Table 1, highlight [Trip number] and [Unit Number] then press "Group By". Use the same setting as the picture below:
image.png

Now we move onto Table 2. Filter the table on discharging then do anoth "Group By" with these settings:
image.png

Finally we want to merge the two tables. Press "Merge as New" then use the following settings (Ctrl click to highlight more columns):
image.png

Finally expand the "End" column:
image.png

Should give you the following result, which i hope is correct:
image.png

Br,
J


 


Connect on LinkedIn
Anonymous
Not applicable

This works! Such a smart solution! Thank you!

 

The only problem left is, when I click Accept as solution, I got error message... 😄

 

tex628
Community Champion
Community Champion

Happy to hear! I can actually help you out with that 😉

Br,
J

 


Connect on LinkedIn
Anonymous
Not applicable

No, it is not. It is an extra column...

I dont know why I can not paste table now 

 

 

Anonymous
Not applicable

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...

Anonymous
Not applicable

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?

Anonymous
Not applicable

dont worry. let me try it first... i think i have a clue... the computer setting, yours is ; and mine is , 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.