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
1320Data
Frequent Visitor

Generate Desired Table based on 2 tables

Hi all,

 

@amitchandak  Could you please help me?!

 

I have 2 tables in my desktop(see below) and I want to generate desired table based on the 2 tables I have in Power BI.

 

DateEmpIdHours
11/11/2021100001168
3/1/2022100001145
5/5/2002100001160
1/1/2022100002168

 

EmpIdNameHours
100001A160
100002B168
100003C164

 

Desired Output table should look like below:

DateEmpIDEmpNameContracthours
1/1/2022100001Employee A168
1/2/2022100001Employee A168
1/3/2022100001Employee A168
1/4/2022100001Employee A168
1/5/2022100001Employee A168
1/6/2022100001Employee A168
1/7/2022100001Employee A168
1/8/2022100001Employee A168
1/9/2022100001Employee A168
1/10/2022100001Employee A

168

 

Till 2/28/202210001A

168

3/1/202210001A

145

(Till 5/4/2022)

 

Any suggestion on this?

 

Thanks. Have a nice day.

 

 

2 ACCEPTED SOLUTIONS
MFelix
Super User
Super User

Hi @1320Data ,

 

Try the following code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTUByIjAyNDJR0lQwMgADPMLJRidaKVjPXBckYociamYDlTfVNMOTMDsJwhmj4jmJmxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, EmpId = _t, Hours = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"EmpId", Int64.Type}, {"Hours", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"EmpId", Order.Ascending}, {"Date", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each try if [EmpId] = #"Added Index" [EmpId] {[Index] + 1} then  #"Added Index" [Date] {[Index] + 1} else null otherwise null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each try {0.. Duration.Days([Custom] - [Date] ) } otherwise null),
    #"Expanded Custom.1" = Table.ExpandListColumn(#"Added Custom1", "Custom.1"),
    #"Added Custom2" = Table.AddColumn(#"Expanded Custom.1", "Custom.2", each try [Date] + #duration([Custom.1],0,0,0) otherwise [Date]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Index", "Custom", "Custom.1", "Date"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom.2", "Date"}}),
    #"Sorted Rows1" = Table.Sort(#"Renamed Columns",{{"Date", Order.Ascending}})
in
    #"Sorted Rows1"

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

1320Data
Frequent Visitor

Thank you. I did the same by creating index column and self join the table. It worked.

View solution in original post

2 REPLIES 2
1320Data
Frequent Visitor

Thank you. I did the same by creating index column and self join the table. It worked.

MFelix
Super User
Super User

Hi @1320Data ,

 

Try the following code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTUByIjAyNDJR0lQwMgADPMLJRidaKVjPXBckYociamYDlTfVNMOTMDsJwhmj4jmJmxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, EmpId = _t, Hours = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"EmpId", Int64.Type}, {"Hours", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"EmpId", Order.Ascending}, {"Date", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each try if [EmpId] = #"Added Index" [EmpId] {[Index] + 1} then  #"Added Index" [Date] {[Index] + 1} else null otherwise null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each try {0.. Duration.Days([Custom] - [Date] ) } otherwise null),
    #"Expanded Custom.1" = Table.ExpandListColumn(#"Added Custom1", "Custom.1"),
    #"Added Custom2" = Table.AddColumn(#"Expanded Custom.1", "Custom.2", each try [Date] + #duration([Custom.1],0,0,0) otherwise [Date]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Index", "Custom", "Custom.1", "Date"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom.2", "Date"}}),
    #"Sorted Rows1" = Table.Sort(#"Renamed Columns",{{"Date", Order.Ascending}})
in
    #"Sorted Rows1"

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



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.

Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.