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
Danielecc
Helper II
Helper II

How to create an index that reset if in a custom column change of value

Hi everyone,

 

I need to create a new column with an index or count that restar if one of my calculated column called IND_DC change of value.

I need create this on power query and not in DAX, and I hope this image could help to understand this question (because my english is very bad)  Thanks to all:

IMAGEN POWER QUERY.png

 

 

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@Danielecc here is M code to achieve this, to understand it, start a blank query -> click advanced editor and paste this code. You will see all the steps on how this solution works, you can apply the same in your actual table.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMkAiMUXwy5JqAhoZCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [IND_DC = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"IND_DC", Int64.Type}}),
    #"Added Index Current Row" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Index Previous Row" = Table.AddIndexColumn(#"Added Index Current Row", "Previous Index", 0, 1, Int64.Type),
    #"Merged Queries" = Table.NestedJoin(#"Added Index Previous Row", {"Index"}, #"Added Index Previous Row", {"Previous Index"}, "Added Index1", JoinKind.LeftOuter),
    #"Expanded Next Row Value" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"IND_DC"}, {"Next IND_DC"}),
    #"Sorted Rows Current Row Index" = Table.Sort(#"Expanded Next Row Value",{{"Index", Order.Ascending}}),
    #"Added Group Column" = Table.AddColumn(#"Sorted Rows Current Row Index", "Group", each if [IND_DC]<>[Next IND_DC] or [Next IND_DC]=null then [Index] else null, Int64.Type),
    #"Filled Up Group" = Table.FillUp(#"Added Group Column",{"Group"}),
    #"Grouped Rows1" = Table.Group(#"Filled Up Group", {"Group"}, {{"All", each Table.AddIndexColumn(_,"New Index",1,1), type table [IND_DC=nullable number, Index=number, Previous Index=number, Next IND_DC=nullable number, Group=number, New Index=Int64.Type]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows1", "All", {"IND_DC", "New Index"}, {"IND_DC", "Index"}),
    #"Removed Group Columns" = Table.RemoveColumns(#"Expanded All",{"Group"})
in
    #"Removed Group Columns"


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

8 REPLIES 8
parry2k
Super User
Super User

@Danielecc hey this video is now available here: 

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Danielecc it's all good, indeed if you subscribe you will get notified when it is out. Anyhow I always post interesting content on my channel, and I'm sure you will learn something new from it. 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Danielecc I'm glad you have a solution in place. Good for you. I'm going to do a video on this topic, it is an interesting question and will be good to do a video with all the explanations so that others can make use of it. If you are interested I will share the link here when it is ready.  



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k  of course I am interested, I dont understand that you made only I aplied fallowing your instructions, for that reason I will waitng for that link here for your video and I think it's a good idea suscribe me to your channel too. Thanks again for help to us (and sorry for my bad english 😄 ).

parry2k
Super User
Super User

@Danielecc the code I gave was just to show how it will work, you have to apply all the steps in your tables, on the expand table step you will have all the columns available from your table.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k , sorry I dont know nothing of M, then I dont understand your first reply, but I made that you said in your second post and works OK. Then I have to:

1. Accept as Solution your first post

2. Thanks a lot for your solution, time and explanation.

 

Best regards...

parry2k
Super User
Super User

@Danielecc here is M code to achieve this, to understand it, start a blank query -> click advanced editor and paste this code. You will see all the steps on how this solution works, you can apply the same in your actual table.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMkAiMUXwy5JqAhoZCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [IND_DC = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"IND_DC", Int64.Type}}),
    #"Added Index Current Row" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Index Previous Row" = Table.AddIndexColumn(#"Added Index Current Row", "Previous Index", 0, 1, Int64.Type),
    #"Merged Queries" = Table.NestedJoin(#"Added Index Previous Row", {"Index"}, #"Added Index Previous Row", {"Previous Index"}, "Added Index1", JoinKind.LeftOuter),
    #"Expanded Next Row Value" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"IND_DC"}, {"Next IND_DC"}),
    #"Sorted Rows Current Row Index" = Table.Sort(#"Expanded Next Row Value",{{"Index", Order.Ascending}}),
    #"Added Group Column" = Table.AddColumn(#"Sorted Rows Current Row Index", "Group", each if [IND_DC]<>[Next IND_DC] or [Next IND_DC]=null then [Index] else null, Int64.Type),
    #"Filled Up Group" = Table.FillUp(#"Added Group Column",{"Group"}),
    #"Grouped Rows1" = Table.Group(#"Filled Up Group", {"Group"}, {{"All", each Table.AddIndexColumn(_,"New Index",1,1), type table [IND_DC=nullable number, Index=number, Previous Index=number, Next IND_DC=nullable number, Group=number, New Index=Int64.Type]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows1", "All", {"IND_DC", "New Index"}, {"IND_DC", "Index"}),
    #"Removed Group Columns" = Table.RemoveColumns(#"Expanded All",{"Group"})
in
    #"Removed Group Columns"


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi @parry2k , thanks a lot for you answer.

I use your code adding a new step, and the counter/index works, but...I see only the first 22 rows and the real have a lot of rows. And I see only 2 columns (IND_DC and Index) but I need to see all columns (Source, Datos, IND_DC and Index). Do you know what I must change to see all rows and all 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.