Transform 2 column to 3 column data

ExcelNewbie2020

Active Member
Joined
Dec 3, 2020
Messages
350
Office Version
  1. 365
Platform
  1. Windows
Sirs,

Using excel formula would it be possible to transform this raw data to get the expected result
Book1 (version 1).xlsb
ABCDEF
1RAW DATAEXPECTED RESULT
2DATEANIMALS/INTAKEDATEANIMALSINTAKE
301-02-23DOG01-02-23DOGFOOD
4FOOD01-02-23DOGWATER
5WATER03-02-23CATFISH
603-02-23CAT03-02-23CATRICE
7FISH03-02-23CATBACON
8RICE
9BACON
Sheet1


Many thanks,
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You could try this:

1690621270216.png


1. Input formula in cell C2, then copy formula down to cells below
Excel Formula:
=IF(A2="",C1,A2)
2. Formula in cell D2, then copy formula down to cells below
Excel Formula:
=IF(A2<>"",B2,D1)
3. Formula in cell E2, then copy formula down to cells below
Excel Formula:
=IF(A2="",B2,"")
4. Hardcode values in columns C, D and E via copy-paste values.
5. Filter out blanks in column E, then delete the filtered-out rows.

Another alternative would be use Power Query to save all these cleaning steps so that each time you have new data, you just need to refresh the Power Query steps.
 
Upvote 0
Another option
Fluff.xlsm
ABCDEFG
1DATEANIMALS/INTAKEDATEANIMALSINTAKE
201/02/2023DOG01/02/2023DOGFOOD
3FOOD01/02/2023DOGWATER
4WATER03/02/2023CATFISH
503/02/2023CAT03/02/2023CATRICE
6FISH03/02/2023CATBACON
7RICE
8BACON
9
Main
Cell Formulas
RangeFormula
E2:G6E2=LET(a,A2:A100,b,B2:B100,s,SEQUENCE(ROWS(a)),f,DROP(FREQUENCY(IF((a="")*(b<>""),s),IF((a<>"")*(b<>""),s)),1),HSTACK(TOCOL(IF(SEQUENCE(,MAX(f))<=f,FILTER(a,a<>""),1/0),2),TOCOL(IF(SEQUENCE(,MAX(f))<=f,FILTER(b,a<>""),1/0),2),FILTER(b,(b<>"")*(a=""))))
Dynamic array formulas.
 
Upvote 0
an alternative solution using Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"ANIMALS/INTAKE", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [DATE] <> null then [#"ANIMALS/INTAKE"] else null),
    #"Duplicated Column" = Table.DuplicateColumn(#"Added Custom", "DATE", "DATE - Copy"),
    #"Filled Down" = Table.FillDown(#"Duplicated Column",{"Custom", "DATE - Copy"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each [DATE] = null),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"DATE"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"DATE - Copy", "Custom", "ANIMALS/INTAKE"})
in
    #"Reordered Columns"

Book1
ABCDEF
2DATEANIMALS/INTAKEDATE - CopyCustomANIMALS/INTAKE
32/1/2023DOG2/1/2023DOGFOOD
4FOOD2/1/2023DOGWATER
5WATER2/3/2023CATFISH
62/3/2023CAT2/3/2023CATRICE
7FISH2/3/2023CATBACON
8RICE
9BACON
Sheet1
 
Upvote 1
Another (slightly shorter) formula
Excel Formula:
=LET(a,A2:A100,b,B2:B100,s,SEQUENCE(ROWS(a)),f,DROP(FREQUENCY(IF((a="")*(b<>""),s),IF((a<>"")*(b<>""),s)),1),r,SCAN(,f,LAMBDA(a,b,a+b)),HSTACK(INDEX(FILTER(HSTACK(a,b),a<>""),XMATCH(SEQUENCE(MAX(r)),r,1),{1,2}),FILTER(b,(b<>"")*(a=""))))
 
Upvote 0
Solution
Another (slightly shorter) formula
Excel Formula:
=LET(a,A2:A100,b,B2:B100,s,SEQUENCE(ROWS(a)),f,DROP(FREQUENCY(IF((a="")*(b<>""),s),IF((a<>"")*(b<>""),s)),1),r,SCAN(,f,LAMBDA(a,b,a+b)),HSTACK(INDEX(FILTER(HSTACK(a,b),a<>""),XMATCH(SEQUENCE(MAX(r)),r,1),{1,2}),FILTER(b,(b<>"")*(a=""))))
thanks man.. this works
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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