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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Creating a Custom Power BI Line Chart for Year-to-Date Sales with Conditional Month Filtering

Hi all 

I'm working with a measure called 'SalesYTD' and want to display it in a line chart with the months on the X-axis. Here's how I'd like it to behave:

  • For Years Before the Current Year (Date[Year] < This Year): Display data for all months.
  • For Years After the Current Year (Date[Year] > This Year): Leave these as blank.
  • For the Current Year (Date[Year] = This Year): The display depends on the selected month:
    • If a Future Month is Selected (Date[Month] > Current Month): Show data up to the previous month only. The current and future months should be blank.
    • If the Current or an Earlier Month is Selected (Date[Month] <= Current Month): Display data only up to the month before the selected one. For instance, if September is chosen, data should be shown only until August.

However, I'm encountering an issue. When I select a month, the chart only displays data for that month instead of cumulatively up to that month.

I've considered using variables and the DATESBETWEEN function, but it's leading to errors. Note that 'SalesYTD' is built on a few other measures and utilizes the TOTALYTD function.

Could you assist me in resolving this? Perhaps with some code examples, I might be able to pinpoint what I'm missing.

Thanks in advance!

1 REPLY 1
Sahir_Maharaj
Super User
Super User

Hello @Anonymous,

 

Can you please try this:

 

1. Define Helper Measures/Variables

Current Year = YEAR(TODAY())
Current Month = MONTH(TODAY())
Selected Year = MAX(Date[Year])
Selected Month = MAX(Date[Month])

2. Modify the SalesYTD Measure

SalesYTD Conditional = 
VAR CurrentYear = [Current Year]
VAR CurrentMonth = [Current Month]
VAR SelectedYear = [Selected Year]
VAR SelectedMonth = [Selected Month]
VAR StartDate = DATE(CurrentYear, 1, 1)
VAR EndDate = 
    IF(
        SelectedYear < CurrentYear,
        DATE(SelectedYear, 12, 31),
        IF(
            SelectedYear > CurrentYear,
            BLANK(),  // Future years
            IF(
                SelectedMonth > CurrentMonth,
                DATE(CurrentYear, CurrentMonth, 1) - 1,
                DATE(CurrentYear, SelectedMonth, 1) - 1
            )
        )
    )
RETURN
IF(
    ISBLANK(EndDate),
    BLANK(),
    CALCULATE(
        [SalesYTD],
        DATESBETWEEN(Date[Date], StartDate, EndDate)
    )
)

 

Should you require any further assistance, please do not hesitate to reach out to me.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution?
➤ Lets connect on LinkedIn: Join my network of 13K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.