Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Cul33
Frequent Visitor

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.

1 ACCEPTED SOLUTION

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. 

 

offset.png


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

View solution in original post

5 REPLIES 5
Phil_Seamark
Employee
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.

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

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

.

Capture.PNG

 

I hope this makes sense.

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. 

 

offset.png


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Thanks very much Phil - greatly appreciated. That addressed the issue.

 

If this were being done in Excel it woud lend itself well to an If function where, if the period is the same add 1 and if it changes return a value of 1 again. That would depend on the first entry having a value of 1 of course.   A nested If / VLookup / Match Index could then return the required Heading according to the value returned.  

 

I wonder therefore if there is a way to accompish this with DAX rather than creating a table?

Do you have any other columns in your table that can help split the ties into 1,2 & 3?

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.