Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Counting last N days with data, excluding weekends

Dear Forum Members,   I have a nicely working DAX code with a parameter (N number of days to consider), but I cannot get it to ignore weekend days (Sat, Sun).   Basically, the "Data Availability"...
  • Icey's avatar
    6 years ago

    Hi Anonymous ,

     

    Try this:

    Data Availability Ratio = 
    VAR num_of_selected_days = [Number of Days Value Value]
    VAR date1 =
        LASTDATE ( 'Calendar'[Date] )
    VAR date2 = date1 - num_of_selected_days
    VAR WeekendDays =
        CALCULATE (
            COUNT ( 'Calendar'[DayType] ),
            FILTER (
                ALLSELECTED ( 'Calendar' ),
                'Calendar'[DayType] = "Weekend"
                    && 'Calendar'[Date] > date2
                    && 'Calendar'[Date] <= date1
            )
        )
    VAR num_ =
        IF ( MOD ( WeekendDays, 2 ) = 0, WeekendDays , WeekendDays+ 1 )
    VAR date3 =
        IF ( WeekendDays >= 1, date2 - num_, date2 )
    VAR num_of_days_with_data =
        CALCULATE (
            SUM ( Futures[Data Availability] ),
            FILTER (
                ALL ( 'Calendar' ),
                'Calendar'[Date] > date3
                    && 'Calendar'[Date] <= date1
                    && 'Calendar'[DayType] = "Weekday"
            )
        )
    VAR Ratio =
        DIVIDE ( num_of_days_with_data, num_of_selected_days )
    RETURN
        IF (
            MAX ( 'Calendar'[DayType] ) = "Weekday",
            CALCULATE (
                IF ( ISBLANK ( ratio ), 0, Ratio ),
                FILTER ( 'Calendar', 'Calendar'[DayType] = "Weekday" )
            )
        )

     

    BTW, .pbix file attached.

     

     

    Best Regards,

    Icey

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.