Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I combine a single query of multiple sub-tables (with repeating headers, etc) into a single table

I have a query (weather data) that results in multiple sub-tables. For example, the initial raw query looks like this:

Column1 Column2 Columns3-12
Boston Hourly Forecast Made Dec 6 2023 1134 UTC
LocalTime Temp
12/6/2023 12:00:00 AM 32
12/6/2023 1:00:00 AM 30.92
12/20/2023 11:00:00 PM 38.48
NYC Hourly Forecast Made Dec 6 2023 1134 UTC
LocalTime Temp
12/6/2023 12:00:00 AM 24.98

So each "sub-table" consists of two header rows: the first with two columns including the city and the forecast date, and the second containing the column headers (12 of them, in this case).

Then we have 360 rows of data (not sure if it’s always 360), followed by a blank row, and then that pattern repeats for the next city (in my example above, "NYC").

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Simply removing the first row, promoting the headers from the second row and then filtering all other "non-forecast" values is easy. What I would really like to do is figure out a way to add the city name to a new column, so that I get this end result:

City LocalTime Temp
Boston 12/6/2023 12:00:00 AM 32
Boston 12/6/2023 1:00:00 AM 30.92
Boston
Boston 12/20/2023 11:00:00 PM 38.48
NYC 12/6/2023 12:00:00 AM 24.98
NYC

I have tried adding index columns and referencing previous rows and all that without much success. My temporary solution will be to just use formulas in Excel itself, but I’m hoping to have a pure Powerquery solution, if only for my own education

>Solution :

When you’re at this stage:

enter image description here

Add a column:

try if Text.StartsWith([Column2], "Hourly Forecast") then [Column1] else null otherwise null

enter image description here
Then fill down

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading