Forum Discussion

RingoMoon's avatar
RingoMoon
Frequent Visitor
2 years ago
Solved

Create a calculated column for prior year

Let's say I have this table below Event Year Revenue Event A 2020 10000 Event A 2021 8000 Event A 2022 11000 Event A 2024 5000   Notice that there is no revenue for 2...
  • bhanu_gautam's avatar
    2 years ago

    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)

  • Alex87's avatar
    2 years ago

    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

     

     

  • MFelix's avatar
    2 years ago

    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

    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]
    		)