Forum Discussion

shivkonar's avatar
shivkonar
Frequent Visitor
7 years ago
Solved

Bar Chart / Date Calculation

Hi,   I am trying to solve a made up problem. Imagine we have a 2 table model. The Sales table and a Date Table in a 1 to Many relationship.   On the Power BI report, there is a slicer for Date[C...
  • shivkonar's avatar
    shivkonar
    7 years ago

    Thanks v-juanli-msft

     

    There is a more elegant solution by Marco Russo.

     

    Step 1:- Create a new DateSelection table, this table will be placed on the slicer

    DateSelection = DISTINCT ( 'Date'[Calendar WeekNumber] )

    Step 2:- Create the measure

    Last 4 Week Sales = 
    VAR LastWeek = SELECTEDVALUE ( 'DateSelection'[Calendar WeekNumber] )
    VAR FirstWeek = LastWeek - 3
    VAR Result =
        CALCULATE (
            SUM ( 'Sales'[Sales] ),
            KEEPFILTERS (
                'Date'[Calendar WeekNumber] >= FirstWeek
                    && 'Date'[Calendar WeekNumber] <= LastWeek
            )
        )
    RETURN Result

    Step 3:- Create the bar chart, by placing Date[Calendar WeekNumber] on the axis and [Last 4 Week Sales] on the values.

     

    So, now for any week selected, the bar chart will show 4 bars, one bar for the selected week and 3 previous week.