Forum Discussion

Koritala's avatar
Koritala
Icon for Post Patron rankPost Patron
1 year ago
Solved

How To Show Last 4 weeks Trend In Column Bar chart in Power bi Desktop Using a DAX measure

Hi,

How can I show the last 4 weeks trend in column bar chart in power bi desktop using a single measure.

 

I want to show the sales for my product categories, always for last 4 weeks in column bar chart.

 

Thanks,

Sri

 

6 Replies

  • Do you mean you always want to see the last 4 weeks in the bar chart?

     

    If yes, based on what do you consider a week to be the last one (week of today, week of the most recent fact...)?

     

    My solution would be to create a calculated column in the calendar, showing a "Y" if the date is in the last 4 weeks and a "N" otherwise

     

    Yu can then use that column to filter the values in the X line (or Y line, depend if you have an horizontal or vertical bar chart)

     

    Is this what you want? If yes, please specify how to identify the last week and I shall provide the code

     

    If this helped, please consider giving kudos and mark as a solution

    mein replies or I'll lose your thread

    consider voting this Power BI idea

    Francesco Bergamaschi

    MBA, M.Eng, M.Econ, Professor of BI

     

     

  • Koritala Create a Date Table: Ensure you have a Date table in your model.

     

    dax
    DateTable =
    ADDCOLUMNS (
    CALENDAR (MIN(Sales[OrderDate]), MAX(Sales[OrderDate])),
    "Year", YEAR([Date]),
    "Month", MONTH([Date]),
    "Week", WEEKNUM([Date], 2),
    "YearMonth", FORMAT([Date], "YYYY-MM")
    )

     

    Create a Measure for Last 4 Weeks Sales:

    dax
    Last4WeeksSales =
    CALCULATE (
    SUM(Sales[SalesAmount]),
    DATESINPERIOD(
    DateTable[Date],
    MAX(DateTable[Date]),
    -4,
    WEEK
    )
    )

     

    Create a Column Bar Chart

    Add Data to the Chart:

    Drag the Date field from your Date table to the Axis of the chart.
    Drag the Last4WeeksSales measure to the Values of the chart.


    Apply a filter to the Date field to show only the last 4 weeks. You can do this by adding a relative date filter in the Filters pane:

    Select the Date field in the Filters pane.
    Choose "Relative date filtering".
    Set it to show the last 4 weeks.
    This setup will ensure that your column bar chart always displays the sales trend for the last 4 weeks.

     

    • Koritala's avatar
      Koritala
      Icon for Post Patron rankPost Patron

      Hi Bhanu,

      I want to show the last 4 weeks trend in column bar chart. On X-axis, it should display the comparison  of last 4 weeks including current week with bars.

      Thanks,

      Sri

  • OK

     

    I do not know how you coded your weeks, calendar weeks? ISO weeks?), so my solution might need fixes

     

    Last 4 weeks =
    VAR CurDate = TODAY () -- or the refresh date TODAY () -1 ??
    VAR CurWeek = YEAR ( CurDate ) *53 + WEEKNUM( CurDate )
    VAR CurDateWeek = YEAR ( 'Calendar'[Date] ) *53 +WEEKNUM( 'Calendar'[Date] )
    RETURN IF ( CurDateWeek >= CurWeek-3 && CurDateWeek <= CurWeek, "Y", "N" )
     

    If this helped, please consider giving kudos and mark as a solution

    mein replies or I'll lose your thread

    Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

    Consider voting this Power BI idea

    Francesco Bergamaschi

    MBA, M.Eng, M.Econ, Professor of BI