Forum Discussion

rane19's avatar
rane19
Helper I
6 years ago

Dynamically calculate difference between columns (Multi Years)Power BI

Situation

I have been tried couple answers in power bi forum but not really fit my case as calculation should be dynamically calculate the differences between columns (Multi Years).

 

After sum up by "Matrix" table

A Measure can calculate like this :

 
 
 
Group201520162017201820192020Grand Total
A2411363010136301366
B3021024424690728926878
C2411363010136301366
D3021024424690728926878
Grand Total65243201449400172824416488

Desire Shape :

Group2015Dif vs LY2016Dif vs LY2017Dif vs LY2018Dif vs LY2019Dif vs LY2020Dif vs LYGrand Total
A24 1136-11123011061020136-126301061366
B302 1024-722429824690-46487283962926366878
C24 1136-11123011061020136-126301061366
D302 1024-722429824690-46487283962926366878
Grand Total652 4320-366814441769400-925617287672244148416488
 

 

 

Example which doesn't work :

Difference = var MaxYear = MAX(data[YEAR])
var MinYear = MIN(data[YEAR])
RETURN CALCULATE(SUM([Value]),data[YEAR]=MaxYear) - CALCULATE(SUM([Value]),data[YEAR]=MinYear)

 sample data :

#data :
Group   YEAR    Value
A   2015    12
B   2015    151
C   2015    12
D   2015    151
A   2016    568
B   2016    512
C   2016    568
D   2016    512
A   2017    15
B   2017    21
C   2017    15
D   2017    21
A   2018    5
B   2018    2345
C   2018    5
D   2018    2345
A   2019    68
B   2019    364
C   2019    68
D   2019    364
A   2020    15
B   2020    46
C   2020    15
D   2020    46
A   2015    12
B   2015    151
C   2015    12
D   2015    151
A   2016    568
B   2016    512
C   2016    568
D   2016    512
A   2017    15
B   2017    21
C   2017    15
D   2017    21
A   2018    5
B   2018    2345
C   2018    5
D   2018    2345
A   2019    68
B   2019    364
C   2019    68
D   2019    364
A   2020    15
B   2020    46
C   2020    15
D   2020    46

 

5 Replies

  • rane19 , if you want min year/max year diff, then you need first unpivot data and have data as rows

    https://radacad.com/pivot-and-unpivot-with-power-bi
    Transpose : https://yodalearning.com/tutorials/power-query-helps-transposing-data/

     


    Then you can have measure like these

    This Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])))
    Last Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])-1))

    Diff = [This Year] -[Last Year]

  • Hi rane19 ,

     

    Try these measures. The PY one is a bit verbose but it was the only way I could get the Prior Year to work:

     

    _valueTY = SUM(dTable[value])

     

    _valuePY = 
    VAR prevYear =
    DATE(
        YEAR(MAX(dTable[dtYear])) - 1,
        MONTH(MAX(dTable[dtYear])),
        DAY(MAX(dTable[dtYear]))
    )
    RETURN
    CALCULATE(
        SUM(dTable[value]),
        FILTER(
            ALLEXCEPT(dTable, dTable[group]),
            dTable[dtYear] = prevYear
        )
    )

     

    _valueDiff = [_valuePY] - [_valueTY]

     

    These measures giveme the following output when applied to a Matrix visual:

     

     

     

    Pete

    • rane19's avatar
      rane19
      Helper I

      Pete , tons of thank I believe this is the answer but i couldn't reproduce the same output as the _valueDiff columns shows minus of same _vauleTY figures .

      I believe the reason is date processing ? as your snapshot shows : 1 - dtYear and 2 - year , which is applied in Matrix table .

      Should I add 1 more step to deal with the raw YEAR column ? (like convert from value to string )

       

      DATE(
          YEAR(MAX(dTable[dtYear])) - 1,
          MONTH(MAX(dTable[dtYear])),
          DAY(MAX(dTable[dtYear]))
      )

       

      • BA_Pete's avatar
        BA_Pete
        Super User

        Hi rane19 ,

         

        Sorry, I should have included the fact that I duplicated your [Year] column in Power Query and then changed the data type to date type to create the [dtYear] column. When you implement this in Power Query, it will change all of your years to 1st January for that year, but it allows you to use the datetime functions used in the _valuePY measure.

         

        Pete