Forum Discussion

x7's avatar
x7
Frequent Visitor
1 year ago
Solved

Averages per Weekday

Hi all,   Given the following table, my main goal is to have a matrix that has weekdays as columns and average sales as values. I want to calculate the average total sales value per weekday in two ...
  • lbendlin's avatar
    1 year ago

    To report on things that are not there you need to use disconnected tables and/or crossjoins

    Add a disconnected calendar table to your data model and then use it in the visual.  Your measure can then do the rest.

  • x7's avatar
    x7
    1 year ago

    This solution seems to work with the sample data and an unconnected calendar:

     

    AverageSalesIncludingNoSales = 
    VAR CurrentWeekday = SELECTEDVALUE(UnconnectedCalendar[Weekday])  // Get the current weekday from the visual context
    VAR TotalSales = CALCULATE(
        SUM(Sales[Sales]),
        FILTER(Sales, WEEKDAY(Sales[Date], 2) = CurrentWeekday)  // Filter sales for the current weekday
    )
    VAR TotalWeekdays = COUNTROWS(
        FILTER(
            UnconnectedCalendar,
            UnconnectedCalendar[Weekday] = CurrentWeekday  // Count all instances of the current weekday
        )
    )
    RETURN
    IF(
        TotalWeekdays = 0,
        0,
        DIVIDE(TotalSales, TotalWeekdays, 0)  // Use 0 as an alternate result for division by zero
    )