Help needed with grouping of data

sdhasan

New Member
Joined
Oct 12, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

I need help with sorting data in the below table. As you can see, the same person no (62) has two different cost center (1210 & 1220). I want a new table which basically mentions the first date of the cost center and the last date of the cost center as shown below:

Current table:

1669893078252.png


Required Table:
1669893153708.png
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
in Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
    tbl1 = Table.AddIndexColumn(tbl, "Index", 0, 1, Int64.Type),
    tbl2 = Table.AddColumn(tbl1, "IsStart", each try if [Cost Centre] <> tbl1[Cost Centre]{[Index] - 1}  or [#"Pers.No."] <> tbl1[#"Pers.No."]{[Index] - 1} then true else false otherwise true),
    tbl3 = Table.AddColumn(tbl2, "IsEnd", each try if [Cost Centre] <> tbl1[Cost Centre]{[Index] + 1}  or [#"Pers.No."] <> tbl1[#"Pers.No."]{[Index] + 1} then true else false otherwise true),
    tbl4 = Table.SelectRows(tbl3, each [IsStart] or [IsEnd]),
    tbl5 = Table.FromRecords(Table.TransformRows(tbl4, each if not [IsEnd] then Record.TransformFields(_, {"End Date", (x)=> null}) else _)),
    tbl6 = Table.FillUp(tbl5,{"End Date"}),
    tbl7 = Table.SelectRows(tbl6, each [IsStart]),
    Result = Table.RemoveColumns(tbl7,{"Index", "IsStart", "IsEnd"})
in
    Result

Book2
ABCDEFGHIJ
1Pers.No.Start DateEnd DateCost CentrePers.No.Start DateEnd DateCost Centre
2621/1/20221/9/20221621/1/20221/9/20221
3621/10/20221/31/20222621/10/20223/15/20222
4622/1/20222/28/20222623/16/20225/10/20221
5623/1/20223/15/20222625/11/20228/19/20222
6623/16/20223/31/20221
7624/1/20224/30/20221
8625/1/20225/10/20221
9625/11/20225/31/20222
10626/1/20226/30/20222
11627/1/20227/31/20222
12628/1/20228/19/20222
13
Sheet1
 
Upvote 0
in Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
    tbl1 = Table.AddIndexColumn(tbl, "Index", 0, 1, Int64.Type),
    tbl2 = Table.AddColumn(tbl1, "IsStart", each try if [Cost Centre] <> tbl1[Cost Centre]{[Index] - 1}  or [#"Pers.No."] <> tbl1[#"Pers.No."]{[Index] - 1} then true else false otherwise true),
    tbl3 = Table.AddColumn(tbl2, "IsEnd", each try if [Cost Centre] <> tbl1[Cost Centre]{[Index] + 1}  or [#"Pers.No."] <> tbl1[#"Pers.No."]{[Index] + 1} then true else false otherwise true),
    tbl4 = Table.SelectRows(tbl3, each [IsStart] or [IsEnd]),
    tbl5 = Table.FromRecords(Table.TransformRows(tbl4, each if not [IsEnd] then Record.TransformFields(_, {"End Date", (x)=> null}) else _)),
    tbl6 = Table.FillUp(tbl5,{"End Date"}),
    tbl7 = Table.SelectRows(tbl6, each [IsStart]),
    Result = Table.RemoveColumns(tbl7,{"Index", "IsStart", "IsEnd"})
in
    Result

Book2
ABCDEFGHIJ
1Pers.No.Start DateEnd DateCost CentrePers.No.Start DateEnd DateCost Centre
2621/1/20221/9/20221621/1/20221/9/20221
3621/10/20221/31/20222621/10/20223/15/20222
4622/1/20222/28/20222623/16/20225/10/20221
5623/1/20223/15/20222625/11/20228/19/20222
6623/16/20223/31/20221
7624/1/20224/30/20221
8625/1/20225/10/20221
9625/11/20225/31/20222
10626/1/20226/30/20222
11627/1/20227/31/20222
12628/1/20228/19/20222
13
Sheet1
Ola Gordon!

I've never used a power query, could you guide me how to use it?

Best Regards,
Syed Hasan Danish
 
Upvote 0
If you wanted a formula approach ..

If you have the HSTACK function you can use the formula in F2, otherwise two formulas as in K2 and M2.

BTW, I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

22 12 03.xlsm
ABCDEFGHIJKLMN
1Pers.No.Start DateEnd DateCost CentrePers.No.Start DateEnd DateCost CentrePers.No.Start DateEnd DateCost Centre
2621/01/20229/01/20221621/01/20229/01/20221621/01/20229/01/20221
36210/01/202231/01/202226210/01/202215/03/202226210/01/202215/03/20222
4621/02/202228/02/202226216/03/202210/05/202216216/03/202210/05/20221
5621/03/202215/03/202226211/05/202219/08/202226211/05/202219/08/20222
66216/03/202231/03/20221
7621/04/202230/04/20221
8621/05/202210/05/20221
96211/05/202231/05/20222
10621/06/202230/06/20222
11621/07/202231/07/20222
12621/08/202219/08/20222
13
Group
Cell Formulas
RangeFormula
F2:I5F2=HSTACK(FILTER(A2:B12,D2:D12<>D1:D11),FILTER(C2:D12,D2:D12<>D3:D13))
K2:L5K2=FILTER(A2:B12,D2:D12<>D1:D11)
M2:N5M2=FILTER(C2:D12,D2:D12<>D3:D13)
Dynamic array formulas.
 
Upvote 0
Solution
If you wanted a formula approach ..

If you have the HSTACK function you can use the formula in F2, otherwise two formulas as in K2 and M2.

BTW, I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

22 12 03.xlsm
ABCDEFGHIJKLMN
1Pers.No.Start DateEnd DateCost CentrePers.No.Start DateEnd DateCost CentrePers.No.Start DateEnd DateCost Centre
2621/01/20229/01/20221621/01/20229/01/20221621/01/20229/01/20221
36210/01/202231/01/202226210/01/202215/03/202226210/01/202215/03/20222
4621/02/202228/02/202226216/03/202210/05/202216216/03/202210/05/20221
5621/03/202215/03/202226211/05/202219/08/202226211/05/202219/08/20222
66216/03/202231/03/20221
7621/04/202230/04/20221
8621/05/202210/05/20221
96211/05/202231/05/20222
10621/06/202230/06/20222
11621/07/202231/07/20222
12621/08/202219/08/20222
13
Group
Cell Formulas
RangeFormula
F2:I5F2=HSTACK(FILTER(A2:B12,D2:D12<>D1:D11),FILTER(C2:D12,D2:D12<>D3:D13))
K2:L5K2=FILTER(A2:B12,D2:D12<>D1:D11)
M2:N5M2=FILTER(C2:D12,D2:D12<>D3:D13)
Dynamic array formulas.
Hey! Thank you so much, the HSTACK function worked like a charm! :)
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,785
Members
449,095
Latest member
m_smith_solihull

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