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.   Date EmpId Hou...
  • MFelix's avatar
    3 years ago

    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
    3 years ago

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