Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
PabloML
Regular Visitor

Sum the averages for the total

Dear all,

 

I'm having a doubt on how calculate a measure that for a single-item is the average, but for the totals it needs to sum the average of each cattegory. 

 

The measure it's:

Target YTD =

var _Year = SELECTEDVALUE(Calendar[Year_Especial])

var _Month = SELECTEDVALUE(Calendar[Month_Especial])

var _MonthOrder =

MAXX(

    FILTER(ALL(Calendar), AND(Calendar[Month_Especial] = _Month, Calendar[Year_Especial]= _Year)),

    Calendar[Year_Month_Order]

)

var _value =

CALCULATE(

    AVERAGE(Fact_Table[TARGET]),

    FILTER(ALL('Calendar'),AND(Calendar[Year_Especial] = _Year, Calendar[Year_Month_Order]<= _MonthOrder))

)

var _table = VALUES(Fact_Table[KPI/ITEM])

var _valueTotal =

ADDCOLUMNS(

    _table,

    "@Value", _value

)

RETURN

IF(

    HASONEVALUE(Fact_Table[KPI/ITEM]), _value,

    SUMX(_valueTotal, [@Value])

)

 

The next table is the expected result, where the Subtotal are the sum of the averages for each category and the Total is the sum of Subtotal, but I don't get the Subtotal correct neither the Total.

 

Anyone has a clue about what I'm doing wrong?

 

Thanks. 

 

Main KPI             KPI/ITEM    Target YTD

KPI 1KPI 1.12
KPI 1KPI 1.23
KPI 1KPI 1.34
SubTotal KPI 1 9
KPI 2KPI 2.12
KPI 2KPI 2.25
KPI 2KPI 2.34
SubTotal KPI 2 11
Total 20
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @PabloML 

 

Try the following dax

Target YTD =
VAR _Year = SELECTEDVALUE(Calendar[Year_Especial])
VAR _Month = SELECTEDVALUE(Calendar[Month_Especial])
VAR _MonthOrder = MAXX(
    FILTER(
        ALL(Calendar),
        Calendar[Month_Especial] = _Month && Calendar[Year_Especial] = _Year
    ),
    Calendar[Year_Month_Order]
)
VAR _CategoryContext = VALUES(Fact_Table[KPI/ITEM])
RETURN
IF(
    HASONEVALUE(Fact_Table[KPI/ITEM]),
    CALCULATE(
        AVERAGE(Fact_Table[TARGET]),
        FILTER(
            ALL(Calendar),
            Calendar[Year_Especial] = _Year && Calendar[Year_Month_Order] <= _MonthOrder
        )
    ),
    SUMX(
        _CategoryContext,
        CALCULATE(
            AVERAGE(Fact_Table[TARGET]),
            FILTER(
                ALL(Calendar),
                Calendar[Year_Especial] = _Year && Calendar[Year_Month_Order] <= _MonthOrder
            )
        )
    )
)

 

 

If the above dax can't help you solve your problem, please provide detailed sample pbix file and the results you expect.So that I can help you better. Please remove any sensitive data in advance.

 

 

 

 

Best Regards,

Jayleny

 

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

View solution in original post

2 REPLIES 2
PabloML
Regular Visitor

Many thanks, Jayleny. It works great. Now, I need to understand why executing the code for "single-item" within the main calculate, where a virtual table is created, will work, and not the way I was doing it.

 

As said, many thanks.

Anonymous
Not applicable

Hi @PabloML 

 

Try the following dax

Target YTD =
VAR _Year = SELECTEDVALUE(Calendar[Year_Especial])
VAR _Month = SELECTEDVALUE(Calendar[Month_Especial])
VAR _MonthOrder = MAXX(
    FILTER(
        ALL(Calendar),
        Calendar[Month_Especial] = _Month && Calendar[Year_Especial] = _Year
    ),
    Calendar[Year_Month_Order]
)
VAR _CategoryContext = VALUES(Fact_Table[KPI/ITEM])
RETURN
IF(
    HASONEVALUE(Fact_Table[KPI/ITEM]),
    CALCULATE(
        AVERAGE(Fact_Table[TARGET]),
        FILTER(
            ALL(Calendar),
            Calendar[Year_Especial] = _Year && Calendar[Year_Month_Order] <= _MonthOrder
        )
    ),
    SUMX(
        _CategoryContext,
        CALCULATE(
            AVERAGE(Fact_Table[TARGET]),
            FILTER(
                ALL(Calendar),
                Calendar[Year_Especial] = _Year && Calendar[Year_Month_Order] <= _MonthOrder
            )
        )
    )
)

 

 

If the above dax can't help you solve your problem, please provide detailed sample pbix file and the results you expect.So that I can help you better. Please remove any sensitive data in advance.

 

 

 

 

Best Regards,

Jayleny

 

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

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors