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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mbuhary
Helper I
Helper I

Power Query - Check if a Text is appearing for the 1st time based on Date/Time column

Hello,

 

I have a database with following columns among many other columns:

- Names; and

- Date (with Time)

 

What I would like to do is to create a custom column where I would like identify if a name is appearing for the 1st time in the database. If so give a value of 1, otherwise make it blank.

 

We need to accomplish this by checking the "Name" in current record, against all other records whihc has a Date/Time, prior to the current record.

 

An example of what I am trying to achieve:

 

Namesss     Date/Time     CusomColumn

Names A     04 Jan 18       Null / 0

Names B     03 Jan 18       1

Names C     02 Jan 18       1

Names A     01 Jan 18       1

Names D     01 Jan 18       Null / 0

Names D     02 Jan 17       Null / 0

Names D     01 Jan 17       1

1 ACCEPTED SOLUTION
v-yuezhe-msft
Employee
Employee

@mbuhary,

Add a blank query in Power BI Desktop, paste the following code into Advanced Editor of the blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTS1WcFTSUTLRN9Q3MjC0UIrVgQk7AYWNMYWdgcJGmMIgQwwxhV1wC0MNMceuGigcCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Names = _t, #"Date/Time" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Names", type text}, {"Date/Time", type date}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Date/Time", "Date/Time - Copy"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Duplicated Column",{{"Date/Time - Copy", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Date/Time - Copy", "number"}}),
    Partition = Table.Group(#"Renamed Columns", {"Names"}, {{"Partition", each Table.AddIndexColumn(Table.Sort(_,{{"number", Order.Ascending}}), "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Names", "Date/Time", "number", "Index"}, {"Partition.Names", "Partition.Date/Time", "Partition.number", "Partition.Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Partition", "Custom", each if [Partition.Index]=1 then 1 else null)
in
    #"Added Custom"

Capture.PNG


Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-yuezhe-msft
Employee
Employee

@mbuhary,

Add a blank query in Power BI Desktop, paste the following code into Advanced Editor of the blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTS1WcFTSUTLRN9Q3MjC0UIrVgQk7AYWNMYWdgcJGmMIgQwwxhV1wC0MNMceuGigcCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Names = _t, #"Date/Time" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Names", type text}, {"Date/Time", type date}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Date/Time", "Date/Time - Copy"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Duplicated Column",{{"Date/Time - Copy", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Date/Time - Copy", "number"}}),
    Partition = Table.Group(#"Renamed Columns", {"Names"}, {{"Partition", each Table.AddIndexColumn(Table.Sort(_,{{"number", Order.Ascending}}), "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Names", "Date/Time", "number", "Index"}, {"Partition.Names", "Partition.Date/Time", "Partition.number", "Partition.Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Partition", "Custom", each if [Partition.Index]=1 then 1 else null)
in
    #"Added Custom"

Capture.PNG


Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you so much.

 

I was expecting there would be a function in this regard.

 

Anyways I will try to implement this Partition thing in my database.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.

Top Solution Authors
Top Kudoed Authors