Power query with data in difficult format

mhessnm

New Member
Joined
Apr 12, 2019
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm trying to take a text file with data in the following format. The data begins like this:

OGRID: [373075] XTO PERMIAN OPERATING LLC.
Reference ID: fAPP2226958316
Reference Period: 202301
Days Reported: 18
Total Flared: 14,355 MCF
Total Vented: 0 MCF
OGRID: [373075] XTO PERMIAN OPERATING LLC.
Reference ID: fAPP2126356070
Reference Period: 202301
Days Reported: 4
Total Flared: 6,570 MCF
Total Vented: 0 MCF


The data repeats this format for about 1800 lines. I would like to use the repeating characters before the colon (OGRID, Reference ID, Reference Period, Days Reported, Total Flared, Total Vented) as column names and then place all the data behind the colon in the appropriate columns, so it looks like this:

OGRIDReference IDReference PeriodDays ReportedTotal FlaredTotal Vented
[373075] XTO PERMIAN OPERATING LLC.fAPP22269583162023011814,355 MCF0 MCF
[373075] XTO PERMIAN OPERATING LLC.fAPP212635607020230146,570 MCF0 MCF

I have been able to separate the data by splitting the column using the colon as delimiter, but after that I'm stuck. I've tried Transform and Pivot Column, but if I tell it not to aggregate I get an error. I would appreciate any help I can get. Thank you!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByDelimiter(": ", QuoteStyle.Csv), {"Col1", "Col2"}),
    tbl1 = Table.Group(tbl, {"Col1"}, {{"Col2", each _[Col2]}}),
    Result = Table.FromColumns(tbl1[Col2], tbl1[Col1])
in
    Result

Book1
ABCDEFGHI
1Column1OGRIDReference IDReference PeriodDays ReportedTotal FlaredTotal Vented
2OGRID: [373075] XTO PERMIAN OPERATING LLC.[373075] XTO PERMIAN OPERATING LLC.fAPP22269583162023011814,355 MCF0 MCF
3Reference ID: fAPP2226958316[373075] XTO PERMIAN OPERATING LLC.fAPP212635607020230146,570 MCF0 MCF
4Reference Period: 202301
5Days Reported: 18
6Total Flared: 14,355 MCF
7Total Vented: 0 MCF
8OGRID: [373075] XTO PERMIAN OPERATING LLC.
9Reference ID: fAPP2126356070
10Reference Period: 202301
11Days Reported: 4
12Total Flared: 6,570 MCF
13Total Vented: 0 MCF
14
Sheet1
 
Upvote 0
Hey JGordon11, that was awesome! I so appreciate your time! I don't understand it, because I'm not that advanced, but I'll study the syntax. Thanks again!
 
Upvote 0
This way may be more useful. It gets rid of the MCF unit in the totals columns and turns the text into numbers that can be operated on (average, max, min, etc.).

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByDelimiter(": ", QuoteStyle.Csv), {"Col1", "Col2"}),
    tbl1 = Table.Group(tbl, {"Col1"}, {{"Col2", each _[Col2]}}),
    tbl2 = Table.FromColumns(tbl1[Col2], tbl1[Col1]),
    tbl3 = Table.TransformColumns(tbl2, {{"Total Flared", each Number.From(Text.Replace(_, " MCF", ""))}, {"Total Vented", each Number.From(Text.Replace(_, " MCF", ""))}}),
    tbl4 = Table.RenameColumns(tbl3, {{"Total Flared", "Total Flared (MCF)"}, {"Total Vented", "Total Vented (MCF)"}}),
    Result = Table.TransformColumnTypes(tbl4,{{"Reference Period", Int64.Type}, {"Total Flared (MCF)", type number}, {"Total Vented (MCF)", type number}, {"Days Reported", Int64.Type}})
in
    Result
 
Upvote 0
Solution
Oh, that's cool! Thank you! While I can understand what the query is doing, I want to make sure I understand how so this will give me a lot of things to study.
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

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