Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Select entire week by one date

Hi, im trying to do a filter where i choose the day and the chart show me the entire weekend(for example, with i choose  february 27, the chart have to plot from february 25 until march 1, those are the dates of the 9 week of the year)

 

  • Try this:

    1. Create a calculated table to use as slicer with the dates you need (update the code as required):

     

     

    SlicerTable =
    VAR _InitialDate =
        DATE ( 2018, 01, 01 )
    VAR _LastDate =
        DATE ( 2018, 12, 31 )
    RETURN
        CALENDAR ( _InitialDate, _LastDate )

    2. Place SlicerTable[Date] in a slicer

    3. Place 'Date'[Date] in the axis of the chart as you were already doing and  create a measure like this:

    AuxMeasure =
    IF (
        WEEKNUM ( SELECTEDVALUE ( 'Date'[Date] ), 2 ) = WEEKNUM ( SELECTEDVALUE ( SlicerTable[Date] ), 2 ),
        [YourMeasureHere]
    )

     

    where [YourMeasureHere] is whatever you want to use to be shown on the chart. What [AuxMeasure] does is return [YourMeasureHere] when the date in the axis is in the same week as the date selected in the slicer and blank otherwise. Therefore, only the dates in the selected week will be shown. 

     

    Let me know if that helps

     

  • Anonymous's avatar
    Anonymous
    7 years ago

    i Found a way to solve my problem

     

    1. I created a table with all weeknum(Named this column as Week_NUM) and weekday(Named this column as Day) from the entire year and didnt make any relationship between the tables (named this new table as Table2)

     

    2.  Secondly i created this measure

    .ALLWEEKMEASURE=
    var vSelectedDate = SELECTEDVALUE(Presence[Date])
    var vSelectedWeekNum = WEEKDAY(vSelectedDate;2)
    var vCurrentWeekNum = MAX(Table2[Day])

    var vCurrentWeekNumSeg =
    SWITCH(TRUE();vCurrentWeekNum = 1 && [vSelectedWeekNum] = 5;4;
    vCurrentWeekNum = 1 && [vSelectedWeekNum] = 4;3;
    vCurrentWeekNum = 1 && [vSelectedWeekNum] = 3;2;
    vCurrentWeekNum = 1 && [vSelectedWeekNum] = 2;1;0)

    var vCurrentWeekNumTer= 
    SWITCH(TRUE();vCurrentWeekNum = 2 && [vSelectedWeekNum] = 5;3;
    vCurrentWeekNum = 2 && [vSelectedWeekNum] = 4;2;
    vCurrentWeekNum = 2 && [vSelectedWeekNum] = 3;1;0)

    var vCurrentWeekNumQua=
    SWITCH(TRUE();vCurrentWeekNum = 3 && [vSelectedWeekNum] = 5;2;
    vCurrentWeekNum = 3 && [vSelectedWeekNum] = 4;1;0)

    var vCurrentWeekNumQui= IF(vCurrentWeekNum = 4;1;vCurrentWeekNum)

    return
    SWITCH(TRUE();
    vCurrentWeekNum = vSelectedWeekNum;vSelectedDate;
    vCurrentWeekNum < vSelectedWeekNum && vCurrentWeekNum = 1; vSelectedDate - vCurrentWeekNumSeg;
    vCurrentWeekNum < vSelectedWeekNum && vCurrentWeekNum = 2; vSelectedDate - vCurrentWeekNumTer;
    vCurrentWeekNum < vSelectedWeekNum && vCurrentWeekNum = 3; vSelectedDate - vCurrentWeekNumQua;
    vCurrentWeekNum < vSelectedWeekNum && vCurrentWeekNum = 4; vSelectedDate - vCurrentWeekNumQui;
    vCurrentWeekNum > vSelectedWeekNum;vSelectedDate + (vCurrentWeekNum-vSelectedWeekNum);
    BLANK()
    )
     
    3. Created this measure:
    .Chart_Calculate=
    var DataTeste = [.ALLWEEKMEASURE]
    return
    CALCULATE(
    COUNT(Presence[Presence]);
    ALL(Presence[Data]);
    Presence[Presence]=1;
    Presence[Data] = DataTeste
    )
     
    4. just plot the chart with [.Chart_Calculate] as values and Table2[Day] as axis

6 Replies

  • AlB's avatar
    AlB
    Icon for Community Champion rankCommunity Champion

    Try this:

    1. Create a calculated table to use as slicer with the dates you need (update the code as required):

     

     

    SlicerTable =
    VAR _InitialDate =
        DATE ( 2018, 01, 01 )
    VAR _LastDate =
        DATE ( 2018, 12, 31 )
    RETURN
        CALENDAR ( _InitialDate, _LastDate )

    2. Place SlicerTable[Date] in a slicer

    3. Place 'Date'[Date] in the axis of the chart as you were already doing and  create a measure like this:

    AuxMeasure =
    IF (
        WEEKNUM ( SELECTEDVALUE ( 'Date'[Date] ), 2 ) = WEEKNUM ( SELECTEDVALUE ( SlicerTable[Date] ), 2 ),
        [YourMeasureHere]
    )

     

    where [YourMeasureHere] is whatever you want to use to be shown on the chart. What [AuxMeasure] does is return [YourMeasureHere] when the date in the axis is in the same week as the date selected in the slicer and blank otherwise. Therefore, only the dates in the selected week will be shown. 

     

    Let me know if that helps

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      AlB  that worked with this chart, but the problem is that i have more charts in that page, and when i create the relationship(one-many in both directions) to make the SlicerTable my filter date, that doesnt work anymore 

      • AlB's avatar
        AlB
        Icon for Community Champion rankCommunity Champion

        Anonymous 

        You would have to replicate the same pattern in every chart cause this type of filtering as you propose it is not supported natively in DAX. Another option would be to create a table  with weeks and days and use it as a slicer but selecting the week instead of the day in the slicer. Then create a relationship between the days in the slicer and the days in the axis of the charts  

  • Hi,

     

    Why would you not want to selec the week in the date slicer?  Why complicate things by just selecting a date.

    • Anonymous's avatar
      Anonymous
      Not applicable

      We share the same thought, the problems is that the client asked for this specific in that way(first of all i tried to suggest a week filter, but he told me that would be worst)

      • Anonymous's avatar
        Anonymous
        Not applicable

        i Found a way to solve my problem

         

        1. I created a table with all weeknum(Named this column as Week_NUM) and weekday(Named this column as Day) from the entire year and didnt make any relationship between the tables (named this new table as Table2)

         

        2.  Secondly i created this measure

        .ALLWEEKMEASURE=
        var vSelectedDate = SELECTEDVALUE(Presence[Date])
        var vSelectedWeekNum = WEEKDAY(vSelectedDate;2)
        var vCurrentWeekNum = MAX(Table2[Day])

        var vCurrentWeekNumSeg =
        SWITCH(TRUE();vCurrentWeekNum = 1 && [vSelectedWeekNum] = 5;4;
        vCurrentWeekNum = 1 && [vSelectedWeekNum] = 4;3;
        vCurrentWeekNum = 1 && [vSelectedWeekNum] = 3;2;
        vCurrentWeekNum = 1 && [vSelectedWeekNum] = 2;1;0)

        var vCurrentWeekNumTer= 
        SWITCH(TRUE();vCurrentWeekNum = 2 && [vSelectedWeekNum] = 5;3;
        vCurrentWeekNum = 2 && [vSelectedWeekNum] = 4;2;
        vCurrentWeekNum = 2 && [vSelectedWeekNum] = 3;1;0)

        var vCurrentWeekNumQua=
        SWITCH(TRUE();vCurrentWeekNum = 3 && [vSelectedWeekNum] = 5;2;
        vCurrentWeekNum = 3 && [vSelectedWeekNum] = 4;1;0)

        var vCurrentWeekNumQui= IF(vCurrentWeekNum = 4;1;vCurrentWeekNum)

        return
        SWITCH(TRUE();
        vCurrentWeekNum = vSelectedWeekNum;vSelectedDate;
        vCurrentWeekNum < vSelectedWeekNum && vCurrentWeekNum = 1; vSelectedDate - vCurrentWeekNumSeg;
        vCurrentWeekNum < vSelectedWeekNum && vCurrentWeekNum = 2; vSelectedDate - vCurrentWeekNumTer;
        vCurrentWeekNum < vSelectedWeekNum && vCurrentWeekNum = 3; vSelectedDate - vCurrentWeekNumQua;
        vCurrentWeekNum < vSelectedWeekNum && vCurrentWeekNum = 4; vSelectedDate - vCurrentWeekNumQui;
        vCurrentWeekNum > vSelectedWeekNum;vSelectedDate + (vCurrentWeekNum-vSelectedWeekNum);
        BLANK()
        )
         
        3. Created this measure:
        .Chart_Calculate=
        var DataTeste = [.ALLWEEKMEASURE]
        return
        CALCULATE(
        COUNT(Presence[Presence]);
        ALL(Presence[Data]);
        Presence[Presence]=1;
        Presence[Data] = DataTeste
        )
         
        4. just plot the chart with [.Chart_Calculate] as values and Table2[Day] as axis