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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
rane19
Helper I
Helper I

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 5
BA_Pete
Super User
Super User

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:

 

rane19.PNG

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




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

 q1.PNGq1_1.PNG

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



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Pete , worked ! you are awesome . (Even I have actually stucked in the step of changing Date in power query that turns my date into 1905/7/17 e.t.c. )

amitchandak
Super User
Super User

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.