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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Jkaelin
Resolver I
Resolver I

Re-Write DAX Measure to NOT calculate Average of Averages

Good morning,

 

I have a DAX measure that has been very useful.  However, I discovered it was calculating the "average of averages" instead of the average of records.

 

Below, the measure is capturing the Top 2 of three different factors.  Instead of averaging the 6 records together, it is calculating the average of the average of the 3x Top 2's.  

 

DAX Test Measure :=
CALCULATE (
    AVERAGEX (
        UNION (
            TOPN ( 2, Table1, Table1[Factor 1] ),
            TOPN ( 2, Table1, Table1[Factor 2] ),
            TOPN ( 2, Table1, Table1[Factor 3] )
        ),
        [Return]
    ),
    FILTER ( Table1, [Valuation] <= PERCENTILE.INC ( [Valuation], 0.5 ) )
)

 

Is their a method to re-write the above DAX measure so it calculates the average of the records instead of the 'avg. of averages'?

 

Thank you,

James K

1 ACCEPTED SOLUTION
Vvelarde
Community Champion
Community Champion

@Jkaelin

 

Hi, please try with this:

 

Correct Answer=
AVERAGEX (
    DISTINCT (
        UNION (
            SELECTCOLUMNS (
                TOPN (
                    2,
                    FILTER (
                        Factors,
                        Factors[Valuation] <= PERCENTILE.INC ( Factors[Valuation], 0,5 )
                    ),
                    Factors[Factor 1]
                ),
                "ID", Factors[PrimaryID],
                "Return", Factors[Return]
            ),
            SELECTCOLUMNS (
                TOPN (
                    2,
                    FILTER (
                        Factors,
                        Factors[Valuation] <= PERCENTILE.INC ( Factors[Valuation], 0,5 )
                    ),
                    Factors[Factor 2]
                ),
                "ID", Factors[PrimaryID],
                "Return", Factors[Return]
            ),
            SELECTCOLUMNS (
                TOPN (
                    2,
                    FILTER (
                        Factors,
                        Factors[Valuation] <= PERCENTILE.INC ( Factors[Valuation], 0,5 )
                    ),
                    Factors[Factor 3]
                ),
                "ID", Factors[PrimaryID],
                "Return", Factors[Return]
            )
        )
    ),
    [Return]
)

Regards

 

Victor

Lima - Peru




Lima - Peru

View solution in original post

5 REPLIES 5
KHorseman
Community Champion
Community Champion

Is [Return] an average measure?





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@KHorseman

 

Yes, [Return] is the column that will be averaged at a record level.  It's technically records of stocks that will be averaged for a portfolio.  

More information:  Below is a link to my sample file and a visual of what I'm trying to accomplish with the measure.  

 

https://www.dropbox.com/s/ycotbvtcpipg7rk/DAX%20Oppty%20Measure.xlsx?dl=0

 

Power BI Dax Measure.PNG

 

 

 

 

Vvelarde
Community Champion
Community Champion

@Jkaelin

 

Hi, please try with this:

 

Correct Answer=
AVERAGEX (
    DISTINCT (
        UNION (
            SELECTCOLUMNS (
                TOPN (
                    2,
                    FILTER (
                        Factors,
                        Factors[Valuation] <= PERCENTILE.INC ( Factors[Valuation], 0,5 )
                    ),
                    Factors[Factor 1]
                ),
                "ID", Factors[PrimaryID],
                "Return", Factors[Return]
            ),
            SELECTCOLUMNS (
                TOPN (
                    2,
                    FILTER (
                        Factors,
                        Factors[Valuation] <= PERCENTILE.INC ( Factors[Valuation], 0,5 )
                    ),
                    Factors[Factor 2]
                ),
                "ID", Factors[PrimaryID],
                "Return", Factors[Return]
            ),
            SELECTCOLUMNS (
                TOPN (
                    2,
                    FILTER (
                        Factors,
                        Factors[Valuation] <= PERCENTILE.INC ( Factors[Valuation], 0,5 )
                    ),
                    Factors[Factor 3]
                ),
                "ID", Factors[PrimaryID],
                "Return", Factors[Return]
            )
        )
    ),
    [Return]
)

Regards

 

Victor

Lima - Peru




Lima - Peru

@Vvelarde

 

Insane solution!  I have been working on this all morning with several variations of Calculate/ TopN/ RankX/ Multiple Filter/ etc.  This is awesome help & I really appreciate it.  You the man!  I don't fully understand how it works, but I'll study it to death & see if I can't make some sense of it.  

 

Thank you,

James

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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