Transposing columns

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
218
Office Version
  1. 2016
Platform
  1. Windows
Hello experts,

I am trying to create a pivot table from an unpivot table (pics) below:


Pivot table:
PivotTable.jpg


Unpivot table:
UnpivotTable.jpg


Using the following code:

VBA Code:
Dim db As DAO.Database
Dim rst As DAO.Recordset

Dim tbl As TableDef
Set db = CurrentDb

For Each tbl In db.TableDefs
    If tbl.Name = "T_Pivot" Then
    db.Execute "DROP TABLE T_Pivot"
    Else
    End If
Next tbl

db.Execute "CREATE TABLE T_Pivot(Stores CHAR, Cities CHAR, Period DATETIME, SalesQty INTEGER);"

Set db = CurrentDb

Set rst = db.OpenRecordset("T_Unpivot", dbOpenTable)

Dim n As Integer
Dim fieldname As String
Dim str As String

 Do
        For n = 1 To rst.Fields.Count - 1
            fieldname = rst.Fields(n).Name
            str = "Insert Into T_Pivot (Stores,Cities,Period,SalesQty) " _
            & "VALUES (" & rst![Stores] & ", " & rst![Cities] & ", " & rst![Period] & "," & rst![SalesQty] & ")"
            Debug.Print str
            Next n
            'rst.MoveNext
            db.Execute str
 Loop Until rst.EOF
End Sub

However, not sure I am not able to recognize the fields Period and SalesQty, any suggestion?

Debug.png
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
rst has you unpivot table data and therefore does not have those 2 fields. You need to reference by index or date name
 
Upvote 0
Thanks for you Patience
I went about it a different way
I opened the new table you created and from i created a single record per each dated Columns using the static fields to fill in the other information


VBA Code:
Option Compare Database

Sub DoSomething()
Dim db As DAO.Database
Dim UnPivot As DAO.Recordset
Dim Pivot As DAO.Recordset

Dim tbl As TableDef
Set db = CurrentDb

For Each tbl In db.TableDefs
    If tbl.Name = "T_Pivot" Then
        db.Execute "DROP TABLE T_Pivot"
    End If
Next tbl

db.Execute "CREATE TABLE T_Pivot(Stores CHAR, Cities CHAR, Period DATETIME, SalesQty INTEGER);"


Set Pivot = db.OpenRecordset("T_Pivot", dbOpenTable)
Set UnPivot = db.OpenRecordset("T_Unpivot", dbOpenTable)

Dim Fld As Integer
Dim str As String

 Do
    With Pivot
        For Fld = 2 To UnPivot.Fields.Count - 1
            .AddNew
            .Fields("Cities").Value = UnPivot.Fields("Cities").Value
            .Fields("Stores").Value = UnPivot.Fields("Stores").Value
            .Fields("Period").Value = DateValue(UnPivot.Fields(Fld).Name)
            .Fields("SalesQty").Value = UnPivot.Fields(Fld).Value
            .Update
        Next Fld
        
    End With
    UnPivot.MoveNext
 Loop Until UnPivot.EOF
 
 Pivot.Close
 UnPivot.Close
 Set Pivot = Nothing
 Set UnPivot = Nothing
 Set db = Nothing
 Set tbl = Nothing
End Sub
 
Upvote 0
Use Power Query to unpivot your data and then create a pivot table in Excel. No coding required. Can all be done in the U/I.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cities", type text}, {"Stores", type text}, {"6/13/2021", Int64.Type}, {"6/14/2021", Int64.Type}, {"6/15/2021", Int64.Type}, {"6/16/2021", Int64.Type}, {"6/17/2021", Int64.Type}, {"6/18/2021", Int64.Type}, {"6/19/2021", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Cities", "Stores"}, "Attribute", "Value")
in
    #"Unpivoted Other Columns"

Book1
FGHI
2StoresCitiesAttributeSum of Value
3StoreASydney6/13/2021228
46/14/20211524
56/15/2021800
66/16/20211578
76/17/20211766
86/18/20211740
96/19/2021556
10StoreBMelbourne6/13/20211504
116/14/20211876
126/15/20211012
136/16/2021744
146/17/20211608
156/18/20211818
166/19/20211346
17StoreCPerth6/13/2021950
186/14/2021246
196/15/2021601
206/16/2021365
216/17/2021115
226/18/2021953
236/19/2021470
24StoreDAdelaide6/13/2021910
256/14/2021240
266/15/2021731
276/16/2021559
286/17/2021405
296/18/2021728
306/19/2021318
31StoreEBrisbane6/13/2021534
326/14/2021637
336/15/2021389
346/16/2021264
356/17/2021785
366/18/2021449
376/19/2021541
Table1
 
Upvote 0
Use Power Query to unpivot your data and then create a pivot table in Excel. No coding required. Can all be done in the U/I.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cities", type text}, {"Stores", type text}, {"6/13/2021", Int64.Type}, {"6/14/2021", Int64.Type}, {"6/15/2021", Int64.Type}, {"6/16/2021", Int64.Type}, {"6/17/2021", Int64.Type}, {"6/18/2021", Int64.Type}, {"6/19/2021", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Cities", "Stores"}, "Attribute", "Value")
in
    #"Unpivoted Other Columns"

Book1
FGHI
2StoresCitiesAttributeSum of Value
3StoreASydney6/13/2021228
46/14/20211524
56/15/2021800
66/16/20211578
76/17/20211766
86/18/20211740
96/19/2021556
10StoreBMelbourne6/13/20211504
116/14/20211876
126/15/20211012
136/16/2021744
146/17/20211608
156/18/20211818
166/19/20211346
17StoreCPerth6/13/2021950
186/14/2021246
196/15/2021601
206/16/2021365
216/17/2021115
226/18/2021953
236/19/2021470
24StoreDAdelaide6/13/2021910
256/14/2021240
266/15/2021731
276/16/2021559
286/17/2021405
296/18/2021728
306/19/2021318
31StoreEBrisbane6/13/2021534
326/14/2021637
336/15/2021389
346/16/2021264
356/17/2021785
366/18/2021449
376/19/2021541
Table1
OP has an Access question?
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,329
Members
449,155
Latest member
ravioli44

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