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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Optimusprime_25
Resolver I
Resolver I

Line Chart: MTD QTD YTD to display data in dynamic months and days in Line Chart in Power BI Desktop

Hello Team,

 

I have a below requirement where I have to display data according to the MTD, QTD and YTD in Line Chart with KPIs in Slicer.

 

YTD:

Optimusprime_25_0-1657114102493.png

When the user selects Sales in slicer and YTD in anaother slicer then data should display from Jan to Till date in months in Line Chart.

 

QTD:

Optimusprime_25_1-1657114182519.png

When the user selects Sales in slicer and QTD in anaother slicer then data should display from start of that particular quarter to Till date in months in Line Chart.

 

MTD:

Optimusprime_25_2-1657114253370.png

When the user selects Sales in slicer and MTD in anaother slicer then data should display from start of that particular Month to Till date in days in Line Chart.

 

Thanks in Advance.

1 ACCEPTED SOLUTION
v-jianboli-msft
Community Support
Community Support

Hi Optimusprime_25,

 

Please follow these steps:

Create two new tables for slicer:

vjianbolimsft_7-1657695450792.png

 

vjianbolimsft_8-1657695450795.png

 

Then create measures for these two slicers separately:

 

Measure =

SWITCH (

    MAX ( 'FOR SLICER1'[Value] ),

    "SALES", SELECTCOLUMNS ( 'Table', "SALES", [SALES] ),

    "STOCK", SELECTCOLUMNS ( 'Table', "STOCK", [STOCK] ),

    "ORDERS", SELECTCOLUMNS ( 'Table', "ORDERS", [ORDERS] ),

    SELECTCOLUMNS ( 'Table', "INVOICE", [INVOICE] )

)

Measure 2 =

SWITCH (

    MAX ( 'FOR SLICER2'[Value] ),

    "YTD",

        IF (

            MAX ( 'Table'[Date] ) <= TODAY ()

                && MAX ( 'Table'[Date] ) >= DATE ( YEAR ( TODAY () ), 1, 1 ),

            1,

            0

        ),

    "MTD",

        IF (

            MAX ( 'Table'[Date] ) <= TODAY ()

                && MAX ( 'Table'[Date] ) >= DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ),

            1,

            0

        ),

    IF (

        MAX ( 'Table'[Date] ) <= TODAY ()

            && QUARTER ( MAX ( 'Table'[Date] ) ) = QUARTER ( TODAY () )

            && YEAR ( MAX ( 'Table'[Date] ) ) = YEAR ( TODAY () ),

        1,

        0

    )

)

 

Apply Measure to the visual:

vjianbolimsft_9-1657695450800.png

 

Apply Measure 2 to the filter:

vjianbolimsft_10-1657695450800.png

 

Turn the single select of the slicers on:

vjianbolimsft_11-1657695450801.png

 

Final output:

vjianbolimsft_12-1657695450812.png

 

vjianbolimsft_13-1657695450815.png

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
v-jianboli-msft
Community Support
Community Support

Hi @Optimusprime_25 ,

 

Could you tell me if your problem has been solved? If it is, kindly Accept it as the solution. More people will benefit from it. Or if you are still confused about it, please provide me with more details about your table and your problem or share me with your pbix file  after removing sensitive data.

Refer to:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-jianboli-msft  Thanks for the solution, appreciate your support.

v-jianboli-msft
Community Support
Community Support

Hi @Optimusprime_25 

 

Please provide me with more details about the error of your DAX , like this:

vjianbolimsft_0-1657706839557.png

 

Or share me with your pbix file after removing sensitive data.

 

Refer to:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

v-jianboli-msft
Community Support
Community Support

Hi Optimusprime_25,

 

Please follow these steps:

Create two new tables for slicer:

vjianbolimsft_7-1657695450792.png

 

vjianbolimsft_8-1657695450795.png

 

Then create measures for these two slicers separately:

 

Measure =

SWITCH (

    MAX ( 'FOR SLICER1'[Value] ),

    "SALES", SELECTCOLUMNS ( 'Table', "SALES", [SALES] ),

    "STOCK", SELECTCOLUMNS ( 'Table', "STOCK", [STOCK] ),

    "ORDERS", SELECTCOLUMNS ( 'Table', "ORDERS", [ORDERS] ),

    SELECTCOLUMNS ( 'Table', "INVOICE", [INVOICE] )

)

Measure 2 =

SWITCH (

    MAX ( 'FOR SLICER2'[Value] ),

    "YTD",

        IF (

            MAX ( 'Table'[Date] ) <= TODAY ()

                && MAX ( 'Table'[Date] ) >= DATE ( YEAR ( TODAY () ), 1, 1 ),

            1,

            0

        ),

    "MTD",

        IF (

            MAX ( 'Table'[Date] ) <= TODAY ()

                && MAX ( 'Table'[Date] ) >= DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ),

            1,

            0

        ),

    IF (

        MAX ( 'Table'[Date] ) <= TODAY ()

            && QUARTER ( MAX ( 'Table'[Date] ) ) = QUARTER ( TODAY () )

            && YEAR ( MAX ( 'Table'[Date] ) ) = YEAR ( TODAY () ),

        1,

        0

    )

)

 

Apply Measure to the visual:

vjianbolimsft_9-1657695450800.png

 

Apply Measure 2 to the filter:

vjianbolimsft_10-1657695450800.png

 

Turn the single select of the slicers on:

vjianbolimsft_11-1657695450801.png

 

Final output:

vjianbolimsft_12-1657695450812.png

 

vjianbolimsft_13-1657695450815.png

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hey 
I tried this solution for my own PBI.

 

But for some reason for the MTD calculation is showing some days in the previous month as well.

Same happens with QTD starts at 06/25 instead of 07/02 which is the first available date in the quarter.

Why is that?

 

pbi_dev_acc22_1-1694523200459.png

This is the actual data  where sales are not 0.

pbi_dev_acc22_0-1694523153697.png

 

This is the last date in the sales table is 09/04/2023 but sales is set to 0 and I have a filter excluding sales equal to 0.

 

The last date in my date table is 31/12/2023. This is because we will include forecasted sales in the near future.

 

I tried the measure with both Sales[Date} and Dim_date[date]  and I get same results. Any insights as what is wrong?

 

 

@v-jianboli-msft Thanks for your response and the solution.

 

I have created Measure and Measure2

 

For Measure, i didn't afce any issues but for Measure2, I am facing below issue.

Measure 2 =

SWITCH (

    MAX ( 'FOR SLICER2'[Value] ),

    "YTD",

        IF (

            MAX ( 'Table'[Date] ) <= TODAY ()

                && MAX ( 'Table'[Date] ) >= DATE ( YEAR ( TODAY () ), 1, 1 ),

            1,

            0

        ),

    "MTD",

        IF (

            MAX ( 'Table'[Date] ) <= TODAY ()

                && MAX ( 'Table'[Date] ) >= DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ),

            1,

            0

        ),

    IF (

        MAX ( 'Table'[Date] ) <= TODAY ()

            && QUARTER ( MAX ( 'Table'[Date] ) ) = QUARTER ( TODAY () )

            && YEAR ( MAX ( 'Table'[Date] ) ) = YEAR ( TODAY () ),

        1,

        0

    )

)

 

Optimusprime_25_0-1657702249239.png

Could you please let me know what is the alternate solution to this.

 

Thanks in advance.

CNENFRNL
Community Champion
Community Champion

Typic senario in which the powerful calculation group and field parameters functionality come into play.

Let report readers use field parameters to change visuals (preview) - Power BI | Microsoft Docs


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.