Forum Discussion

DominicHobbs's avatar
DominicHobbs
New Member
6 years ago
Solved

Typical value based on time range and category

I've seen a number of posts similar to this but am too new to be able to translate to my specific problem. Sorry if old news.

I'm trying to get the typical value (prefer median but okay with mean) over a date range where a category matches that of the current row.

Specifically I have a table with dates, the date's day of week, and a value. I want to see the typical value within the last x weeks where the weekday matches.

 

DateDayOfWeekValue
19/03/20203233
18/03/20202663
17/03/20201488
16/03/20200520
15/03/20206813
14/03/20205849
13/03/20204331
12/03/20203414
11/03/20202591
10/03/20201330
09/03/20200705
08/03/20206249
07/03/20205794
06/03/20204512
05/03/20203583
04/03/20202952
03/03/20201449
02/03/20200765
01/03/20206

554

The data is highly variant based on day of week and trends over time. I want a rough prediction of the value for the next day based on these elements.

TIA

  • Greg_Deckler's avatar
    Greg_Deckler
    6 years ago

    @DominicHobbs - Yes, you totally forgot about a EVERYTHING

    Median Measure = 
      VAR __x = 1 //number of weeks
      VAR __MaxDate = MAX('Table (18)'[Date]) //current date in context
      VAR __MinDate = __MaxDate - (7 * __x) - 1
      VAR __Table = FILTER(ALL('Table (18)'),[Date]>=__MinDate && [Date]<=__MaxDate)
    RETURN
      MEDIANX(FILTER(__Table,WEEKDAY([Date])=WEEKDAY(__MaxDate)),[DayTotalTickets])

6 Replies

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

    DominicHobbs - You could do this:

     

    Median Measure =
      VAR __x = 5 //number of weeks
      VAR __MaxDate = MAX('Table'[Date]) //current date in context
      VAR __MinDate = __MaxDate - (7 * 5) - 1
      VAR __Table = FILTER('Table',[Date]>=__MinDate && [Date]<=__MaxDate)
    RETURN
      MEDIANX(FILTER(__Table,WEEKDAY([Date])=WEEKDAY(__MaxDate)),[DayOfWeekValue])

     

     

    Also, you may find this interesting - https://community.powerbi.com/t5/Quick-Measures-Gallery/To-bleep-With-MEDIAN/td-p/1322755

  • DominicHobbs , same weekday is 7 days behind

     

    7 Day behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-7,Day))
    7th Last Day = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Date]=max('Date'[Date])-7))
    7th Last Day = CALCULATE(sum('order'[Qty]), previousday(dateadd('Date'[Date],-6,Day)))

     

    refer

    Power BI — Day Intelligence
    https://medium.com/@amitchandak.1978/power-bi-day-intelligence-questions-time-intelligence-5-5-5c3243d1f9

     

    Power BI — WTD
    https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
    https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-Last-Week/ba-p/1051123

    • DominicHobbs's avatar
      DominicHobbs
      New Member

      Thanks both for your replies. I think the table formatting in my OP has caused some confusion. There are 3 columns, the second being the DayOfWeek (int 0 to 6).

      amitchandak  I will look at the reference you have sent however I'm not struggling to identify what I want to filter by - rather to effectively filter/aggregate the values based on it. If I'm missing a solution to that in your reply then apologies. As I say, I'm new to Power BI and just now exploring its syntax etc.

      Greg_Deckler  I tried your code with minor tweaks (table name added, amended value col name, changed hard coded 5 to variable). The output was that the Median Measure column simply held the same values as the value column. Quite prepared to accept I messed up here but unsure how I managed that with the changes I made

       

       

      Median Measure =
        VAR __x = 5 //number of weeks
        VAR __MaxDate = MAX(DailyTicketCount[Date]) //current date in context
        VAR __MinDate = __MaxDate - (7 * __x) - 1
        VAR __Table = FILTER(DailyTicketCount,[Date]>=__MinDate && [Date]<=__MaxDate)
      RETURN
        MEDIANX(FILTER(__Table,WEEKDAY([Date])=WEEKDAY(__MaxDate)),[DayTotalTickets])

       

      Table below a mock-up example only

      Date    DayOfWeek  DayTotalTickets  Median Measure
      19/03/2020    3935935
      18/03/2020    2259259
      17/03/2020    1728728
      16/03/2020    0239239
      15/03/2020    6158158
      14/03/2020    5876876
      13/03/2020    4655655
      12/03/2020    3226226
      11/03/2020    2530530
      10/03/2020    1485485
      09/03/2020    0557557
      08/03/2020    6359359
      07/03/2020    5287287
      06/03/2020    4255255
      05/03/2020    3923923
      04/03/2020    2482482
      03/03/2020    1503503
      02/03/2020    0879879
      01/03/2020    6146146

       

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

        @DominicHobbs - Yes, you totally forgot about a EVERYTHING

        Median Measure = 
          VAR __x = 1 //number of weeks
          VAR __MaxDate = MAX('Table (18)'[Date]) //current date in context
          VAR __MinDate = __MaxDate - (7 * __x) - 1
          VAR __Table = FILTER(ALL('Table (18)'),[Date]>=__MinDate && [Date]<=__MaxDate)
        RETURN
          MEDIANX(FILTER(__Table,WEEKDAY([Date])=WEEKDAY(__MaxDate)),[DayTotalTickets])