2022 Week 03 | Power BI: Can you visualize percent difference from a selected date?

Introduction

This week we are taking a page from the Tableau team and looking at how a metric has changed from the value at a specified reference date. 

Requirements

  1. Create a line chart showing percent change of the close price from a selected date. 
  2. Use a slicer or other visual to allow users to change the reference date. 
  3. Use conditional formatting to display the selected reference date in the title of the line chart. 
  4. Instead of a legend, display the series label at the end of each line. 
  5. Add a vertical reference line to the line chart that marks the selected reference date. 

Dataset

This week’s data comes from Yahoo Stocks API. We are using the data that Lorna collected and made available on Data.World

Owner: missdataviz
Dataset ID: wow22w3-stocks-jan22

Share

After you finish your workout, share on Twitter using the hashtags #WOW2022 and #PowerBI, and tag @JSBaucke@MMarie, @shan_gsd, @KerryKolosko, and @NerdyWithData. Also make sure to fill out the Submission Tracker so that we can count you as a participant this week in order to track our participation throughout the year. 

Solution

7 thoughts on “2022 Week 03 | Power BI: Can you visualize percent difference from a selected date?”

  1. Managed to get all other things except the DAX code to
    1. keep the lines unfiltered when a date is picked in the slicer. Currently, when I pick a date all the lines are getting filtered.
    2. DAX to work out percent change of the close price from a selected date.

    Wondering is below DAX valid to compute the monthly % change of close prices?
    Monthly Close % Chg =
    VAR PrevMonthClose =
    CALCULATE ( SUM ( Stocks[Close] ), DATEADD ( Stocks[Date], -1, MONTH ) )
    VAR CloseVal =
    CALCULATE ( SUM( Stocks[Close] ), Stocks[Date] = SelDate )
    RETURN
    DIVIDE ( CloseVal – PrevMonthClose, PrevMonthClose )

    1. Hi, Kevin. The PBIX and solution video are available now, so that should help. Your DAX is pretty close, but the exercise is calculating difference from the reference point instead of the previous month. Here is what I used.
      % Change Close =
      Var CurClose = Max('Stocks'[Close])
      Var SelectedDate = SELECTEDVALUE('Reference Date'[Date])
      Var RefClose = CALCULATE(Max('Stocks'[Close]), 'Stocks'[Date] =SelectedDate)
      RETURN
      Divide(CurClose - RefClose, RefClose)

      For the date selection part, you’ll need to use a disconnected table.

  2. Many thanks, Meagan for the DAX. I was able to replicate the same in my file. Hoping to learn about more such clever tricks.. thanks again.

  3. Hello Meagan, thank you for the informative video. Can i ask if this method will work with Year rather than a date? I’m just having trouble getting a year version to work correctly.

    1. Meagan Longoria

      I’m not sure what you want when you say “work with year”. If you have a date table connected to your fact and can aggregate your data at the year level, then you can make this work.

  4. Hello and many thanks to you, Meagan
    I just need to have my stocks returns for the last 15 day period, How can I modify your code to get that done?

    1. Meagan Longoria

      I’m not sure if you mean the last 15 days from the current date or something like the 15-day rolling average close price. If the former, instead of a reference date, you will want to add a regular date table and create a relationship between the Stocks table and Dates table. Then you can add a slicer and change it to relative date and enter 15 days. If you are looking for more of a rolling average, you can find helpful DAX at https://www.daxpatterns.com/standard-time-related-calculations/

Leave a Reply to Meagan Longoria Cancel Reply

Your email address will not be published. Required fields are marked *

Scroll to Top