Forum Discussion

Cul33's avatar
Cul33
Frequent Visitor
8 years ago
Solved

Assigning values to increments in a loop

I have a column of data comprising blocks of 3 entries.  Each block of 3 is unique and I need to add a new column which contains n an incremental value for each of the entries within each block as A, B, C (or 1, 2, 3) then start again at the next unique block.

 

e.g. there could be 3 entries labelled "car" followed by another 3 entries labelled "bus". I want to assign a value of A in the new column to the first entry of car, B to the second entry, C to the third - then repeat the cycle from A for bus.

 

Any advice on the best way to accompish this would be greatly appreciated.

  • Hi Cul33

     

    In Power query, I would

     

    1. Add an index to the core table
    2. take a copy of the table.
    3. group the copied table by the first three columns - adding a MIN aggregation column
    4. Merge the grouped table back to the original table including the new [offset] column
    5. create a NEW column that subtracts the offset from the index to generate a number that can be converted to ABC
    6. remove the interum columns. 

     

5 Replies

  • Phil_Seamark's avatar
    Phil_Seamark
    Microsoft Employee
    HI there, Can you please post a small sample of your data so we can suggest some calculations. A mock up would be fine.
    • Phil_Seamark's avatar
      Phil_Seamark
      Microsoft Employee
      Do you have any other columns in your table that can help split the ties into 1,2 & 3?
    • Cul33's avatar
      Cul33
      Frequent Visitor

      Hi Phil

       

      Thanks for the reply.  

       

      Below is a sample (which is extracted from the preliminary stages of transformation steps, hence the data types are not applied).

       

      Each value corresponds to a heading that I want to create, which could be A, B or C.   So for the first block of 3 entries, in rows 67 to 69 -  the first entry for period Jan 2015 to Dec 2015 I want to be able to assign a value of "A" in an adjacent column, then for the second entry  a value of "B" and the third a value of "C".  When a change in Period is identifed, i.e. in row 70, I am wanting to return a value of A again in the adjacent and then to loop through to C - which would repeat for several thousand rows

      .

       

      I hope this makes sense.

      • Phil_Seamark's avatar
        Phil_Seamark
        Microsoft Employee

        Hi Cul33

         

        In Power query, I would

         

        1. Add an index to the core table
        2. take a copy of the table.
        3. group the copied table by the first three columns - adding a MIN aggregation column
        4. Merge the grouped table back to the original table including the new [offset] column
        5. create a NEW column that subtracts the offset from the index to generate a number that can be converted to ABC
        6. remove the interum columns.