Apply text to first row of every group of rows

farris

New Member
Joined
Jul 18, 2013
Messages
5
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.
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,177
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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
Joined
Jul 18, 2013
Messages
5
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
Joined
May 2, 2008
Messages
35,177
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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?
 

Watch MrExcel Video

Forum statistics

Threads
1,102,859
Messages
5,489,309
Members
407,686
Latest member
Chuck1960

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top