Forum Discussion

Serdet's avatar
Serdet
Copper Contributor
May 17, 2023
Solved

Export data transformation

Hello,

 

I have a system that exports data in a poor fashion and I need help transforming this into readable data.

 

Standard export

DateNameTotal HoursA HoursB HoursC HoursD Hours
01/01/2023John102.52.52.52.5
01/01/2023Maggie205555

 

Desired output

DateNameHours
01/01/2023John2.5
01/01/2023John2.5
01/01/2023John2.5
01/01/2023John2.5
01/01/2023Maggie5
01/01/2023Maggie5
01/01/2023Maggie5
01/01/2023Maggie5

 

For every A - D hours that has value greater than 0, it would need to create a new row and show this as hours for each available category.

 

Things to note - there will only ever be A - D hours, some hours in A - D will be 0/blank 

 

Is this possible to solve? Ideally without a macro where the source table is pasted and another sheet in the same workbook shows the desired output 

  • Hi Serdet 

     

    What HansVogelaar suggested gives the following. However, I'm not quite clear re. what you mean with For every A - D hours that has value greater than 0, it would need to create a new row and show this as hours for each available category

     

     

    Does this reflect what you expect? If not what's wrong/should be changed?

4 Replies

  • Serdet 

    It is possible to write a formula to unpivot using Excel 365 but it would be better to transform the data as part of the ETL process using Power Query.

    = LET(
        attributes, Table1[[Date]:[Name]],
        headers,    Table1[[#Headers],[Date]:[Name]],
        crosstab,   Table1[[A Hours]:[D Hours]],
        rowNumber,  SEQUENCE(1,ROWS(attributes)),
        repeats,    SEQUENCE(1,COLUMNS(crosstab),1,0),
        valueField, TOCOL(crosstab),
        attrFields, REDUCE(headers, rowNumber,
          LAMBDA(acc,k, VSTACK(acc, CHOOSEROWS(attributes, k*repeats)))
        ),
        unpivotted, HSTACK(DROP(attrFields,1), valueField),
        FILTER(unpivotted, valueField<>"")
      )

    Unpivoting the value fields is the easy bit.  Soring the repeats of the attribute fields is more of a mess.

    p.s.  I have just noticed I have 3 other files named unpivot so it is probably time to get a definitive treatment!

  • Lorenzo's avatar
    Lorenzo
    Silver Contributor

    Hi Serdet 

     

    What HansVogelaar suggested gives the following. However, I'm not quite clear re. what you mean with For every A - D hours that has value greater than 0, it would need to create a new row and show this as hours for each available category

     

     

    Does this reflect what you expect? If not what's wrong/should be changed?

    • Serdet's avatar
      Serdet
      Copper Contributor
      Unpivot works perfect. Thank you guys!

Resources