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
PhillipC1
Helper I
Helper I

How to add a custom column to show if the category went up, down, or no change

Hello,

I have a folder connected to Power BI that is stacking weekly files that come in each week that is shown below, to which we want to add a custom column in power bi that will tell us if this person's category went up, down, or had no change.

 

IDStatusWeekScore_GroupCurrent ScoreCategory
12345Valid6/24/2024Clean12Authorized
45678Valid6/24/2024Clean26Moderate
64845Valid6/24/2024Clean50High
55848Valid6/24/2024Clean48High
12345Valid7/1/2024Clean9Authorized
45678Valid7/1/2024Clean54High
64845Valid7/1/2024Clean50High
55848Valid7/1/2024Clean0Authorized
12345Valid7/8/2024Clean24Moderate
45678Valid7/8/2024Clean4Authorized
64845Valid7/8/2024Clean54High
55848Valid7/8/2024Clean26Moderate
12345Valid7/15/2024Clean8Authorized
45678Valid7/15/2024Clean10Authorized
64845Valid7/15/2024Clean48High
55848Valid7/15/2024Clean52High

 

Essentially we would like the table in power bi to add this column to make it show what the change is from the prior week. This will allow us to then query how many times a person has a shift up or down. 

 

IDStatusWeekScore_GroupCurrent ScoreCategoryChange
12345Valid6/24/2024Clean12AuthorizedNo Change
45678Valid6/24/2024Clean26ModerateNo Change
64845Valid6/24/2024Clean50HighNo Change
55848Valid6/24/2024Clean48HighNo Change
12345Valid7/1/2024Clean9AuthorizedNo Change
45678Valid7/1/2024Clean54HighUp
64845Valid7/1/2024Clean50HighNo Change
55848Valid7/1/2024Clean0AuthorizedDown
12345Valid7/8/2024Clean24ModerateUp
45678Valid7/8/2024Clean4AuthorizedDown
64845Valid7/8/2024Clean54HighNo Change
55848Valid7/8/2024Clean26ModerateUp
12345Valid7/15/2024Clean8AuthorizedDown
45678Valid7/15/2024Clean10AuthorizedNo Change
64845Valid7/15/2024Clean48HighNo Change
55848Valid7/15/2024Clean52HighUp

 

The flow of change should go Authorized -> Moderate -> High meaning if week 1 I am authorized and in week 2 I move to Moderate or High that is Up where as if I am moderate in week 1 and move to authorized in week 2 then that is down. 

 

I hope this is explained clearly, but please let me know if there is anything additional I can clarify on to address this. 

2 ACCEPTED SOLUTIONS

Hi, @PhillipC1 

try below code for Week Number Column

Week_number = 
var a = WEEKNUM('Table'[Week],1)
var currentYear = YEAR('Table'[Week])
var Current_id = 'Table'[ID] 
var Previousweek = MAXX(FILTER('Table',YEAR('Table'[Week])<currentYear && 'Table'[ID]=Current_id ),WEEKNUM('Table'[Week],1))
var final = IF(ISBLANK(Previousweek),a,Previousweek+a)
RETURN
final



The Measure is same as Above

Measure 2 = 
VAR a = MAXX(
		FILTER(
			ALL(
				'Table'[ID],
				'Table'[Week_number]
			),
			'Table'[ID] = MIN('Table'[ID]) && 'Table'[Week_number] < MIN('Table'[Week_number])
		),
		[Week_number]
	)
	VAR b = IF(
		ISBLANK(a),
		MIN('Table'[Week_number]),
		a
	)
	VAR c = MINX(
		FILTER(
			ALL(
				'Table'[ID],
				'Table'[Category],
				'Table'[Week_number]
			),
			'Table'[ID] = MIN('Table'[ID]) && 'Table'[Week_number] = b
		),
		'Table'[Category]
	)
	VAR c1 = SWITCH(
		c,
		"Authorized", 1,
		"Moderate", 2,
		"High", 3
	)
	VAR d = SWITCH(
		MIN('Table'[Category]),
		"Authorized", 1,
		"Moderate", 2,
		"High", 3
	)

	RETURN
		SWITCH(
			TRUE(),
			c1 = d, "NO Change",
			c1 < d, "UP",
			"Down"
		)




Dangar332_0-1726867139131.png

You can Download file from below

Best Regards,
Dangar

 

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

So basically it compares the category for the current week with the previous weeks categoty..

VAR _NextCategory = MAX ( 'new 11_2'[PreviousCategory] )


the variable name is a misnomer.I named it wrongly, It should had been _PreviousCategory.

Maybe that was the confusion.

   _CurrentCategory = "Moderate" && _PreviousCategory = "Authorized"
|| _CurrentCategory = "High"     && _PreviousCategory = "Moderate"
|| _CurrentCategory = "High"     && _PreviousCategory = "Authorized"
|| _CurrentCategory = "Critical" && _PreviousCategory = "Moderate"
|| _CurrentCategory = "Critical" && _PreviousCategory = "High"
|| _CurrentCategory = "Critical" && _PreviousCategory = "Authorized" ,"UP"
 

I have reformatted the code and renamed the variable to _PreviousCategory.I think now it would be easy for you to adjust the formula.




Regards,
Sachin
Check out my Blog

View solution in original post

8 REPLIES 8
SachinNandanwar
Super User
Super User

@PhillipC1 

This is my attempt.The table name in my solution is new 11_2


Create two calculated columns :

 

 

Rank = RANKX (
    FILTER (
        'new 11_2',
       'new 11_2'[ID] = EARLIER ( 'new 11_2'[ID])
    ),
    'new 11_2'[Week].[Date],
    , 
    ASC,Dense
)

 

 

PreviousCategory = 
VAR i = ( 'new 11_2'[Rank] )
VAR pi =
    CALCULATE (
        MAX ( 'new 11_2'[Rank] ),
        ALLEXCEPT ( 'new 11_2', 'new 11_2'[id] ),
        'new 11_2'[Rank] < i
    )
RETURN
    CALCULATE (
        MAX ( 'new 11_2'[Category] ),
        ALLEXCEPT ( 'new 11_2', 'new 11_2'[ID] ),
        'new 11_2'[Rank] = pi
    )

 

 

 

SachinNandanwar_2-1726788700622.png

 

and then create a measure to calculate the final status.

 

 

Final_Status =
VAR _CurrentCategory = MAX ( 'new 11_2'[Category] )
VAR _NextCategory = MAX ( 'new 11_2'[PreviousCategory] )
RETURN
    SWITCH (
        TRUE (),
        _CurrentCategory = "Moderate"
            && _NextCategory = "Authorized"
            || _CurrentCategory = "High"
            && _NextCategory = "Moderate"
            || _CurrentCategory = "High"
            && _NextCategory = "Authorized", "UP",
            _NextCategory = BLANK ()
            || _NextCategory = _CurrentCategory, "NO CHANGE",
        "DOWN"
    )

 

 

SachinNandanwar_1-1726788559611.png

 



Regards,
Sachin
Check out my Blog

This looks good. If we wanted to add another category for example "Critical" which would be up from High how would we add this? So the flow would go from Authorized -> Moderate -> High -> Critical. I am a little confused by what exactly is taking place here.

 

PhillipC1_0-1726841516625.png

 

So basically it compares the category for the current week with the previous weeks categoty..

VAR _NextCategory = MAX ( 'new 11_2'[PreviousCategory] )


the variable name is a misnomer.I named it wrongly, It should had been _PreviousCategory.

Maybe that was the confusion.

   _CurrentCategory = "Moderate" && _PreviousCategory = "Authorized"
|| _CurrentCategory = "High"     && _PreviousCategory = "Moderate"
|| _CurrentCategory = "High"     && _PreviousCategory = "Authorized"
|| _CurrentCategory = "Critical" && _PreviousCategory = "Moderate"
|| _CurrentCategory = "Critical" && _PreviousCategory = "High"
|| _CurrentCategory = "Critical" && _PreviousCategory = "Authorized" ,"UP"
 

I have reformatted the code and renamed the variable to _PreviousCategory.I think now it would be easy for you to adjust the formula.




Regards,
Sachin
Check out my Blog

Got it, that works! Thank you so much.

Dangar332
Super User
Super User

Hi, @PhillipC1 

Make Week Number Column

Week_number = WEEKNUM('Table'[Week],1)


Try below Measure 

Measure 2 = 
VAR a = MAXX(
		FILTER(
			ALL(
				'Table'[ID],
				'Table'[Week_number]
			),
			'Table'[ID] = MIN('Table'[ID]) && 'Table'[Week_number] < MIN('Table'[Week_number])
		),
		[Week_number]
	)
	VAR b = IF(
		ISBLANK(a),
		MIN('Table'[Week_number]),
		a
	)
	VAR c = MINX(
		FILTER(
			ALL(
				'Table'[ID],
				'Table'[Category],
				'Table'[Week_number]
			),
			'Table'[ID] = MIN('Table'[ID]) && 'Table'[Week_number] = b
		),
		'Table'[Category]
	)
	VAR c1 = SWITCH(
		c,
		"Authorized", 1,
		"Moderate", 2,
		"High", 3
	)
	VAR d = SWITCH(
		MIN('Table'[Category]),
		"Authorized", 1,
		"Moderate", 2,
		"High", 3
	)

	RETURN
		SWITCH(
			TRUE(),
			c1 = d, "NO Change",
			c1 < d, "UP",
			"Down"
		)

 

Dangar332_0-1726779220187.png

 

Download File From below

Best Regards,
Dangar

 

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



Thank you Dangar! What happens when a new year starts with this? If week number is 1-52 eventually this will rollover into a new year and then we will get repeats in that value. 

Hi, @PhillipC1 

try below code for Week Number Column

Week_number = 
var a = WEEKNUM('Table'[Week],1)
var currentYear = YEAR('Table'[Week])
var Current_id = 'Table'[ID] 
var Previousweek = MAXX(FILTER('Table',YEAR('Table'[Week])<currentYear && 'Table'[ID]=Current_id ),WEEKNUM('Table'[Week],1))
var final = IF(ISBLANK(Previousweek),a,Previousweek+a)
RETURN
final



The Measure is same as Above

Measure 2 = 
VAR a = MAXX(
		FILTER(
			ALL(
				'Table'[ID],
				'Table'[Week_number]
			),
			'Table'[ID] = MIN('Table'[ID]) && 'Table'[Week_number] < MIN('Table'[Week_number])
		),
		[Week_number]
	)
	VAR b = IF(
		ISBLANK(a),
		MIN('Table'[Week_number]),
		a
	)
	VAR c = MINX(
		FILTER(
			ALL(
				'Table'[ID],
				'Table'[Category],
				'Table'[Week_number]
			),
			'Table'[ID] = MIN('Table'[ID]) && 'Table'[Week_number] = b
		),
		'Table'[Category]
	)
	VAR c1 = SWITCH(
		c,
		"Authorized", 1,
		"Moderate", 2,
		"High", 3
	)
	VAR d = SWITCH(
		MIN('Table'[Category]),
		"Authorized", 1,
		"Moderate", 2,
		"High", 3
	)

	RETURN
		SWITCH(
			TRUE(),
			c1 = d, "NO Change",
			c1 < d, "UP",
			"Down"
		)




Dangar332_0-1726867139131.png

You can Download file from below

Best Regards,
Dangar

 

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



 

Thank you, I tested this as well and it works. 

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.