Forum Discussion

msuser48's avatar
msuser48
Icon for Helper I rankHelper I
3 years ago
Solved

Find average values in dataset when duplicate rows exist

I have the following data:   Issue 1) I need to find the average number of visits per employee_id. - How do I accomplish this when there are duplicates of employee_Id?   Issue 2) I first n...
  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi msuser48,

    #1, You can create a variable with summarize function to check the current value count to get average on the result if they have more than one row.

    formula =
    VAR summary =
        SUMMARIZE (
            FILTER (
                ALLSELECTED ( table ),
                table[employee_id] IN VALUES ( table[employee_id] )
            ),
            [employee_id],
            [average_times],
            "CountTimes", COUNTROWS ( table )
        )
    RETURN
        IF (
            COUNTROWS ( FILTER ( summary, [CountTimes] ) ) > 0,
            CALCULATE (
                AVERAGE ( table[average_times] ),
                ALLSELECTED ( table ),
                VALUES ( table[employee_id] )
            )
        )

    #2, you can try to use the following formulas if help:

    average_time_per_month_by_employee =
    VAR currDate =
        MAX ( table[date] )
    RETURN
        CALCULATE (
            AVERAGE ( table[average_times] ),
            FILTER (
                ALLSELECTED ( table ),
                YEAR ( [Date] ) = YEAR ( currDate )
                    && MONTH ( [date] ) = MONTH ( currDate )
            ),
            VALUES ( table[employee_id] )
        )
    
    average_time_per_month =
    VAR summary =
        SUMMARIZE (
            ADDCOLUMNS (
                table,
                "year", YEAR ( table[date] ),
                "month", MONTH ( table[date] )
            ),
            [year],
            [month],
            [employee_id],
            "AVG", AVERAGE ( table[average_times] )
        )
    RETURN
        AVERAGEX ( summary, [AVG] )

    Regards,

    Xiaoxin Sheng