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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
sromondas
Frequent Visitor

Mark first occurrence of a value in a column

Hi,

I have a table that is as follows. The table is first sorted by "Class" (asc) and then by Date (asc)

ClassDate
A

1 Jan 2023

A2 Jan 2023
A3 Jan 2023
B1 Jan 2023
B1 Feb 2023
B3 Feb 2023
C4 May 2023
C5 May 2023
C20 May 2023
C21 May 2023

 

What I want to do is, for the first row of every "Class", I want to mark it as a "Yes" in a new column (NewColumn) as shown below:

 

ClassDateNewColumn
A

1 Jan 2023

Yes

A2 Jan 2023 
A3 Jan 2023 
B1 Jan 2023Yes
B1 Feb 2023 
B3 Feb 2023 
C4 May 2023Yes
C5 May 2023 
C20 May 2023 
C21 May 2023 

 

I'd like to perform this during the transformation steps using PowerQuery. Appreciate any and all the help on this. Thanks!

2 REPLIES 2
BIswajit_Das
Super User
Super User

Hello @sromondas You can also use a calculated column to acheive what you need
I.e

NewColumn =
VAR _class =
    CALCULATE(
        MIN('XOX2'[Date]),
        ALLEXCEPT('XOX2', 'XOX2'[Class])
    )
RETURN
    IF('XOX2'[Date] = _class, "YES", BLANK())
c1.PNG
Dhairya
Super User
Super User

Hey @sromondas 
Please paste this below-mentioned code in your Advanced Query Editor, you will get the expected output

 


let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJU8ErMUzAyMDJWitWBCBlhChmjCjlhaoQIuaUmoQoZowo5A4VMFHwTK1GFTDGFjAywiBkiicUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Class = _t, Date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Class", type text}, {"Date", type date}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Class", Order.Ascending}, {"Date", Order.Ascending}}),
shiftedList = {null} & List.RemoveLastN(#"Sorted Rows"[Class],1),
custom1 = Table.ToColumns(Source) & {shiftedList},
custom2 = Table.FromColumns(custom1,Table.ColumnNames(Source) & {"Next Row"}),
#"Added Custom" = Table.AddColumn(custom2, "First Occurence of Class?", each if [Class] = [Next Row] then "No" else "Yes"),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Next Row"})
in
#"Removed Columns"

Dhairya_0-1688542374752.png

If this helps you please mark my solution as accepted so that others can find this quickly when they encounter similar issue. Thank you!

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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