Creating Duplicate Rows Out of pivot with two extra columns

Mughees

New Member
Joined
Nov 5, 2019
Messages
4
Hi everyone,

I have a data set that is updated on regular basis by multiple users. Hence, the number of rows of this data set keeps on changing. However, the number of columns remain the same. The data set looks like following:

IDUcodeProvinceDivisionDistrictCodeDiv_IDTehsilUCUCIDNameHFAddressHFPhoneHFformalIsActiveFreqLevelTypeSectorAssignedPersonNameIDsupDaillyTurnoverDateIncludedIsFocalPersonFPNameFPPhoneHMISCodeRemarks
983014203007PUNJABSARGODHAKHUSHAB31QUAIDA ABADQUAIDA ABAD1776Mazhar Hospital Quaidabad011QuarterlySecondaryZeroPrivate12028001-01-171Faisal Aziz454920089
937114104017PUNJABSARGODHASARGODHA31SARGODHACHAK # 46 SB1644Iqra Medical complex001QuarterlyPrimaryZeroPrivateDr. Muhammad Riaz12026501-01-171Hammad Khalil3006055995
915614302013PUNJABSARGODHAMIANWALI31MAINWALIMARI INDUS1806BHu Mari Indus011QuarterlyPrimaryZeroGovernmentDR MIAN KASHIF ALI12027001-01-161Samiullah3339837516
915914302002PUNJABSARGODHAMIANWALI31MAINWALIBAN HAFAZ GI1795BHU Laloo Khel011QuarterlyPrimaryZeroGovernmentDR MIAN KASHIF ALI12026501-01-161Samiullah3339837516
915814303001PUNJABSARGODHAMIANWALI31PIPLANALOWALI1823BHU Dhoke Ayub011QuarterlyPrimaryZeroGovernmentDR MIAN KASHIF ALI12024801-01-161Samiullah3339837516
915514301010PUNJABSARGODHAMIANWALI31EISAKHAILSULTAN KHAIL1788BHU Sultan Khel011QuarterlyPrimaryZeroGovernmentDR MIAN KASHIF ALI12026501-01-161Samiullah3339837516
1023114404001PUNJABSARGODHABHAKKAR31MANKERA67 ML1872BHU 67 ML011QuarterlyPrimaryZeroGovernment12025001-01-171Faisal Gul3156849656
1023214401001PUNJABSARGODHABHAKKAR31BHAKKAR61ML1837BHU Saraye Muhajir011QuarterlyPrimaryZeroGovernment12028017-01-011Faisal Gul3156849656
1023415102001PUNJABFAISALABADFAISALABAD25FSD CITYUC 1771894ZCD 204 RB204 RB011QuarterlyPrimaryZeroGovernment1257001-07-171
1023515102025PUNJABFAISALABADFAISALABAD25FSD CITYUC 2011918ZCD MananwalaMananwala011QuarterlyPrimaryZeroGovernment12577501-07-171Dr.Iqra
1023615102032PUNJABFAISALABADFAISALABAD25FSD CITYUC 2081925GD Bolley de JhuggiBolley de Ghuggi011QuarterlyPrimaryZeroGovernment125710001-07-171Dr.Umair
1023715102055PUNJABFAISALABADFAISALABAD25FSD CITYUC 2311948GD Factory areaFactory Area,Faisalabad011QuarterlyPrimaryZeroGovernment125710001-07-171Dr.Shaisata


Please note that it is just a 2% of real data as the real data contains nearly 4000+ rows.

I needed few rows out of this database (required rows highlighted yellow in the above table) for my analysis. That is why I created a pivot table out of this data sheet to extract my desired columns. You can see pivot table below:

file.xlsx
ABCDEFGHIJK
1IsActive1
2
3IDUcodeDistrictTehsilUCNameHFFreqLevelTypeDateIncludedDaillyTurnover
4915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero01-01-1665
5915614302013MIANWALIMAINWALIMARI INDUSBHu Mari IndusQuarterlyPrimaryZero01-01-1670
6915814303001MIANWALIPIPLANALOWALIBHU Dhoke AyubQuarterlyPrimaryZero01-01-1648
7915914302002MIANWALIMAINWALIBAN HAFAZ GIBHU Laloo KhelQuarterlyPrimaryZero01-01-1665
8937114104017SARGODHASARGODHACHAK # 46 SBIqra Medical complexQuarterlyPrimaryZero01-01-1765
9983014203007KHUSHABQUAIDA ABADQUAIDA ABADMazhar Hospital QuaidabadQuarterlySecondaryZero01-01-1780
101023114404001BHAKKARMANKERA67 MLBHU 67 MLQuarterlyPrimaryZero01-01-1750
111023214401001BHAKKARBHAKKAR61MLBHU Saraye MuhajirQuarterlyPrimaryZero17-01-0180
121023415102001FAISALABADFSD CITYUC 177ZCD 204 RBQuarterlyPrimaryZero01-07-170
131023515102025FAISALABADFSD CITYUC 201ZCD MananwalaQuarterlyPrimaryZero01-07-1775
141023615102032FAISALABADFSD CITYUC 208GD Bolley de JhuggiQuarterlyPrimaryZero01-07-17100
151023715102055FAISALABADFSD CITYUC 231GD Factory areaQuarterlyPrimaryZero01-07-17100
16Grand Total
Pivot


However, the tricky part in which I got stuck is to create duplicate rows for each single row present in this pivot table. The pivot table if you can see contains health facilities and each of this health facility has a unique "ID". What I intend to add is 23 more duplicate rows for each single ID. All the data of pivot table rows remain same in the final outcome sheet, except for two new columns of "Year" and "Month" added against each row (as shown in the below table). Year will contain 2020/2021 and month will contain all 12 months. In total 24 rows need to be created against each single ID through some vba command. Simplifying it further, we currently have 12 rows in pivot table or your can say that 12 unique ID's. We need to add 23 new rows against each ID and hence, total of 24*12=288 rows will be created along with year and month column. Below image shows the outcome for a single ID.

IDUcodeDistrictTehsilUCNameHFFreqLevelTypeDateIncludedDaillyTurnoverYearMonth
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Jan
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Feb
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Mar
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Apr
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020May
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Jun
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Jul
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Aug
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Sep
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Oct
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Nov
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652020Dec
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Jan
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Feb
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Mar
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Apr
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021May
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Jun
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Jul
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Aug
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Sep
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Oct
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Nov
915514301010MIANWALIEISAKHAILSULTAN KHAILBHU Sultan KhelQuarterlyPrimaryZero42370652021Dec


Will really appreciate your support to automate the task shown in the last table.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,176,091
Messages
5,901,363
Members
434,887
Latest member
zoath

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
Top