Hey everyone!
I really enjoyed this one.
I decided NOT to create multiple measures for each step and do it in one measure. I don’t do this in my daily job as I have to have basic measures in my report in case I need them but for this challenge, I wanted to use variables and see how I can use best practices to write the code in one measure. I decided to return BLANK whenever we don’t have values for either Current Year or the Previous Year, and return dash (instead of up and down arrows) whenever the difference is ZERO.
I created another measure for colour as well which is pretty much the same as what I had in the first measure, but if you have basic measures in your report like “Oil Prod Net” and “Oil Prod Net PY” you don’t need to repeat what you had in your first measure in the colour measure.
And as I LOVE measure tables, I created it even for this challenge with two measures
This is the first measure calculating the YoY%
Oil Prod Net YoY% =
VAR OilProdNet = SUM( 'Data Cleaned'[Oil Prod Net] )
VAR OilProdNetPY = CALCULATE( SUM( 'Data Cleaned'[Oil Prod Net] ), DATEADD( 'Date'[Date], -1, YEAR ) )
VAR Diff = OilProdNet - OilProdNetPY
VAR Result = DIVIDE( Diff, OilProdNetPY, BLANK() )
RETURN
SWITCH( TRUE,
OR( ISBLANK( OilProdNet ), ISBLANK( OilProdNetPY ) ), BLANK(),
Diff >0, UNICHAR(129053) & " " & FORMAT( Result, "0.0%" ),
Diff <0, UNICHAR(129055) & " " & FORMAT( Result, "0.0%" ),
UNICHAR(8210) & " " & FORMAT( Result, "0.0%" )
)
And this is the measure for colour:
Oil Prod Net YoY% colour =
VAR OilProdNet = SUM( 'Data Cleaned'[Oil Prod Net] )
VAR OilProdNetPY = CALCULATE( SUM( 'Data Cleaned'[Oil Prod Net] ), DATEADD( 'Date'[Date], -1, YEAR ) )
VAR Diff = OilProdNet - OilProdNetPY
VAR Result = DIVIDE( Diff, OilProdNetPY, BLANK() )
RETURN
SWITCH( TRUE,
Diff >0, "#2a9d8f",
Diff <0, "#e63946",
"#adb5bd"
)
And this is the result
Here is the pbix file:
DAX Workout No4 033023 - Hossein.pbix (372.0 KB)
Hope you enjoy it
Regards,
Hossein