Apply text to first row of every group of rows

farris

New Member
I am looking for a formula to apply text (in a new custom column) if it is the first row of a group of rows.

Take a look at this example. I pull Column 1 and Column 2 from a SQL server, and I want to build the custom, third column, myself.


Data:
[Column 1] [Column 2] [Custom Column]
A 10,000 First
A 15,000 (blank)
B 20,000 First
C 5,000 First
C 7,500 (blank)
C 10,000 (blank)
C 15,000 (blank)
D 1,000 First
D 10,000 (blank)
E 100,000 First
E 200,000 (blank
E 300,000 (blank)

One solution is to put the data in Excel and build an IF function (IF A2=A1;First;"") and Auto-Fill, but I don't want to switch back and forth between Power View and Excel tables (import en export) because I think that will cost performance.
If it's possible in Power Query, that would be nice :D Or maybe in Power View.
 

RoryA

MrExcel MVP, Moderator
There are probably simpler ways, but here's one in PowerQuery. Hopefully the comments will allow you to adapt it to your real data:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    /* this is the key field
    want a one column table converted to a list
    so we can return the first position of the key field later */
    GroupField = Table.ToList(Table.SelectColumns(Source,{"Field1"})),

    // add an index field to the source
    WithIndex = Table.AddIndexColumn(Source,"Index"),

    /* calculated column tests if Index number is the same as the first position
    of the key value in the list created earlier */
    FirstPos = Table.AddColumn(WithIndex, "Custom", each if List.PositionOf(GroupField, [Field1]) = [Index] then "First" else null),

    // remove index column
    FinalTable = Table.RemoveColumns(FirstPos, {"Index"})
in
    FinalTable
 

farris

New Member
There are probably simpler ways, but here's one in PowerQuery. Hopefully the comments will allow you to adapt it to your real data:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    /* this is the key field
    want a one column table converted to a list
    so we can return the first position of the key field later */
    GroupField = Table.ToList(Table.SelectColumns(Source,{"Field1"})),

    // add an index field to the source
    WithIndex = Table.AddIndexColumn(Source,"Index"),

    /* calculated column tests if Index number is the same as the first position
    of the key value in the list created earlier */
    FirstPos = Table.AddColumn(WithIndex, "Custom", each if List.PositionOf(GroupField, [Field1]) = [Index] then "First" else null),

    // remove index column
    FinalTable = Table.RemoveColumns(FirstPos, {"Index"})
in
    FinalTable
Thanks for your effort! I am struggling to put in the query. It's a query with some filter steps and it seems that your source is from an Excel table. I have to adapt your formula to an existing query which is a bit hard for me. Is this still possible with your formula?
 

RoryA

MrExcel MVP, Moderator
Yes, you can just replace the Source line with whatever table your query currently returns as output. What does your M code look like now?
 

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...
Top