Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Running Total with three years

I have a sales table with the booking amount(Int Format), fiscal quarter, and fiscal week in text format. they have values , fiscal quarter from 2022Q1 TO 2025Q4 and each quarter has 13 weeks values such as w01,w02,till w13. I have a slicer with the fiscal quarter. fiscal quarter 2025q4 has only 4 weeks of data rest of the quarters have all 13 weeks of data, when I select 2025q4 it should show 4 weeks of data or till it has but the rest of quarters 2024q4, 2023q4 calculated dynamically with slicer selection 2025q4 should display all week 13 data, I need to calculate cumulative booking amount and show three lines on line chart, one for selected quarter, rest two line s previous year same quarter, previous to previous year same quarter.

 

I am using the below Dax but it is showing 4 weeks of data for all means 2025q4 is showing 4 weeks of data, and other 2024q4,2023q4 also showing 4 weeks of data which is incorrect they should show 13 weeks of data.  please support me

 

 

previous year quarter =

var selected_quarter= SELECTEDVALUE(DNM_DRILL_THROUGH[FISCAL_QTR])

var curren_year = int(left(selected_quarter,4))

var current_qtr = right(selected_quarter,2)

var pqtr=(current_year-1)&current_qtr

var max_week = int(right(CALCULATE(MAX(DNM_DRILL_THROUGH[FISCAL_WEEK_NUMBER_D]),DNM_DRILL_THROUGH[FISCAL_QTR]=pqtr),2)) RETURN

CALCULATE([Filterbookingamount],

ALLSELECTED (DNM_DRILL_THROUGH[fiscal_qtr]),

DNM_DRILL_THROUGH[FISCAL_QTR]=pqtr,

int(right(DNM_DRILL_THROUGH[FISCAL_WEEK_NUMBER_D],2))<=max_week)

  • Hi Anonymous,

    Apologize for the delayed response. After thoroughly reviewing the details you provided, I reproduced the scenario again, and it worked on my end. I used it as sample data and successfully implemented it.

    outcome:

     


    I am also including .pbix file for your better understanding, please have a look into it:

    If this post helps, then please give us ‘Kudos’ and consider Accept it as a solution to help the other members find it more quickly.

    Thank you for using Microsoft Community Forum.

9 Replies

  • Hi Anonymous  - can you change the logic for previous year as below:

     

    PreviousYearQuarter =
    VAR SelectedQuarter = SELECTEDVALUE(DNM_DRILL_THROUGH[FISCAL_QTR]) -- Selected quarter from slicer
    VAR CurrentYear = INT(LEFT(SelectedQuarter,4)) -- Extract year
    VAR CurrentQtr = RIGHT(SelectedQuarter,2) -- Extract Q1, Q2, etc.
    VAR PrevYearQtr = FORMAT(CurrentYear - 1, "0000") & CurrentQtr -- Get previous year same quarter

    -- Find max week number for selected quarter
    VAR MaxWeekSelectedQtr =
    MAXX(
    FILTER(DNM_DRILL_THROUGH, DNM_DRILL_THROUGH[FISCAL_QTR] = SelectedQuarter),
    INT(RIGHT(DNM_DRILL_THROUGH[FISCAL_WEEK_NUMBER_D], 2))
    )

    RETURN
    CALCULATE(
    [FilterBookingAmount], -- Your measure for Booking Amount
    ALLSELECTED(DNM_DRILL_THROUGH[FISCAL_QTR]),
    DNM_DRILL_THROUGH[FISCAL_QTR] = PrevYearQtr,

    -- Ensure previous year always shows full 13 weeks, while selected quarter is restricted dynamically
    IF(
    SelectedQuarter = PrevYearQtr,
    INT(RIGHT(DNM_DRILL_THROUGH[FISCAL_WEEK_NUMBER_D],2)) <= 13, -- Show full 13 weeks for previous year
    INT(RIGHT(DNM_DRILL_THROUGH[FISCAL_WEEK_NUMBER_D],2)) <= MaxWeekSelectedQtr -- Restrict only selected quarter
    )
    )

     

    Hope this helps. . please check

  • Ray_Minds's avatar
    Ray_Minds
    Solution Supplier

    Hi Anonymous 

    Please use this DAX to get the correct answer 

    Cumulative Booking Amount =

    VAR selected_quarter = SELECTEDVALUE(DNM_DRILL_THROUGH[FISCAL_QTR])

    VAR current_year = INT(LEFT(selected_quarter, 4))  -- Extracts the year

    VAR current_qtr = RIGHT(selected_quarter, 2)       -- Extracts the quarter

     

    -- Get previous and previous-to-previous year quarters

    VAR pqtr = FORMAT(current_year - 1, "0000") & current_qtr

    VAR ppqtr = FORMAT(current_year - 2, "0000") & current_qtr

     

    -- Get max week available for the selected quarter

    VAR max_week_selected =

        MAXX(

            FILTER(DNM_DRILL_THROUGH, DNM_DRILL_THROUGH[FISCAL_QTR] = selected_quarter),

            INT(RIGHT(DNM_DRILL_THROUGH[FISCAL_WEEK_NUMBER_D], 2))

        )

     

    -- Compute Cumulative Booking Amount

    RETURN

    CALCULATE(

        [Filterbookingamount],  -- Use your booking amount measure

        DNM_DRILL_THROUGH[FISCAL_QTR] IN { selected_quarter, pqtr, ppqtr },

        IF(

            DNM_DRILL_THROUGH[FISCAL_QTR] = selected_quarter,

            INT(RIGHT(DNM_DRILL_THROUGH[FISCAL_WEEK_NUMBER_D], 2)) <= max_week_selected,

            TRUE()  -- Ensures previous quarters show all 13 weeks

        )

    )




    Best regards,
    Ray Minds
    http://www.rayminds.com
    https://www.linkedin.com/company/rayminds/

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

  • v-kpoloju-msft's avatar
    v-kpoloju-msft
    Community Support

    Hi Anonymous,

    Thank you for reaching out to the Microsoft fabric community forum. Thank you Ray_Minds, and rajendraongole1, for your inputs on this issue. After thoroughly reviewing the details you provided, I was able to reproduce the scenario, and it worked on my end. I have used it as sample data on my end and successfully implemented it.

    Please try this measures I have tried with some sample data in my end, it worked fine:

    Selected Quarter Cumulative Booking Amount:

    CumulativeBookingAmount =

    VAR SelectedQuarter = SELECTEDVALUE(SalesTable[Fiscal Quarter])

    VAR MaxWeek = MAXX(FILTER(SalesTable, SalesTable[Fiscal Quarter] = SelectedQuarter), SalesTable[FiscalWeekNumber])

    RETURN

    CALCULATE(

        SUM(SalesTable[Booking Amount]),

        SalesTable[Fiscal Quarter] = SelectedQuarter,

        SalesTable[FiscalWeekNumber] <= MaxWeek

    )


    Previous Year Same Quarter Cumulative Booking Amount:

    PY_CumulativeBookingAmount =

    VAR SelectedQuarter = SELECTEDVALUE(SalesTable[Fiscal Quarter])

    VAR CurrentYear = LEFT(SelectedQuarter, 4) * 1

    VAR CurrentQtr = RIGHT(SelectedQuarter, 2)

    VAR PrevQuarter = FORMAT(CurrentYear - 1, "0000") & CurrentQtr

    RETURN

    CALCULATE(

        SUM(SalesTable[Booking Amount]),

        SalesTable[Fiscal Quarter] = PrevQuarter

    )

    Previous to Previous Year Same Quarter Cumulative Booking Amount:

    PPY_CumulativeBookingAmount =

    VAR SelectedQuarter = SELECTEDVALUE(SalesTable[Fiscal Quarter])

    VAR CurrentYear = LEFT(SelectedQuarter, 4) * 1

    VAR CurrentQtr = RIGHT(SelectedQuarter, 2)

    VAR PrevPrevQuarter = FORMAT(CurrentYear - 2, "0000") & CurrentQtr

    RETURN

    CALCULATE(

        SUM(SalesTable[Booking Amount]),

        SalesTable[Fiscal Quarter] = PrevPrevQuarter

    )

     

     


    I am also including .pbix file for your better understanding, please have a look into it:


    I hope this could resolve your issue, if you need any further assistance, feel free to reach out. If this post helps, then please give us ‘Kudos’ and consider Accept it as a solution to help the other members find it more quickly.

    Thank you for using Microsoft Community Forum.

    • Anonymous's avatar
      Anonymous
      Not applicable

      approach is good but when i selected 2025Q4 it is only showing current year data, it should show three lines in a single chart with 4 weeks data for 2025q4 and all 13 weeks dta for 2024q4,2023q4

      • v-kpoloju-msft's avatar
        v-kpoloju-msft
        Community Support

        Hi Anonymous,

        Apologize for the delayed response. After thoroughly reviewing the details you provided, I reproduced the scenario again, and it worked on my end. I used it as sample data and successfully implemented it.

        outcome:

         


        I am also including .pbix file for your better understanding, please have a look into it:

        If this post helps, then please give us ‘Kudos’ and consider Accept it as a solution to help the other members find it more quickly.

        Thank you for using Microsoft Community Forum.

    • v-kpoloju-msft's avatar
      v-kpoloju-msft
      Community Support

      Hi Anonymous,

       

      May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

       

      Thank you.

      • v-kpoloju-msft's avatar
        v-kpoloju-msft
        Community Support

        Hi Anonymous,


        I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.


        Thank you.