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
WencyREN
Frequent Visitor

Flag first occurrence item in Power Query

Dear, I have a sample dataset as shown below, could you please instruct me on how to add a custom column to flag the first occurrence of the product type (as 1, else as 0) in the Power Query environment? The [flag first occurrence of the product type] column below is the expected outcome.

Thank you in advance!

 

ProductXXXXXXXXX[flag first occurrence of the product type]
AXXXXXXXXX1
AXXXXXXXXX0
AXXXXXXXXX0
BXXXXXXXXX1
CXXXXXXXXX1
CXXXXXXXXX0
DXXXXXXXXX1
DXXXXXXXXX0
BXXXXXXXXX0
CXXXXXXXXX0
DXXXXXXXXX0
1 ACCEPTED SOLUTION
Jakinta
Solution Sage
Solution Sage

This can help.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUYqIiEAhY3VIF3fCIe5MorgLieKk2ovVnFgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, XXX = _t, XXX.1 = _t, XXX.2 = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each List.PositionOf( Source[Product], [Product]) ),
    #"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", 0, 1, Int64.Type),
    #"Added Custom1" = Table.AddColumn(#"Added Index", "Flag", each Number.From([Custom]=[Index])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Index"})
in
    #"Removed Columns"

 

 

View solution in original post

5 REPLIES 5
mikekim123
Frequent Visitor

is it possible to get the last occurence flag using any similar method

arnabmit
Helper I
Helper I

Hi @v-eqin-msft @Jakinta 

 

I have a similar query; I am trying to flag the first occurrence of a combination of values from two different columns.

 

Thanks for any guidance!

 

ProductMonthFirst Occurrence
A11
A10
A21
B11
C11
C21
D11
D10
B10
C20
D21

Try the following code with replacing the Source step with your query.

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ToText = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Month", type text}}),
    List = List.Transform( List.Zip ( {ToText[Product], ToText[Month]} ),  Text.Combine),
    #"Added Custom" = Table.AddColumn(ToText, "Custom", each List.PositionOf( List, [Product]&[Month])),
    #"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", 0, 1, Int64.Type),
    #"Added Custom1" = Table.AddColumn(#"Added Index", "First Occurrence", each Number.From( [Custom]=[Index] )),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Index"})
in
    #"Removed Columns"

Result

Jakinta_0-1656548124526.png

 

v-eqin-msft
Community Support
Community Support

Hi @WencyREN ,

 

Please firstly try @Jakinta 's method.

 

My workaround is adding Index column and using "Group by":

Eyelyn9_0-1653981122401.png

You could create a blank query and paste the following M syntax to Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUYqIiEAhY3VIF3fCIe5MorgLieKk2ovVnFgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, A = _t, B = _t, C = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"A", type text}, {"B", type text}, {"C", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Grouped Rows" = Table.Group(#"Added Index", {"Product"}, {{"Count", each _, type table [Product=nullable text, A=nullable text, B=nullable text, C=nullable text, Index=number]}, {"Min", each List.Min([Index]), type number}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"A", "B", "C", "Index"}, {"A", "B", "C", "Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Count", "Custom", each if [Index]=[Min] then 1 else 0),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Min"})
in
    #"Removed Columns"

Output:

Eyelyn9_1-1653981236687.png

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Jakinta
Solution Sage
Solution Sage

This can help.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUYqIiEAhY3VIF3fCIe5MorgLieKk2ovVnFgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, XXX = _t, XXX.1 = _t, XXX.2 = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each List.PositionOf( Source[Product], [Product]) ),
    #"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", 0, 1, Int64.Type),
    #"Added Custom1" = Table.AddColumn(#"Added Index", "Flag", each Number.From([Custom]=[Index])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Index"})
in
    #"Removed Columns"

 

 

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