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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
dading
Helper II
Helper II

Merge status and cycle

Hi everyone,

i'm having trouble merging 2 tables base on id and timestamp

could you please help to solve it using power query?

 

i attach the file in link below. the result i want is in sheet result

 

link file 

 

thank you

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1665664911823.png

let
    status = Excel.CurrentWorkbook(){[Name="status"]}[Content],
    cycle = Excel.CurrentWorkbook(){[Name="cycle"]}[Content],
    Custom1 = let
                 cyclegrp=Table.Group(cycle,"Truck",{"n",each Table.Sort(_,"Timefull")})
              in
                 #table(
                        Table.ColumnNames(status)&{"Remark"},
                        List.TransformMany(
                                           Table.ToRows(status),
                                           each let
                                                   a=cyclegrp{[Truck=_{0}]}?[n]?,
                                                   b=Table.FirstN(a,(x)=>x[Timefull]<=_{1}),
                                                   c=Table.FirstN(Table.Skip(a,Table.RowCount(b)),(x)=>x[Timefull]<_{2}),
                                                   d={List.Max({List.Last(b[Timefull],null),_{1}})}&c[Timefull]&{_{2}},
                                                   e={List.Last(b[Remark],null)}&c[Remark]
                                                in
                                                   if a=null then {{_{1},_{2},null}}
                                                   else List.Zip({List.RemoveLastN(d),List.Skip(d),e}),
                                           (x,y)=>{x{0},y{0},y{1},Duration.TotalHours(y{1}-y{0}),x{4},y{2}}
                                          )
                       )
in
    Custom1

 

View solution in original post

8 REPLIES 8
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1665664911823.png

let
    status = Excel.CurrentWorkbook(){[Name="status"]}[Content],
    cycle = Excel.CurrentWorkbook(){[Name="cycle"]}[Content],
    Custom1 = let
                 cyclegrp=Table.Group(cycle,"Truck",{"n",each Table.Sort(_,"Timefull")})
              in
                 #table(
                        Table.ColumnNames(status)&{"Remark"},
                        List.TransformMany(
                                           Table.ToRows(status),
                                           each let
                                                   a=cyclegrp{[Truck=_{0}]}?[n]?,
                                                   b=Table.FirstN(a,(x)=>x[Timefull]<=_{1}),
                                                   c=Table.FirstN(Table.Skip(a,Table.RowCount(b)),(x)=>x[Timefull]<_{2}),
                                                   d={List.Max({List.Last(b[Timefull],null),_{1}})}&c[Timefull]&{_{2}},
                                                   e={List.Last(b[Remark],null)}&c[Remark]
                                                in
                                                   if a=null then {{_{1},_{2},null}}
                                                   else List.Zip({List.RemoveLastN(d),List.Skip(d),e}),
                                           (x,y)=>{x{0},y{0},y{1},Duration.TotalHours(y{1}-y{0}),x{4},y{2}}
                                          )
                       )
in
    Custom1

 

The result is exactly what I wanted. 

i also have add more rows data and it works

 

thank you.

ImkeF
Super User
Super User

Hi @dading ,
given the lack of clarification about what the transformations should achieve and taking into account my experiences with sparse data like this ("Oh, I forgot to include this and that") I decided not to follow this up.

So hopefully someone else will jump in here.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

ok , Sorry for the inconvenience

 

thank you.

ImkeF
Super User
Super User

Hi @dading ,
I must admit that I am a bit surprised that you didn't give any explanation to this transformation, as it doesn't look trivial to me.

Please consider explaining what you are actually trying to achieve here.
Specifically I have the following question: In your sample data there is always only one entry from table "cycle" for each row of the table "status" at max. Will this always be the case or could there be more than one row from table "cycle" for a single row of table "status"?

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

yes you are correct, cycle could be more than one row.

i have add a cycle to make it clear

ImkeF
Super User
Super User

Hi @dading ,
getting this error-message when trying to download the data:

ImkeF_0-1665636344817.png

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

please re-try

link 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Solution Authors
Top Kudoed Authors