Forum Discussion

1320Data's avatar
1320Data
Frequent Visitor
3 years ago
Solved

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.

 

 

  • 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"

     

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

2 Replies

  • 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"

     

  • 1320Data's avatar
    1320Data
    Frequent Visitor

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