Power Query: Complex unpivot rows within table

Dandada2602

New Member
Joined
Nov 22, 2018
Messages
13
Hi,

Im doing a time measurement of cycle times per operator. The sheet use to track the time for each name has title rows that describe crane , lane, location where the measurement was taken (see image is the area in the red box). This is done per each operator. I'm trying to unpivot the data for a more usable to be able to analyze. I just can figure out how to unpivot the data. Consider I have several sheets and longer list of data for this reason I need help with guidance.

This is the data:
Sr NoEmp IdOperator
Date
DescriptionTime 1Time 2Time 3Time 4Time 5Time 6Time 7Time 8Time 9Time 10Median by Step# MeasurementsTotal Medain
1Juan12/1/2023CraneQ02Q02Q02Q06Q06Q06Q06Q06Q069106.80
Lane333222222
Location140704140504140708100304100306100308100404100406100408
Step17.237.248.238.289.327.6979.167.977.97
Step219.6924.9712.814.3420.3418.7714.6513.0423.9318.77
Step317.4318.5117.2215.4415.2115.9816.9616.0917.616.96
Step435.4330.1538.2721.2615.526.7832.622.4824.726.78
Step531.9321.9513.1125.821.2212.2624.5316.4916.0421.22
Step623.7421.9417.1721.0719.9518.2820.3220.9120.3820.38
Total Cycle Time135.45124.76106.8106.19101.5499.76116.0698.17110.62106.80
2Pedro12/1/2023CraneQ01Q01Q013101.88
Lane444
Location100208100404100312
Step18.169.157.858.16
Step216.1614.7318.4916.16
Step316.6117.8516.8716.87
Step424.3523.328.724.35
Step515.3719.8216.9216.92
Step617.6717.0316.5817.03
Total Cycle Time98.32101.88105.41101.88
3Felipe12/1/2023CraneQ01Q01Q01Q01Q01Q01Q017106.90
Lane4444444
Location100502100406100408100604100606100608100610
Step19.526.557.637.117.387.547.277.38
Step219.5826.6813.2115.7326.1113.7318.618.60
Step318.6516.6416.916.5416.6216.8515.8716.64
Step419.2522.9721.2719.9134.7225.4324.4622.97
Step531.4921.2516.7623.6615.3511.1611.3216.76
Step621.1813.9216.6523.9521.5622.4118.6221.18
Total Cycle Time119.67108.0192.42106.9121.7497.1296.14106.90
4MariaCraneQ02Q02Q02Q02Q02Q02Q02Q02Q02Q0210108.18
Lane4444444444
Location220108220110220202220204220206220302220304220404220406220408
Step19.158.898.5510.319.468.89.099.118.38.538.99
Step216.4917.4723.6515.8816.115.0116.8815.7617.6918.8216.69
Step317.4719.6918.6618.9418.2317.7718.518.8119.4819.118.74
Step425.2928.3228.5322.5928.0222.8229.2828.7522.7427.6727.85
Step512.3515.3618.4515.4513.1517.3816.0718.3116.9114.815.76
Step621.3921.222.922.1421.8726.1823.5421.9720.5519.4821.92
Total Cycle Time102.14110.93120.74105.31106.83107.96113.36112.71105.67108.4108.2
5Ana12/27/2023CraneQ06Q06Q06Q06Q06Q066103.87
Lane222222
Location100304100306100308100404100406100408
Step18.289.327.6979.167.978.13
Step214.3420.3418.7714.6513.0423.9316.71
Step315.4415.2115.9816.9616.0917.616.04
Step421.2615.526.7832.622.4824.723.59
Step525.821.2212.2624.5316.4916.0418.86
Step621.0719.9518.2820.3220.9120.3820.35
Total Cycle Time106.19101.5499.76116.0698.17110.62103.9
7Gabriel12/27/2023CraneQC06QC06QC06QC06QC06QC06QC06QC06893.13
Lane22222222
Location380106380506380606380206380306380406380706380806
Step113.077.468.367.728.618.529.517.768.44
Step213.7111.2412.4730.0111.7411.2412.4314.0112.45
Step316.1218.5216.1516.2316.2615.8615.9535.8416.19
Step415.2415.3621.5112.9111.2613.3126.0614.4414.84
Step514.6512.8713.2513.8713.9112.9226.512.1513.56
Step617.8420.6423.8819.5121.9919.219.6914.1619.60
Total Cycle Time90.6386.0995.62100.2583.7781.05110.1498.3693.13
 

Attachments

  • Unpivot 5.jpg
    Unpivot 5.jpg
    165.2 KB · Views: 23

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Since you did not provide a mock up of what you want your unpivoted data to look like, I guessed. If this is not what you expect, then mock up a solution and show us so we can further assist

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Filled Down" = Table.FillDown(#"Changed Type",{"Sr No", "Emp Id", "Operator", "Date"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Filled Down", {"Sr No", "Emp Id", "Operator", "Date", "Description"}, "Attribute", "Value")
in
    #"Unpivoted Other Columns"
 
Upvote 0
The issue is you have 4 rows of headers that need to be consolidated by transposing the data. Your issue is that some of information on the same rows of headers is source data. I'm not use how to get around that without Blunt Force Manual manipulation. Good Luck, but I don't have a solution for you. Maybe one of the stronger PQ experts has the time and energy for this one.
 
Upvote 0
Some caveats:
1) This code might be pretty slow for large data sets,
2) It will fail unless there are exactly 10 Time columns in the Source data, and the columns are exactly as specified in the sample.
3) It assumes the Source data is an excel table called Table1, if not change the first step accordingly.
4) There is a typo in your sample data. A column called "Total Medain" is in the sample and has not been changed. If this typo is corrected you will need to change "Total Medain" to "Total Median" in four places in the code.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Time 10", "Time 99"}}),
    #"Replaced Value1" = Table.ReplaceValue(#"Renamed Columns",null,"x",Replacer.ReplaceValue,List.Select(Table.ColumnNames(#"Renamed Columns"), each Text.Start(_,5)="Time ")),
    #"Filled Down" = Table.FillDown(#"Replaced Value1",{"Sr No", "Emp Id", "Operator", "Date"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Filled Down", {"Sr No", "Emp Id", "Operator", "Date", "Description", "# Measurements", "Total Medain"}, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Sr No"}, {{"All", each 
        let 
            #"Replaced Value" = Table.ReplaceValue(_,null,"x",Replacer.ReplaceValue,{"Description", "# Measurements", "Total Medain", "Attribute", "Value"}),
            #"Pivoted Column" = Table.Pivot(#"Replaced Value", List.Distinct(#"Replaced Value"[Description]), "Description", "Value"),
            #"Filtered Rows" = Table.SelectRows(#"Pivoted Column", each ([Attribute] <> "Median by Step")),
            tbl = Table.RemoveColumns(#"Filtered Rows",{"Sr No", "Emp Id", "Operator", "Date", "# Measurements", "Total Medain", "Attribute"}),
            lst = Table.ToColumns(tbl),
            lst1 = List.Transform(lst, each List.RemoveNulls(_)),
            lst2 = Table.ToColumns(Table.FirstN(Table.SelectColumns(#"Filtered Rows", {"Sr No", "Emp Id", "Operator", "Date", "# Measurements", "Total Medain", "Attribute"}),10)),
            tbl2 = Table.FromColumns(lst2 & lst1, Table.ColumnNames(#"Filtered Rows")),
            tbl3 = Table.SelectRows(tbl2, each [Crane]<>"x"),
            tbl4 = Table.TransformColumns(tbl3, {"# Measurements", each 1}),
            lst3 = {null,null,null,null, Table.RowCount(tbl4), null, "Median by step", null,null,null,
                    List.Median(tbl4[Step1]),List.Median(tbl4[Step2]),List.Median(tbl4[Step3]),List.Median(tbl4[Step4]),List.Median(tbl4[Step5]),List.Median(tbl4[Step6]),List.Median(tbl4[Total Cycle Time])},
            rcd = Record.FromList(lst3, Table.ColumnNames(tbl4)),
            tbl5 = Table.FromRecords(Table.ToRecords(tbl4) & {rcd})
        in 
            tbl5}}
        , GroupKind.Local),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", List.Skip(Table.ColumnNames(#"Grouped Rows"[All]{0}))),
    #"Renamed Columns1" = Table.RenameColumns(#"Expanded All",{{"Attribute", "Description"}}),
    #"Replaced Value" = Table.ReplaceValue(#"Renamed Columns1","Time 99","Time 10",Replacer.ReplaceText,{"Description"}),
    #"Filled Down1" = Table.FillDown(#"Replaced Value",{"Sr No", "Emp Id", "Operator", "Date"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Filled Down1",List.Select(Table.ColumnNames(#"Filled Down1"), each _<> "# Measurements") & {"# Measurements"})
in
    #"Reordered Columns"
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top