Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Mohaaa
Frequent Visitor

Need help to extract specific cells from rows to be demoted as fields

Hi, I am looking for help in Power query extraction, where in Row 1 the CSV file has specific values in the cells B1 (Date), C1 (Time) & D1 (Date) associated with the file. Proper dataset starts from Row 2 which has 64 columns. I would require the cells B1, C1 & D1 to retrieve as additional 3 columns to the dataset. Post which we will promote the row 2 as headers of the dataset and use these 3 fields to the dataset. Image of expected results  from PQ1_Extraction.pngfrom Power query extraction

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @Mohaaa, check this:

 

Before

dufoq3_0-1726423931644.png

 

After

dufoq3_1-1726423943818.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcvNwCVLSUVIAA9eKkqLE5BIF56LUxJLM/DwFl8SSVCsFAyN9I0t9IwMjE9wqQzJzgSqNDK0MLIGKnP2dULTF6kQrOefnlObmKRiCpCFMIwTTGMGEqC7Oz01VSEksSQSph3OMkDnGyBygrlgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column 1" = _t, #"Column 2" = _t, #"Column 3" = _t, #"Column 4" = _t]),
    Row1Extract = List.Transform(List.Skip(Record.ToList(Source{0})), Text.Trim),
    Combined = Table.FromColumns(Table.ToColumns(Table.Skip(Source, 1)) & Table.ToColumns(Table.FromRows({Row1Extract})))
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

8 REPLIES 8
dufoq3
Super User
Super User

Hi @Mohaaa, check this:

 

Before

dufoq3_0-1726423931644.png

 

After

dufoq3_1-1726423943818.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcvNwCVLSUVIAA9eKkqLE5BIF56LUxJLM/DwFl8SSVCsFAyN9I0t9IwMjE9wqQzJzgSqNDK0MLIGKnP2dULTF6kQrOefnlObmKRiCpCFMIwTTGMGEqC7Oz01VSEksSQSph3OMkDnGyBygrlgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column 1" = _t, #"Column 2" = _t, #"Column 3" = _t, #"Column 4" = _t]),
    Row1Extract = List.Transform(List.Skip(Record.ToList(Source{0})), Text.Trim),
    Combined = Table.FromColumns(Table.ToColumns(Table.Skip(Source, 1)) & Table.ToColumns(Table.FromRows({Row1Extract})))
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

v-heq-msft
Community Support
Community Support

Hi @Mohaaa ,
You can try the following code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7PKc3NUzBU0oExjRBMYwTTRCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [FHDR = _t, #"Extract Creation Date: 02/29/2024" = _t, #"Extract Creation Time: 21:09" = _t, #"COB: 02/29/2024" = _t]),
    ColumnNames = List.Skip(Table.ColumnNames(Source), 1),
    ColumnNamesToRepeat = List.FirstN(ColumnNames, 3),
    ColumnsToRepeat = Table.SelectColumns(Source, ColumnNamesToRepeat),
    RenamedColumnsToRepeat = Table.RenameColumns(ColumnsToRepeat, List.Zip({ColumnNamesToRepeat, List.Transform(ColumnNamesToRepeat, each _ & " (Copy)")})),
    CombinedTable = Table.FromColumns(
        Table.ToColumns(Source) & Table.ToColumns(RenamedColumnsToRepeat),
        Table.ColumnNames(Source) & List.Transform(ColumnNamesToRepeat, each _ & " (Copy)")
    ),
    #"Replaced Value" = Table.ReplaceValue(CombinedTable,"Column 2","Column 65",Replacer.ReplaceText,{"Extract Creation Date: 02/29/2024 (Copy)"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Column 3","Column 66",Replacer.ReplaceText,{"Extract Creation Time: 21:09 (Copy)"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","Column 4","Column 67",Replacer.ReplaceText,{"COB: 02/29/2024 (Copy)"})
in
    #"Replaced Value2"

vheqmsft_0-1725850913354.png

 

Best regards,
Albert He


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

Hi Albert -Thanks for your code. However I use the 30 days of CSV files generated in a folder, so I have amended the Source in the query and combined with yours as below. I would need your help to extract these 3 cells (B1, C1 & D1) as separate columns for each file which already has 64 columns. So if I wanted to retrieve 67 columns (64 + 3) in total from each file.

 

My Code: 

let
Source = Folder.Files("T:\MI & Control\File Uploads\IM Custodian File\BONY\2024\03 March"),
Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45wc3PNYzDIz0PMyBUOExjRBWYlTRCKzfGA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"HDR" = _t, #"Extract Creation Date: 02/29/2024" = _t, #"Extract Creation Time: 21:09" = _t, #"COB: 02/29/2024" = _t]),
columnNames = List.Skip(Table.ColumnNames(Source), 1),
ColumnNamesToRepeat = List.FirstN(columnNames, 3),
ColumnsToRepeat = Table.SelectColumns(Source, ColumnNamesToRepeat),
RenamedColumnsToRepeat = Table.RenameColumns(ColumnsToRepeat, List.Zip({ColumnNamesToRepeat, List.Transform(ColumnNamesToRepeat, each _ & " (Copy)")})),
CombinedTable = Table.FromColumns(
Table.ToColumns(Source) & Table.ToColumns(RenamedColumnsToRepeat),
Table.ColumnNames(Source) & List.Transform(ColumnNamesToRepeat, each _ & " (Copy)")),
#"Replaced Value" = Table.ReplaceValue(CombinedTable, "column 1", Table.Replacer.ReplaceText, {"Extract Creation Date: 02/29/2024 (Copy)"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value", "column 3", Table.Replacer.ReplaceText, {"Extract Creation Time: 21:09 (Copy)"}),
#"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2", "column 4", Table.Replacer.ReplaceText, {"COB: 02/29/2024 (Copy)"})
in
#"Replaced Value3"

Mohaaa
Frequent Visitor

@Greg_Deckler  - Pls find the data pasted as text below:

 

FHDR Extract Creation Date: 02/29/2024 Extract Creation Time: 21:09 COB: 02/29/2024 Extract Creation Date: 02/29/2024 Extract Creation Time: 21:09 COB: 02/29/2024
Column 1 Column 2 Column 3 Column 4 Column 5 - - - Column 64 Column 65 Column 66 Column 67

 

Omid_Motamedise
Memorable Member
Memorable Member

To achieve this in Power Query, you can follow these steps:

Import the CSV file: Load the CSV file into Power Query.

Retrieve values from B1, C1, and D1:

First, you can refer to Row 1 using the Table.FirstN function.
Then, you can extract the values from columns B, C, and D.
Add the extracted values as new columns:

Use the Table.AddColumn function to add the values of B1, C1, and D1 as new columns to your dataset.
Remove Row 1 and promote Row 2 to headers:

Remove Row 1 since the data starts from Row 2.
Promote Row 2 as headers using the Table.PromoteHeaders function.

 

 

let
    // Load the data
    Source = Csv.Document(File.Contents("YourFilePath.csv"), [Delimiter=",", Columns=64, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    
    // Extract values from Row 1
    FirstRow = Table.FirstN(Source, 1),
    Date1 = FirstRow{0}[Column2],  // B1
    Time = FirstRow{0}[Column3],   // C1
    Date2 = FirstRow{0}[Column4],  // D1
    
    // Skip the first row and start from the actual data (Row 2 onwards)
    DataRows = Table.Skip(Source, 1),
    
    // Promote the second row to headers
    PromotedHeaders = Table.PromoteHeaders(DataRows, [PromoteAllScalars=true]),
    
    // Add the extracted columns
    AddDate1 = Table.AddColumn(PromotedHeaders, "Date1", each Date1),
    AddTime = Table.AddColumn(AddDate1, "Time", each Time),
    AddDate2 = Table.AddColumn(AddTime, "Date2", each Date2)
in
    AddDate2

Hi Omid- Thanks for your help. However, I amended the Source in the code accordingly as below but noticed this error.  Please advise.

Error I noticed:

Mohaaa_0-1726308799005.jpeg

 

 

 

 

My Code

let
Source = Folder.Files(("T:\\MI & Control\\File Uploads\\IM Custodian File\\BONY\\2024\\03 March")),

// Extract values from Row 1
FirstRow = Table.FirstN(Source, 1),
Date1 = FirstRow{0}[Column2], // B1
Time = FirstRow{0}[Column3], // C1
Date2 = FirstRow{0}[Column4], // D1

// Skip the first row and start from the actual data (Row 2 onwards)
DataRows = Table.Skip(Source, 1),

// Promote the second row to headers
PromotedHeaders = Table.PromoteHeaders(DataRows, [PromoteAllScalars=true]),

// Add the extracted columns
AddDate1 = Table.AddColumn(PromotedHeaders, "Date1", each Date1),
AddTime = Table.AddColumn(AddDate1, "Time", each Time),
AddDate2 = Table.AddColumn(AddTime, "Date2", each Date2)
in
AddDate2

 

 

 

Mohaaa
Frequent Visitor

I have the sample file format, unfortunate no option to upload Excel/CSV files. So pasting them here.

 

FHDR      Extract Creation Date: 02/29/2024      Extract Creation Time: 21:09COB: 02/29/2024           Extract Creation Date: 02/29/2024      Extract Creation Time: 21:09COB: 02/29/2024
Column 1Column 2Column 3Column 4Column 5---Column 64Column 65Column 66Column 67
Greg_Deckler
Super User
Super User

@Mohaaa Can you post same data as text?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.