Any idea on how I group the data by years?

Bluebird1927rw

New Member
Joined
Nov 20, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Welcome to the board!

You're going to have to unpivot the table first and you're lucky to have Power Query there to help you.

Open an empty Excel workbook and select Get Data / From Other Sources / Blank Query from the Data group of your Excel ribbon. Power Query editor opens. Select Advanced Editor and replace the existing code with this one:
Power Query:
let
    Source = Excel.Workbook(Web.Contents("https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/birthsdeathsandmarriages/conceptionandfertilityrates/datasets/conceptionstatisticsenglandandwalesreferencetables/2018/conceptions2018workbook.xls"), null, true),
    #"Table 1" = Source{[Name="Table 6"]}[Data],
    #"Removed Top Rows" = Table.Skip(#"Table 1",5),
    #"Transposed Table" = Table.Transpose(#"Removed Top Rows"),
    #"Filled Down" = Table.FillDown(#"Transposed Table",{"Column1"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Column2] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Column3", "Column4", "Column5"}),
    #"Extracted First Characters" = Table.TransformColumns(#"Removed Columns", {{"Column1", each Text.Start(_, 4), type text}}),
    #"Merged Columns" = Table.CombineColumns(#"Extracted First Characters",{"Column1", "Column2"},Combiner.CombineTextByDelimiter("#(tab)", QuoteStyle.None),"Merged"),
    #"Transposed Table1" = Table.Transpose(#"Merged Columns"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Area#(tab)Code1", type text}, {"Area#(tab)Name", type text}, {"Area#(tab)Geography", type text}, {"2018#(tab)Number of Conceptions", Int64.Type}, {"2018#(tab)Conception rate per 1,000 women in age group", type number}, {"2018#(tab)Maternity rate per 1,000 women in age group", type number}, {"2018#(tab)Abortion rate per 1,000 women in age group", type number}, {"2018#(tab)Percentage of conceptions leading to abortion", type number}, {"2017#(tab)Number of Conceptions", Int64.Type}, {"2017#(tab)Conception rate per 1,000 women in age group", type number}, {"2017#(tab)Maternity rate per 1,000 women in age group", type number}, {"2017#(tab)Abortion rate per 1,000 women in age group", type number}, {"2017#(tab)Percentage of conceptions leading to abortion", type number}, {"2016#(tab)Number of Conceptions", Int64.Type}, {"2016#(tab)Conception rate per 1,000 women in age group", type number}, {"2016#(tab)Maternity rate per 1,000 women in age group", type number}, {"2016#(tab)Abortion rate per 1,000 women in age group", type number}, {"2016#(tab)Percentage of conceptions leading to abortion", type number}, {"2015#(tab)Number of Conceptions", Int64.Type}, {"2015#(tab)Conception rate per 1,000 women in age group", type number}, {"2015#(tab)Maternity rate per 1,000 women in age group", type number}, {"2015#(tab)Abortion rate per 1,000 women in age group", type number}, {"2015#(tab)Percentage of conceptions leading to abortion", type number}, {"2014#(tab)Number of Conceptions", Int64.Type}, {"2014#(tab)Conception rate per 1,000 women in age group", type number}, {"2014#(tab)Maternity rate per 1,000 women in age group", type number}, {"2014#(tab)Abortion rate per 1,000 women in age group", type number}, {"2014#(tab)Percentage of conceptions leading to abortion", type number}, {"2013#(tab)Number of Conceptions", Int64.Type}, {"2013#(tab)Conception rate per 1,000 women in age group", type number}, {"2013#(tab)Maternity rate per 1,000 women in age group", type number}, {"2013#(tab)Abortion rate per 1,000 women in age group", type number}, {"2013#(tab)Percentage of conceptions leading to abortion", type number}, {"2012#(tab)Number of Conceptions", Int64.Type}, {"2012#(tab)Conception rate per 1,000 women in age group", type number}, {"2012#(tab)Maternity rate per 1,000 women in age group", type number}, {"2012#(tab)Abortion rate per 1,000 women in age group", type number}, {"2012#(tab)Percentage of conceptions leading to abortion", type number}, {"2011#(tab)Number of Conceptions", Int64.Type}, {"2011#(tab)Conception rate per 1,000 women in age group", type number}, {"2011#(tab)Maternity rate per 1,000 women in age group", type number}, {"2011#(tab)Abortion rate per 1,000 women in age group", type number}, {"2011#(tab)Percentage of conceptions leading to abortion", type number}, {"2010#(tab)Number of Conceptions", Int64.Type}, {"2010#(tab)Conception rate per 1,000 women in age group", type number}, {"2010#(tab)Maternity rate per 1,000 women in age group", type number}, {"2010#(tab)Abortion rate per 1,000 women in age group", type number}, {"2010#(tab)Percentage of conceptions leading to abortion", type number}, {"2009#(tab)Number of Conceptions", Int64.Type}, {"2009#(tab)Conception rate per 1,000 women in age group", type number}, {"2009#(tab)Maternity rate per 1,000 women in age group", type number}, {"2009#(tab)Abortion rate per 1,000 women in age group", type number}, {"2009#(tab)Percentage of conceptions leading to abortion", type number}, {"2008#(tab)Number of Conceptions", Int64.Type}, {"2008#(tab)Conception rate per 1,000 women in age group", type number}, {"2008#(tab)Maternity rate per 1,000 women in age group", type number}, {"2008#(tab)Abortion rate per 1,000 women in age group", type number}, {"2008#(tab)Percentage of conceptions leading to abortion", type number}, {"2007#(tab)Number of Conceptions", Int64.Type}, {"2007#(tab)Conception rate per 1,000 women in age group", type number}, {"2007#(tab)Maternity rate per 1,000 women in age group", type number}, {"2007#(tab)Abortion rate per 1,000 women in age group", type number}, {"2007#(tab)Percentage of conceptions leading to abortion", type number}, {"2006#(tab)Number of Conceptions", Int64.Type}, {"2006#(tab)Conception rate per 1,000 women in age group", type number}, {"2006#(tab)Maternity rate per 1,000 women in age group", type number}, {"2006#(tab)Abortion rate per 1,000 women in age group", type number}, {"2006#(tab)Percentage of conceptions leading to abortion", type number}, {"2005#(tab)Number of Conceptions", Int64.Type}, {"2005#(tab)Conception rate per 1,000 women in age group", type number}, {"2005#(tab)Maternity rate per 1,000 women in age group", type number}, {"2005#(tab)Abortion rate per 1,000 women in age group", type number}, {"2005#(tab)Percentage of conceptions leading to abortion", type number}, {"2004#(tab)Number of Conceptions", Int64.Type}, {"2004#(tab)Conception rate per 1,000 women in age group", type number}, {"2004#(tab)Maternity rate per 1,000 women in age group", type number}, {"2004#(tab)Abortion rate per 1,000 women in age group", type number}, {"2004#(tab)Percentage of conceptions leading to abortion", type number}, {"2003#(tab)Number of Conceptions", Int64.Type}, {"2003#(tab)Conception rate per 1,000 women in age group", type number}, {"2003#(tab)Maternity rate per 1,000 women in age group", type number}, {"2003#(tab)Abortion rate per 1,000 women in age group", type number}, {"2003#(tab)Percentage of conceptions leading to abortion", type number}, {"2002#(tab)Number of Conceptions", Int64.Type}, {"2002#(tab)Conception rate per 1,000 women in age group", type number}, {"2002#(tab)Maternity rate per 1,000 women in age group", type number}, {"2002#(tab)Abortion rate per 1,000 women in age group", type number}, {"2002#(tab)Percentage of conceptions leading to abortion", type number}, {"2001#(tab)Number of Conceptions", Int64.Type}, {"2001#(tab)Conception rate per 1,000 women in age group", type number}, {"2001#(tab)Maternity rate per 1,000 women in age group", type number}, {"2001#(tab)Abortion rate per 1,000 women in age group", type number}, {"2001#(tab)Percentage of conceptions leading to abortion", type number}, {"2000#(tab)Number of Conceptions", Int64.Type}, {"2000#(tab)Conception rate per 1,000 women in age group", type number}, {"2000#(tab)Maternity rate per 1,000 women in age group", type number}, {"2000#(tab)Abortion rate per 1,000 women in age group", type number}, {"2000#(tab)Percentage of conceptions leading to abortion", type number}, {"1999#(tab)Number of Conceptions", Int64.Type}, {"1999#(tab)Conception rate per 1,000 women in age group", type number}, {"1999#(tab)Maternity rate per 1,000 women in age group", type number}, {"1999#(tab)Abortion rate per 1,000 women in age group", type number}, {"1999#(tab)Percentage of conceptions leading to abortion", type number}, {"1998#(tab)Number of Conceptions", Int64.Type}, {"1998#(tab)Conception rate per 1,000 women in age group", type number}, {"1998#(tab)Maternity rate per 1,000 women in age group", type number}, {"1998#(tab)Abortion rate per 1,000 women in age group", type number}, {"1998#(tab)Percentage of conceptions leading to abortion", type number}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Area#(tab)Code1", "Area#(tab)Name", "Area#(tab)Geography"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Year", "Attribute"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Year", Int64.Type}, {"Attribute", type text}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute]), "Attribute", "Value"),
    #"Removed Errors" = Table.RemoveRowsWithErrors(#"Pivoted Column", {"Conception rate per 1,000 women in age group"})
in
    #"Removed Errors"

The code gets data directly from the linked file and unpivots it for you.

The code gets you only half way there, though: There's still a lot of Total / Sub Total rows in the data that you might want to filter out before the data is really usable. I'd use the Area Geography column to filter out all the ones you don't want. Do this in Power Query as well. When you're done you can load your data to a worksheet or to your Data Model if you want to create your pivot table using the Power Pivot.
 
Upvote 0
Welcome to the board!

You're going to have to unpivot the table first and you're lucky to have Power Query there to help you.

Open an empty Excel workbook and select Get Data / From Other Sources / Blank Query from the Data group of your Excel ribbon. Power Query editor opens. Select Advanced Editor and replace the existing code with this one:
Power Query:
let
    Source = Excel.Workbook(Web.Contents("https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/birthsdeathsandmarriages/conceptionandfertilityrates/datasets/conceptionstatisticsenglandandwalesreferencetables/2018/conceptions2018workbook.xls"), null, true),
    #"Table 1" = Source{[Name="Table 6"]}[Data],
    #"Removed Top Rows" = Table.Skip(#"Table 1",5),
    #"Transposed Table" = Table.Transpose(#"Removed Top Rows"),
    #"Filled Down" = Table.FillDown(#"Transposed Table",{"Column1"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Column2] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Column3", "Column4", "Column5"}),
    #"Extracted First Characters" = Table.TransformColumns(#"Removed Columns", {{"Column1", each Text.Start(_, 4), type text}}),
    #"Merged Columns" = Table.CombineColumns(#"Extracted First Characters",{"Column1", "Column2"},Combiner.CombineTextByDelimiter("#(tab)", QuoteStyle.None),"Merged"),
    #"Transposed Table1" = Table.Transpose(#"Merged Columns"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Area#(tab)Code1", type text}, {"Area#(tab)Name", type text}, {"Area#(tab)Geography", type text}, {"2018#(tab)Number of Conceptions", Int64.Type}, {"2018#(tab)Conception rate per 1,000 women in age group", type number}, {"2018#(tab)Maternity rate per 1,000 women in age group", type number}, {"2018#(tab)Abortion rate per 1,000 women in age group", type number}, {"2018#(tab)Percentage of conceptions leading to abortion", type number}, {"2017#(tab)Number of Conceptions", Int64.Type}, {"2017#(tab)Conception rate per 1,000 women in age group", type number}, {"2017#(tab)Maternity rate per 1,000 women in age group", type number}, {"2017#(tab)Abortion rate per 1,000 women in age group", type number}, {"2017#(tab)Percentage of conceptions leading to abortion", type number}, {"2016#(tab)Number of Conceptions", Int64.Type}, {"2016#(tab)Conception rate per 1,000 women in age group", type number}, {"2016#(tab)Maternity rate per 1,000 women in age group", type number}, {"2016#(tab)Abortion rate per 1,000 women in age group", type number}, {"2016#(tab)Percentage of conceptions leading to abortion", type number}, {"2015#(tab)Number of Conceptions", Int64.Type}, {"2015#(tab)Conception rate per 1,000 women in age group", type number}, {"2015#(tab)Maternity rate per 1,000 women in age group", type number}, {"2015#(tab)Abortion rate per 1,000 women in age group", type number}, {"2015#(tab)Percentage of conceptions leading to abortion", type number}, {"2014#(tab)Number of Conceptions", Int64.Type}, {"2014#(tab)Conception rate per 1,000 women in age group", type number}, {"2014#(tab)Maternity rate per 1,000 women in age group", type number}, {"2014#(tab)Abortion rate per 1,000 women in age group", type number}, {"2014#(tab)Percentage of conceptions leading to abortion", type number}, {"2013#(tab)Number of Conceptions", Int64.Type}, {"2013#(tab)Conception rate per 1,000 women in age group", type number}, {"2013#(tab)Maternity rate per 1,000 women in age group", type number}, {"2013#(tab)Abortion rate per 1,000 women in age group", type number}, {"2013#(tab)Percentage of conceptions leading to abortion", type number}, {"2012#(tab)Number of Conceptions", Int64.Type}, {"2012#(tab)Conception rate per 1,000 women in age group", type number}, {"2012#(tab)Maternity rate per 1,000 women in age group", type number}, {"2012#(tab)Abortion rate per 1,000 women in age group", type number}, {"2012#(tab)Percentage of conceptions leading to abortion", type number}, {"2011#(tab)Number of Conceptions", Int64.Type}, {"2011#(tab)Conception rate per 1,000 women in age group", type number}, {"2011#(tab)Maternity rate per 1,000 women in age group", type number}, {"2011#(tab)Abortion rate per 1,000 women in age group", type number}, {"2011#(tab)Percentage of conceptions leading to abortion", type number}, {"2010#(tab)Number of Conceptions", Int64.Type}, {"2010#(tab)Conception rate per 1,000 women in age group", type number}, {"2010#(tab)Maternity rate per 1,000 women in age group", type number}, {"2010#(tab)Abortion rate per 1,000 women in age group", type number}, {"2010#(tab)Percentage of conceptions leading to abortion", type number}, {"2009#(tab)Number of Conceptions", Int64.Type}, {"2009#(tab)Conception rate per 1,000 women in age group", type number}, {"2009#(tab)Maternity rate per 1,000 women in age group", type number}, {"2009#(tab)Abortion rate per 1,000 women in age group", type number}, {"2009#(tab)Percentage of conceptions leading to abortion", type number}, {"2008#(tab)Number of Conceptions", Int64.Type}, {"2008#(tab)Conception rate per 1,000 women in age group", type number}, {"2008#(tab)Maternity rate per 1,000 women in age group", type number}, {"2008#(tab)Abortion rate per 1,000 women in age group", type number}, {"2008#(tab)Percentage of conceptions leading to abortion", type number}, {"2007#(tab)Number of Conceptions", Int64.Type}, {"2007#(tab)Conception rate per 1,000 women in age group", type number}, {"2007#(tab)Maternity rate per 1,000 women in age group", type number}, {"2007#(tab)Abortion rate per 1,000 women in age group", type number}, {"2007#(tab)Percentage of conceptions leading to abortion", type number}, {"2006#(tab)Number of Conceptions", Int64.Type}, {"2006#(tab)Conception rate per 1,000 women in age group", type number}, {"2006#(tab)Maternity rate per 1,000 women in age group", type number}, {"2006#(tab)Abortion rate per 1,000 women in age group", type number}, {"2006#(tab)Percentage of conceptions leading to abortion", type number}, {"2005#(tab)Number of Conceptions", Int64.Type}, {"2005#(tab)Conception rate per 1,000 women in age group", type number}, {"2005#(tab)Maternity rate per 1,000 women in age group", type number}, {"2005#(tab)Abortion rate per 1,000 women in age group", type number}, {"2005#(tab)Percentage of conceptions leading to abortion", type number}, {"2004#(tab)Number of Conceptions", Int64.Type}, {"2004#(tab)Conception rate per 1,000 women in age group", type number}, {"2004#(tab)Maternity rate per 1,000 women in age group", type number}, {"2004#(tab)Abortion rate per 1,000 women in age group", type number}, {"2004#(tab)Percentage of conceptions leading to abortion", type number}, {"2003#(tab)Number of Conceptions", Int64.Type}, {"2003#(tab)Conception rate per 1,000 women in age group", type number}, {"2003#(tab)Maternity rate per 1,000 women in age group", type number}, {"2003#(tab)Abortion rate per 1,000 women in age group", type number}, {"2003#(tab)Percentage of conceptions leading to abortion", type number}, {"2002#(tab)Number of Conceptions", Int64.Type}, {"2002#(tab)Conception rate per 1,000 women in age group", type number}, {"2002#(tab)Maternity rate per 1,000 women in age group", type number}, {"2002#(tab)Abortion rate per 1,000 women in age group", type number}, {"2002#(tab)Percentage of conceptions leading to abortion", type number}, {"2001#(tab)Number of Conceptions", Int64.Type}, {"2001#(tab)Conception rate per 1,000 women in age group", type number}, {"2001#(tab)Maternity rate per 1,000 women in age group", type number}, {"2001#(tab)Abortion rate per 1,000 women in age group", type number}, {"2001#(tab)Percentage of conceptions leading to abortion", type number}, {"2000#(tab)Number of Conceptions", Int64.Type}, {"2000#(tab)Conception rate per 1,000 women in age group", type number}, {"2000#(tab)Maternity rate per 1,000 women in age group", type number}, {"2000#(tab)Abortion rate per 1,000 women in age group", type number}, {"2000#(tab)Percentage of conceptions leading to abortion", type number}, {"1999#(tab)Number of Conceptions", Int64.Type}, {"1999#(tab)Conception rate per 1,000 women in age group", type number}, {"1999#(tab)Maternity rate per 1,000 women in age group", type number}, {"1999#(tab)Abortion rate per 1,000 women in age group", type number}, {"1999#(tab)Percentage of conceptions leading to abortion", type number}, {"1998#(tab)Number of Conceptions", Int64.Type}, {"1998#(tab)Conception rate per 1,000 women in age group", type number}, {"1998#(tab)Maternity rate per 1,000 women in age group", type number}, {"1998#(tab)Abortion rate per 1,000 women in age group", type number}, {"1998#(tab)Percentage of conceptions leading to abortion", type number}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Area#(tab)Code1", "Area#(tab)Name", "Area#(tab)Geography"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"Year", "Attribute"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Year", Int64.Type}, {"Attribute", type text}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute]), "Attribute", "Value"),
    #"Removed Errors" = Table.RemoveRowsWithErrors(#"Pivoted Column", {"Conception rate per 1,000 women in age group"})
in
    #"Removed Errors"

The code gets data directly from the linked file and unpivots it for you.

The code gets you only half way there, though: There's still a lot of Total / Sub Total rows in the data that you might want to filter out before the data is really usable. I'd use the Area Geography column to filter out all the ones you don't want. Do this in Power Query as well. When you're done you can load your data to a worksheet or to your Data Model if you want to create your pivot table using the Power Pivot.
@Misca That's incredibly helpful, thank you very much for taking your time to help with this. I get 2 errors with the code, firstly under the 'remove' errors step i'm getting 'We couldn't convert to number'. Second error is the list is incomplete, it seems that the query is running on data up to a 211 rows and is missing some of the geographical regions. Do you have an idea on how I could fix these 2 issues?

Many thanks
 
Upvote 0
That's weird: I get 397 unique Area Names with the same query code.

The whole reason why the Remove Errors step is there is because of all the "z" -values in the columns where the data type is changed to numbers. My code only removes errors from the "Conception rate per 1,000 women in age group" column because all the errors seemed to be on the same rows. But if you want to make sure all the rows with errors will be deleted you can change replace the original "Removed Errors" row in the code with
Power Query:
#"Removed Errors" = Table.RemoveRowsWithErrors(#"Pivoted Column"),
Note the comma at the end. The commas are needed in the code after every step but the very last one.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

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