Forum Discussion

booknerd_87's avatar
booknerd_87
Copper Contributor
Jun 16, 2022
Solved

Auto populating new rows of data based on existing rows of data

Hello,

 

I'm trying to figure out how to auto populate rows of data based on existing data. In the example I have, I have a list of Unique Campaign Names that is dynamically updated from another reporting sheet as more campaigns are added to it. Next to this list, I also have a list of all the months. I want to be able to take the list of campaigns and combine it with the months so that each campaign has a corresponding month (see screenshot). Does anyone know how to do this, and how do I make sure it's done dynamically so that as campaigns are added to that first column, months are added alongside it? Any help is appreciated! I have Office 365 if that helps as well. 

 

Beginning Data:

 

End Data:

 

 

  • booknerd_87 

    i

    There are multiple ways to do this: Using a macro; using a Lamda helper function with Lambda; using the new text manipulation functions available to 365 Insider Beta users; using Power Queary; and a bunch of other methods as well. 

     

    Power Query is incredibly powerful, and very easy to pick up. given an example. I hope this helps.

     

    1. Name the Campaign Table: tEvents
    2. Name the Month Table: tMonths
    3. Add both tables to Power query:
    4. Click in tEvent> Data tab> From Table or Range.
    5. In the Power Queary Editor file tab> Close and Load> Close and Load to> Only create connection.
    6. Right click in tMonth> Get Data From Table or Range.
    7. In the list of queries in the left section of the PQ Editor, click tEvents to select it, Add Column tab> Conditional Column> 
      1. New Column Name: Column 1
      2. Column Name: Event
      3. Operator: equals
      4. Value: null
      5. Output: 0
      6. Else: 1
    8. Click OK.

    A new Custom column with 0 is added to the tEvent table.

    1. Click on tMonths, Transform tab>Transpose

    The table rows become columns with the names Column1-12

    1. with tMonths still active, Add column tab> Index Column> From 0

    An Index column is added on the far right of tMonths with the row value 0.

    1. Select tEvents, click the Custom column’s field header to highlight the column, then click on the ABC123 icon in the header> Decibel Number

    ABC123 icon changes to 1.2 denoting the columns data type is now a Decimal Number.

    1. Home tab> Merge Quearies as New
    2. In the Merge window select the Custom column by clicking on the column header.
    3. Select tMonth from the pull-down menu and scroll to the far right, select the Index column.
    4. Keep "Left Outer" in the Join pull down below> click OK.

    A new table called Merge1 is created with both columns form tEvent and a new tMonth column with Table listed as the value of each row. 

    1. tMonth column, Click the double arrow filter icon on the right side of the row header> Deselect Index and the Use Column name boxes > OK

    tMerge has 12 new columns with a single month as the values of each row. 

    1. Select the Custom Colum by clicking in the header, right click the header> Remove
    2. Select the Event column by clicking in the header, right click the header> Unpivot other columns.

    The Month columns are now 12 rows for each event.

    1. Select and remove the Attribute column.
    2. Click the ABC123 icon in the Value column header> Text
    3. (Optional) Double click the column name value to replace with "Month" 
    4. (Optional) Double click "Merge1" in the queries sidebar and rename to "tEventMonths"
    5. File tab> Close and Load to > Only create connection > OK

    dYou’ll be returned to Excel. a sidebar on the right called @ueries and Connections with a list of the Power Query tables.

    1. right click tMerge (or tEventsMonths)> Load to> Table> Create new worksheet > OK
    2. The table is added to a new worksheet named Merge1 (or tEventMonth)

     

    When new Campaigns are added to the tEvents table.

    1. Data tab> Refresh All

3 Replies

  • AmarshallSF's avatar
    AmarshallSF
    Copper Contributor

    booknerd_87 

    i

    There are multiple ways to do this: Using a macro; using a Lamda helper function with Lambda; using the new text manipulation functions available to 365 Insider Beta users; using Power Queary; and a bunch of other methods as well. 

     

    Power Query is incredibly powerful, and very easy to pick up. given an example. I hope this helps.

     

    1. Name the Campaign Table: tEvents
    2. Name the Month Table: tMonths
    3. Add both tables to Power query:
    4. Click in tEvent> Data tab> From Table or Range.
    5. In the Power Queary Editor file tab> Close and Load> Close and Load to> Only create connection.
    6. Right click in tMonth> Get Data From Table or Range.
    7. In the list of queries in the left section of the PQ Editor, click tEvents to select it, Add Column tab> Conditional Column> 
      1. New Column Name: Column 1
      2. Column Name: Event
      3. Operator: equals
      4. Value: null
      5. Output: 0
      6. Else: 1
    8. Click OK.

    A new Custom column with 0 is added to the tEvent table.

    1. Click on tMonths, Transform tab>Transpose

    The table rows become columns with the names Column1-12

    1. with tMonths still active, Add column tab> Index Column> From 0

    An Index column is added on the far right of tMonths with the row value 0.

    1. Select tEvents, click the Custom column’s field header to highlight the column, then click on the ABC123 icon in the header> Decibel Number

    ABC123 icon changes to 1.2 denoting the columns data type is now a Decimal Number.

    1. Home tab> Merge Quearies as New
    2. In the Merge window select the Custom column by clicking on the column header.
    3. Select tMonth from the pull-down menu and scroll to the far right, select the Index column.
    4. Keep "Left Outer" in the Join pull down below> click OK.

    A new table called Merge1 is created with both columns form tEvent and a new tMonth column with Table listed as the value of each row. 

    1. tMonth column, Click the double arrow filter icon on the right side of the row header> Deselect Index and the Use Column name boxes > OK

    tMerge has 12 new columns with a single month as the values of each row. 

    1. Select the Custom Colum by clicking in the header, right click the header> Remove
    2. Select the Event column by clicking in the header, right click the header> Unpivot other columns.

    The Month columns are now 12 rows for each event.

    1. Select and remove the Attribute column.
    2. Click the ABC123 icon in the Value column header> Text
    3. (Optional) Double click the column name value to replace with "Month" 
    4. (Optional) Double click "Merge1" in the queries sidebar and rename to "tEventMonths"
    5. File tab> Close and Load to > Only create connection > OK

    dYou’ll be returned to Excel. a sidebar on the right called @ueries and Connections with a list of the Power Query tables.

    1. right click tMerge (or tEventsMonths)> Load to> Table> Create new worksheet > OK
    2. The table is added to a new worksheet named Merge1 (or tEventMonth)

     

    When new Campaigns are added to the tEvents table.

    1. Data tab> Refresh All
    • booknerd_87's avatar
      booknerd_87
      Copper Contributor
      Thank you both so much! I actually ended up taking a different direction here, but I do like the Power Query option and will keep that in mind if/when I decide to go that route. Power Query is new to me, but it seems like it has some great functions I should take advantage of. Thank you again!
  • booknerd_87 

    Sub campaign()
    
    Dim maxrow As Integer
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    
    Range("F:G").Clear
    
    maxrow = Range("B" & Rows.Count).End(xlUp).Row
    
    k = 2
    For j = 2 To maxrow
    For i = 2 To 13
    
    Cells(k, 6).Value = Cells(j, 2).Value
    Cells(k, 7).Value = Cells(i, 3).Value
    
    k = k + 1
    
    Next i
    Next j
    
    End Sub

    Maybe with these lines of code. In the attached file you can click the button in cell I2 to start the macro.

Resources