Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Two rows in different levels in one line Power Query

There ia a table with a lot of id. And 2 form where entered date. 

Ned to copy date in one line (according to "subject" and "visit") and date_2 closest to date, because "visit" may have similar name 😞

 

 

Please find attached

 

result

 

 

id_protocolid_sitenameid_date_formvisitdate_1date_2
11212-0011visit 110.01.2024 
11212-0012visit 1 13.01.2024
11212-0021visit 115.05.2024 
11313-0011visit 311.03.2024 
11313-0012visit 3 12.03.2024
11313-0021visit 114.09.2023 
34040-0013visit 503.04.2023 
34040-0014visit 5 06.04.2023
34040-0023visit 101.04.2024 
34040-0024visit 1 04.04.2024
33333-0013visit 411.03.2023 
33333-0014visit 4 12.03.2023
33333-0023visit 1 14.05.2023
  • Hi Anonymous ,

     

    Try out this example code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZFRDsMgCIavYnxuDQIu2VmaHqDPW3r+IVUzgk0fJKB8/IDbFnNcYkY1K4BGcs7jc3yDRpAgJwRkCULclxmCBgn1iQbmEfQqJUGZqJAa15he5wT0hKBBtDEcmEcmjXGCd02noVI9BjVNhQZSxJPywE8IG6Q2Bq+BeQSNSi0g273S+UYFjUr/F2E61hG6jJuF/5dsZ7EIG8QsmSaInyW0RZeG7D8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id_protocol = _t, id_site = _t, name = _t, id_date_form = _t, visit = _t, date_1 = _t, date_2 = _t]),
        repBlankNull = Table.ReplaceValue(Source," ",null,Replacer.ReplaceValue,{"date_1", "date_2"}),
    
    // Relevant steps from here =====>
        groupNameAllRows = Table.Group(repBlankNull, {"name"}, {{"data", each _, type table [id_protocol=nullable text, id_site=nullable text, name=nullable text, id_date_form=nullable text, visit=nullable text, date_1=nullable text, date_2=nullable text]}}),
        fillUpNestedName = Table.TransformColumns(groupNameAllRows, {"data", each Table.FillUp(_, {"date_2"})}),
        expandNestedData = Table.ExpandTableColumn(fillUpNestedName, "data", {"id_protocol", "id_site", "id_date_form", "visit", "date_1", "date_2"}, {"id_protocol", "id_site", "id_date_form", "visit", "date_1", "date_2"})
        
    in
        expandNestedData

     

    Summary:

    groupNameAllRows = Group the table by [name] using the All Rows operator get nested tables.

    fillUpNestedName = Apply the Fill Up transformation to the [name] column to each nested table.

    expandNestedData = Reinstate the nested columns back to the table.

     

    Example output:

     

    Pete

1 Reply

  • Hi Anonymous ,

     

    Try out this example code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZFRDsMgCIavYnxuDQIu2VmaHqDPW3r+IVUzgk0fJKB8/IDbFnNcYkY1K4BGcs7jc3yDRpAgJwRkCULclxmCBgn1iQbmEfQqJUGZqJAa15he5wT0hKBBtDEcmEcmjXGCd02noVI9BjVNhQZSxJPywE8IG6Q2Bq+BeQSNSi0g273S+UYFjUr/F2E61hG6jJuF/5dsZ7EIG8QsmSaInyW0RZeG7D8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id_protocol = _t, id_site = _t, name = _t, id_date_form = _t, visit = _t, date_1 = _t, date_2 = _t]),
        repBlankNull = Table.ReplaceValue(Source," ",null,Replacer.ReplaceValue,{"date_1", "date_2"}),
    
    // Relevant steps from here =====>
        groupNameAllRows = Table.Group(repBlankNull, {"name"}, {{"data", each _, type table [id_protocol=nullable text, id_site=nullable text, name=nullable text, id_date_form=nullable text, visit=nullable text, date_1=nullable text, date_2=nullable text]}}),
        fillUpNestedName = Table.TransformColumns(groupNameAllRows, {"data", each Table.FillUp(_, {"date_2"})}),
        expandNestedData = Table.ExpandTableColumn(fillUpNestedName, "data", {"id_protocol", "id_site", "id_date_form", "visit", "date_1", "date_2"}, {"id_protocol", "id_site", "id_date_form", "visit", "date_1", "date_2"})
        
    in
        expandNestedData

     

    Summary:

    groupNameAllRows = Group the table by [name] using the All Rows operator get nested tables.

    fillUpNestedName = Apply the Fill Up transformation to the [name] column to each nested table.

    expandNestedData = Reinstate the nested columns back to the table.

     

    Example output:

     

    Pete