Forum Discussion

AndrewPF's avatar
AndrewPF
Helper V
4 months ago
Solved

split, process and recombine data

I have some [anonymised] data - 10 rows in total - which looks like this: 

 

Employee IDFull NameEffective dateDepartmentPrev Row DepartmentPrev Row Effective datePrev Row End effective date
01John Smith30/06/2026N HouseH House13/03/202629/06/2026
02Jane Smith30/04/2026L HouseK Moore13/03/202629/04/2026
03Jessica Smith 31/03/2026M ViewB Cottage13/03/202630/03/2026
04Johnny Smith31/05/2026P ViewThe Parsley13/03/202630/05/2026
05Jezabel Smith31/05/2026A HouseE House13/03/202630/05/2026
06Jim Smith31/05/2026M ViewP Fold13/03/202630/05/2026
07Jake Smith30/04/2026A HouseR House13/03/202629/04/2026
08Jenny Smith31/05/2026D ViewP Fold14/03/202630/05/2026
09Jennifer Smith30/04/2026B FarmH House13/03/202629/04/2026
10Jackie Smith30/06/2026C HouseS View13/03/202629/06/2026

 

representing staff moves from one location to another. 

 

The "Department" therefore represents "Loaned To", and the "Prev Row Department" represents "Borrowed From". 

 

I need my final dataset to be 20 rows and look something like this: 

 

DepartmentFTE %
N House50
L House100
M View100
P View80
A House100
M View100
A House66
D View100
B Farm100
C House100
H House-50
K Moore-100
B Cottage-100
The Parsley-80
E House-100
P Fold-100
R House-66
P Fold-100
H House-100
S View-100

 

which I then need to summarise at home level (but that bit's easy). 

 

What's the best way of transposing the data? 

  • Your dataset is silent on the actual FTE values so I will make an assumption that the FTE values can be related to an employee via Employee ID and are available in an external table. 

    With that assumption in place, you can get your desired result by selecting the 'Department', 'Prev Row Department', and 'Employee ID' columns and then unpivoting based on 'Department' and 'Prev Row Department'.

    From there you can merge in the FTE values from the external table and then turn the 'Prev Row Department' rows negative.

     

    Here is a sample code, 

    let
        Source = 
        Table.FromRows(
            Json.Document(
                Binary.Decompress(
                    Binary.FromText(
                        "hdJdb4IwFAbgv3LCtQmFgs5LP2aMHwuZizfGi6rH0Qg0Aczifv1aZmudxd215PDkPT1ns/FI4HW8mUgLWOW8TuWFEp90/ZCEXXl5g6k4VyhPU3MKqE+oLgj7pnrbkVyoOFbgHRfp6oVB5rAUonRykcVRxWFV8T37FUGRgfXHEtYcv+RhCCNR1+zzwVQJqGVG146Lyy2kFGNdn2jxI0VIWFlleHGZsWXGTc5vtsPMjQ5M56/uh/wrqm8znrs103QCE5Ed/rN6zVBOLUO5RXtvn7E9lJem2db3Gz+Gi56E6181fsTSHXAIE1bmz3fQyheQpt39iaN7qUeGWemorTu9/QE=", 
                        BinaryEncoding.Base64
                    ), 
                    Compression.Deflate
                )
            ), 
            let 
                _t = ((type nullable text) meta [Serialized.Text = true]) 
            in 
                type table [#"Employee ID" = _t, #"Full Name" = _t, #"Effective date" = _t, Department = _t, #"Prev Row Department" = _t, #"Prev Row Effective date" = _t, #"Prev Row End effective date" = _t]
        ),
        initial_type_set = 
        Table.TransformColumnTypes(
            Source,
            {
                {"Employee ID", Int64.Type}, {"Full Name", type text}, {"Effective date", type text}, {"Department", type text}, {"Prev Row Department", type text}, {"Prev Row Effective date", type text}, {"Prev Row End effective date", type text}
            }
        ),
        unpivot_selected = 
        Table.Unpivot(
            Table.SelectColumns(
                initial_type_set, 
                {"Department", "Prev Row Department", "Employee ID"}
            ), 
            {"Department", "Prev Row Department"}, 
            "Attribute", 
            "Value"
        ),
        merge_fte = 
        Table.NestedJoin(
            unpivot_selected, 
            {"Employee ID"}, 
            fteTable, 
            {"Employee ID"}, 
            "fteTable", 
            JoinKind.LeftOuter
        ),
        expand_fte = 
        Table.ExpandTableColumn(
            merge_fte, 
            "fteTable", 
            {"FTE"}, 
            {"FTE"}
        ),
        set_negatives = 
        Table.ReplaceValue(
            expand_fte, 
            each [FTE], 
            each 
                if [Attribute] = "Prev Row Department" 
                    then -[FTE] 
                    else [FTE], 
            Replacer.ReplaceValue, 
            {"FTE"}
        ),
        remove_columns = 
        Table.RemoveColumns(
            set_negatives,
            {"Employee ID", "Attribute"}
        ),
        final_type_set = 
        Table.TransformColumnTypes(
            remove_columns,
            {
                {"FTE", type number}
            }
        ),
        group_rows = 
        Table.Group(
            final_type_set, 
            {"Value"}, 
            {
                {"FTE", each List.Sum([FTE]), type nullable number}
            }
        ),
        rename_value = 
        Table.RenameColumns(
            group_rows,
            {
                {"Value", "Department"}
            }
        )
    in
        rename_value

     

    I used the following code to create FTE values based on the example data you provided.

    let
        Source = 
        #table(
            type table [#"Employee ID"=nullable number, FTE=nullable number],
            {
                {1,.5},
                {2,1},
                {3,1},
                {4,.8},
                {5,1},
                {6,1},
                {7,.66},
                {8,1},
                {9,1},
                {10,1}
            }
        )
    in
        Source

     

    To end up with the result. (Note: I grouped by 'Department', summing the FTE.)

     

6 Replies

  • Hi! AndrewPF 

     

    You might have to share your logic around it? How are you coming to the expected numbers so I can have a look at it.

     

     

  • When a member of staff (temporarily) moves from place A to place B, that represents "loaned to", or minus, from place A and a "borrowed from", or plus, in place B.  Therefore, if that staff member is on a part-time contract, and has an FTE of 80% of standard then, in the summary, place A would be 0.80 (80%) down, and place B would be 0.80 (80%) up. 

  • Your dataset is silent on the actual FTE values so I will make an assumption that the FTE values can be related to an employee via Employee ID and are available in an external table. 

    With that assumption in place, you can get your desired result by selecting the 'Department', 'Prev Row Department', and 'Employee ID' columns and then unpivoting based on 'Department' and 'Prev Row Department'.

    From there you can merge in the FTE values from the external table and then turn the 'Prev Row Department' rows negative.

     

    Here is a sample code, 

    let
        Source = 
        Table.FromRows(
            Json.Document(
                Binary.Decompress(
                    Binary.FromText(
                        "hdJdb4IwFAbgv3LCtQmFgs5LP2aMHwuZizfGi6rH0Qg0Aczifv1aZmudxd215PDkPT1ns/FI4HW8mUgLWOW8TuWFEp90/ZCEXXl5g6k4VyhPU3MKqE+oLgj7pnrbkVyoOFbgHRfp6oVB5rAUonRykcVRxWFV8T37FUGRgfXHEtYcv+RhCCNR1+zzwVQJqGVG146Lyy2kFGNdn2jxI0VIWFlleHGZsWXGTc5vtsPMjQ5M56/uh/wrqm8znrs103QCE5Ed/rN6zVBOLUO5RXtvn7E9lJem2db3Gz+Gi56E6181fsTSHXAIE1bmz3fQyheQpt39iaN7qUeGWemorTu9/QE=", 
                        BinaryEncoding.Base64
                    ), 
                    Compression.Deflate
                )
            ), 
            let 
                _t = ((type nullable text) meta [Serialized.Text = true]) 
            in 
                type table [#"Employee ID" = _t, #"Full Name" = _t, #"Effective date" = _t, Department = _t, #"Prev Row Department" = _t, #"Prev Row Effective date" = _t, #"Prev Row End effective date" = _t]
        ),
        initial_type_set = 
        Table.TransformColumnTypes(
            Source,
            {
                {"Employee ID", Int64.Type}, {"Full Name", type text}, {"Effective date", type text}, {"Department", type text}, {"Prev Row Department", type text}, {"Prev Row Effective date", type text}, {"Prev Row End effective date", type text}
            }
        ),
        unpivot_selected = 
        Table.Unpivot(
            Table.SelectColumns(
                initial_type_set, 
                {"Department", "Prev Row Department", "Employee ID"}
            ), 
            {"Department", "Prev Row Department"}, 
            "Attribute", 
            "Value"
        ),
        merge_fte = 
        Table.NestedJoin(
            unpivot_selected, 
            {"Employee ID"}, 
            fteTable, 
            {"Employee ID"}, 
            "fteTable", 
            JoinKind.LeftOuter
        ),
        expand_fte = 
        Table.ExpandTableColumn(
            merge_fte, 
            "fteTable", 
            {"FTE"}, 
            {"FTE"}
        ),
        set_negatives = 
        Table.ReplaceValue(
            expand_fte, 
            each [FTE], 
            each 
                if [Attribute] = "Prev Row Department" 
                    then -[FTE] 
                    else [FTE], 
            Replacer.ReplaceValue, 
            {"FTE"}
        ),
        remove_columns = 
        Table.RemoveColumns(
            set_negatives,
            {"Employee ID", "Attribute"}
        ),
        final_type_set = 
        Table.TransformColumnTypes(
            remove_columns,
            {
                {"FTE", type number}
            }
        ),
        group_rows = 
        Table.Group(
            final_type_set, 
            {"Value"}, 
            {
                {"FTE", each List.Sum([FTE]), type nullable number}
            }
        ),
        rename_value = 
        Table.RenameColumns(
            group_rows,
            {
                {"Value", "Department"}
            }
        )
    in
        rename_value

     

    I used the following code to create FTE values based on the example data you provided.

    let
        Source = 
        #table(
            type table [#"Employee ID"=nullable number, FTE=nullable number],
            {
                {1,.5},
                {2,1},
                {3,1},
                {4,.8},
                {5,1},
                {6,1},
                {7,.66},
                {8,1},
                {9,1},
                {10,1}
            }
        )
    in
        Source

     

    To end up with the result. (Note: I grouped by 'Department', summing the FTE.)

     

  • Hi AndrewPF 

    May I check if this issue has been resolved? If not, Please feel free to contact us if you have any further questions.


    Thank you

  • Hi AndrewPF 

    This isn’t actually a transpose problem—it’s about splitting each row into two records to represent movement in and out. The most effective way to handle it is in Power Query by creating two rows per original record: one for the “Loaned To” department (using the Department column with a positive FTE value) and one for the “Borrowed From” department (using Prev Row Department with a negative FTE value). You can do this either by duplicating the query and appending, or more efficiently by creating a list column that generates both records per row and then expanding it. This approach ensures you end up with the required 20 rows, correctly capturing inflow and outflow, and makes your final aggregation by department straightforward and accurate.