Search code examples
powerbidaxinterpolation

Repeat the last value over time


I have a table with power plant capacities in different years. There are only entries when something changes in the capacities. In the years not listed, the last value applies.

Plant Year Capacity
Cottam 2003 800
Cottam 2009 600
Cottam 2015 800
Drax 2000 600
Drax 2005 1200
Drax 2010 1800
Drax 2013 1200
Drax 2020 0
Ironbridge 2007 500
Ironbridge 2015 0

Now I would like to transform the initial table, so that I also have values for all years in between and can display them in a stacked column chart, for example. The result should look like shown in the table below. Marked in yellow are the numbers from the initial table.

enter image description here


Solution

  • Here's how to solve this (more easily) in DAX:

    1. Prerequisite is separate Calendar table with a 1:many relation on the year
    Calendar = 
    SELECTCOLUMNS(
       GENERATESERIES(
           MIN(Plants[Year]),
           MAX(Plants[Year])
       ),
       "Year", [Value]
    )
    
    1. Next calculate the Last Given Capacity per year
    Last Given Capacity = 
    VAR current_year = 
        MAX(Calendar[Year])
    VAR last_capacity_year =
        CALCULATE(
            MAX(Plants[Year]),
            'Calendar'[Year] <= current_year
        )
    RETURN
        CALCULATE(
            MAX(Plants[Capacity]),
            Calendar[Year] = last_capacity_year
        )
    
    1. Finally put it all together in a Stacked Column Chart with
    X-axis: 'Calendar'[Year]
    Y-axis: [Last Given Capacity]
    Legend: 'Plants'[Plant]
    

    enter image description here