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
prateekraina
Memorable Member
Memorable Member

Need auto increment column based on condition

Hi Guys,

 

I have this requirement where i need to generate auto inrement numbers based on a condition.
Below table explains it all. I have tried using Index column in Query Editor but was not able to get the requried result.
I require the solution in Power Query M, any Leads would be appreciated.

   What i getWhat i require
XYZIndexIndex
nullnullnull1null
nullnullnull2null
12531
nullnullnull4null
nullnullnull5null
31162
26873


Prateek Raina

1 ACCEPTED SOLUTION

Nearly 🙂

 

Here comes the full code with sample data:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WyivNyVHSQaFidXAKGwI5RkBsik8RDmFjIMcQjEE8kClmQGyhFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [X = _t, Y = _t, Z = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"X", Int64.Type}, {"Y", Int64.Type}, {"Z", Int64.Type}}),
    FirstIndex = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Filtered Rows" = Table.SelectRows(FirstIndex, each ([Z] <> null)),
    SecondIndex = Table.AddIndexColumn(#"Filtered Rows", "NewIndex", 1, 1),
    #"Merged Queries" = Table.NestedJoin(FirstIndex,{"Index"},SecondIndex,{"Index"},"NewColumn",JoinKind.LeftOuter),
    #"Removed Columns" = Table.RemoveColumns(#"Merged Queries",{"Index"}),
    #"Expanded NewColumn" = Table.ExpandTableColumn(#"Removed Columns", "NewColumn", {"NewIndex"}, {"Index"})
in
    #"Expanded NewColumn"

If you have trouble dealing with it, please watch this video: http://community.powerbi.com/t5/Webinars-and-Video-Gallery/Power-BI-Forum-Help-How-to-integrate-M-co...

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

7 REPLIES 7
ImkeF
Super User
Super User

What you get is a good start.

Next filter that table on the rows where you want to apply your final Index to (in this case: Filter out rows with null).

Then create a new Index on that result and merge that table back with the previous step (where all rows where still in) on the old Index-column as key.

Delete the old Index column and expand the new one.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF,

 

I have applied final index and then created new index on that result. However, i do not know how to merge that result with previuos step. All i can see is that we can merge two different tables and not query steps. Is there anything which i am missing.

 

Kindly let me know.

 

Prateek Raina

Pls check if this vid helps: https://www.youtube.com/watch?v=looCm3cbINw

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF,

 

I understood what the video has showcased, but in my scenario its not helping because of following reasons:
What i have done:
Step 1:  I added Index column. It creates a step named say "Added Index"

What i have done post your answer:

Step 2: I have filtered the null rows. It creates a step named say "Filtered Rows"
Step 3: Now in generate the Index column again. Step name "Added NewIndex"
Step 4: Now i do self merge on OldIndex  and it creates a step say "Merged Queries"
Power Query generated is:

= Table.NestedJoin(#"Added NewIndex",{"Index"},#"Added NewIndex",{"NewIndex"},"NewColumn",JoinKind.LeftOuter)


Step 5: Now, as per you i should merge with Step 1 in which all my rows were present, so when i edit the above M Query to replace  #"Added NewIndex" with #"Added Index"
When i do this "NewIndex" column gives error since it did not exist in that step.


Please let me know if i am on the right track or not.

 

Prateek Raina

 

Nearly 🙂

 

Here comes the full code with sample data:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WyivNyVHSQaFidXAKGwI5RkBsik8RDmFjIMcQjEE8kClmQGyhFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [X = _t, Y = _t, Z = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"X", Int64.Type}, {"Y", Int64.Type}, {"Z", Int64.Type}}),
    FirstIndex = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Filtered Rows" = Table.SelectRows(FirstIndex, each ([Z] <> null)),
    SecondIndex = Table.AddIndexColumn(#"Filtered Rows", "NewIndex", 1, 1),
    #"Merged Queries" = Table.NestedJoin(FirstIndex,{"Index"},SecondIndex,{"Index"},"NewColumn",JoinKind.LeftOuter),
    #"Removed Columns" = Table.RemoveColumns(#"Merged Queries",{"Index"}),
    #"Expanded NewColumn" = Table.ExpandTableColumn(#"Removed Columns", "NewColumn", {"NewIndex"}, {"Index"})
in
    #"Expanded NewColumn"

If you have trouble dealing with it, please watch this video: http://community.powerbi.com/t5/Webinars-and-Video-Gallery/Power-BI-Forum-Help-How-to-integrate-M-co...

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF,

 

Thank you so much for the help.

 

Prateek Raina

Hi @ImkeF,

 

Thanks for the prompt response I will try this approach and share the result soon.

Prateek Raina

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.