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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
RingoMoon
Frequent Visitor

Create a calculated column for prior year

Let's say I have this table below

EventYearRevenue

Event A

202010000
Event A20218000
Event A202211000
Event A20245000

 

Notice that there is no revenue for 2023. I want to create a new calculated column called "Prior Year". The logic for the prior year column would be based on whether that prior year sales is blank or not. If the prior year sales is not blank then return that as the prior year, otherwise if it is blank then go back another year until a non blank year is found. So in the scenario above, the year 2024 shoud have 2022 as its prior year due to the fact that 2023 has no revenue. So my output table would look like below.

 

EventYearRevenuePrior Year
Event A202010000blank
Event A202180002020
Event A2022110002021
Event A202450002022

 

How do I achieve this?

3 ACCEPTED SOLUTIONS
bhanu_gautam
Super User
Super User

@RingoMoon , Try using below method create a new calculated column, replace column and table name 

 


Prior Year =
VAR CurrentYear = [EventYearRevenue]
VAR PriorYearRevenue =
CALCULATE(
MAX([EventYearRevenue]),
FILTER(
ALL('TableName'),
[EventYearRevenue] <> BLANK() && [EventYearRevenue] < CurrentYear
)
)
RETURN
IF(ISBLANK(PriorYearRevenue), BLANK(), PriorYearRevenue)





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

Alex87
Solution Supplier
Solution Supplier

This should do the trick

 

 

Prior Year = 
VAR _CurrentYear = SolutionAlex[Year]
VAR _PriorYear = _CurrentYear - 1
VAR _FindYear = 
    CALCULATE(
        MAX(SolutionAlex[Year]),
        SolutionAlex[Year] < _CurrentYear,
        NOT(ISBLANK(SolutionAlex[Revenue]))
    )
VAR _Value =  CALCULATE(SUM(SolutionAlex[Revenue]), ALL(SolutionAlex), SolutionAlex[Year] = _FindYear)
VAR _Result = 
IF(
    ISBLANK(_Value),
    BLANK(),
    _Value
)
RETURN
_FindYear

 

If you want the Prior Year, then return _FindYear as in my DAX, if you want the associated revenues, return _Result.

If it answers your query, please mark my reply as the solution. Thanks!

 

Don't forget to change the table name 'SolutionAlex' with your tableName

Alex87_0-1715075692526.png

 

 

View solution in original post

MFelix
Super User
Super User

Hi @RingoMoon ,

 

You can do it in Power Query or using dax.

Power Query:

  • Sort the table by event and by year
  • Add the following column
try
  if #"Added Index"{[Index]-1}[Event] = [Event]
then
 #"Added Index"{[Index]-1}[Revenue] else null

otherwise 
null

MFelix_0-1715075301747.png

DAX

Add the following column:

VAR temp_table = SUMMARIZECOLUMNS(
		'Table'[Event],
		'Table'[Year],
		'Table'[Revenue]
	)
	RETURN

		SELECTCOLUMNS(
			OFFSET(
				-1,

				temp_table,
				ORDERBY('Table'[Year]),
				,
				PARTITIONBY('Table'[Event])
			),
			'Table'[Revenue]
		)

MFelix_2-1715075711312.png

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

3 REPLIES 3
MFelix
Super User
Super User

Hi @RingoMoon ,

 

You can do it in Power Query or using dax.

Power Query:

  • Sort the table by event and by year
  • Add the following column
try
  if #"Added Index"{[Index]-1}[Event] = [Event]
then
 #"Added Index"{[Index]-1}[Revenue] else null

otherwise 
null

MFelix_0-1715075301747.png

DAX

Add the following column:

VAR temp_table = SUMMARIZECOLUMNS(
		'Table'[Event],
		'Table'[Year],
		'Table'[Revenue]
	)
	RETURN

		SELECTCOLUMNS(
			OFFSET(
				-1,

				temp_table,
				ORDERBY('Table'[Year]),
				,
				PARTITIONBY('Table'[Event])
			),
			'Table'[Revenue]
		)

MFelix_2-1715075711312.png

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Alex87
Solution Supplier
Solution Supplier

This should do the trick

 

 

Prior Year = 
VAR _CurrentYear = SolutionAlex[Year]
VAR _PriorYear = _CurrentYear - 1
VAR _FindYear = 
    CALCULATE(
        MAX(SolutionAlex[Year]),
        SolutionAlex[Year] < _CurrentYear,
        NOT(ISBLANK(SolutionAlex[Revenue]))
    )
VAR _Value =  CALCULATE(SUM(SolutionAlex[Revenue]), ALL(SolutionAlex), SolutionAlex[Year] = _FindYear)
VAR _Result = 
IF(
    ISBLANK(_Value),
    BLANK(),
    _Value
)
RETURN
_FindYear

 

If you want the Prior Year, then return _FindYear as in my DAX, if you want the associated revenues, return _Result.

If it answers your query, please mark my reply as the solution. Thanks!

 

Don't forget to change the table name 'SolutionAlex' with your tableName

Alex87_0-1715075692526.png

 

 

bhanu_gautam
Super User
Super User

@RingoMoon , Try using below method create a new calculated column, replace column and table name 

 


Prior Year =
VAR CurrentYear = [EventYearRevenue]
VAR PriorYearRevenue =
CALCULATE(
MAX([EventYearRevenue]),
FILTER(
ALL('TableName'),
[EventYearRevenue] <> BLANK() && [EventYearRevenue] < CurrentYear
)
)
RETURN
IF(ISBLANK(PriorYearRevenue), BLANK(), PriorYearRevenue)





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.