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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register 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
v-jialongy-msft
Community Support
Community Support

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.

v-jialongy-msft
Community Support
Community Support

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.