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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Mark rows depending on previous row

Hello everyone,

 

I have currenly the following problem: I have a column A of numbers (which repeat). One of the rows with the same number has the value "1" in column B (all the others have "0"). What I want is to mark every row with the same number after the row with the value 1 with the value 2:

 

A

B AB ABC
10 10 100
10 10 100
11 11 110
10 12 102
10 12 102
10 12 102
21 21 210
20"=>"22or202
20 22 202
30 30 300
30 30 300
30 30 300
30 30 300
31 31 310
30 32 302

 

Has anyone any idea?

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

you can add a group-function and then with a Table.TransformColumns apply the necessary changes to the table.

Here the complete solution

let
	Source = #table
	(
		{"A","B"},
		{
			{"1","0"},	{"1","0"},	{"1","1"},	{"1","0"},	{"1","0"},	{"1","0"},	{"2","1"},	{"2","0"},	{"2","0"},	{"3","0"},	{"3","0"},	{"3","0"},	{"3","0"},	{"3","1"},	
			{"3","0"}
		}
	),
    Group = Table.Group(Source, {"A"}, {{"AllRowsB", each Table.SelectColumns(_, "B"), type table [A=text, B=text]}}),
	AddColumn = Table.TransformColumns
	(
		Group,
		{
			{
				"AllRowsB",
				each Table.ReplaceValue(Table.FillDown( Table.AddColumn(_,"C",(row)=> if row[B] = "1" then "2" else null), {"C"}), null, "0", Replacer.ReplaceValue, {"C"})
			}
		}
	),
    ExpandedAllRowsB = Table.ExpandTableColumn(AddColumn, "AllRowsB", {"B", "C"}, {"B", "C"})
in
    ExpandedAllRowsB

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

2 REPLIES 2
Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

you can add a group-function and then with a Table.TransformColumns apply the necessary changes to the table.

Here the complete solution

let
	Source = #table
	(
		{"A","B"},
		{
			{"1","0"},	{"1","0"},	{"1","1"},	{"1","0"},	{"1","0"},	{"1","0"},	{"2","1"},	{"2","0"},	{"2","0"},	{"3","0"},	{"3","0"},	{"3","0"},	{"3","0"},	{"3","1"},	
			{"3","0"}
		}
	),
    Group = Table.Group(Source, {"A"}, {{"AllRowsB", each Table.SelectColumns(_, "B"), type table [A=text, B=text]}}),
	AddColumn = Table.TransformColumns
	(
		Group,
		{
			{
				"AllRowsB",
				each Table.ReplaceValue(Table.FillDown( Table.AddColumn(_,"C",(row)=> if row[B] = "1" then "2" else null), {"C"}), null, "0", Replacer.ReplaceValue, {"C"})
			}
		}
	),
    ExpandedAllRowsB = Table.ExpandTableColumn(AddColumn, "AllRowsB", {"B", "C"}, {"B", "C"})
in
    ExpandedAllRowsB

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Greg_Deckler
Community Champion
Community Champion

Yeah, it seems that you want some variation of Cthulhu. https://community.powerbi.com/t5/Quick-Measures-Gallery/Cthulhu/td-p/509739

 

You will almost certainly need to add an Index column to your data, otherwise it is impossible to know which row comes before another.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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.