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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
mastone
Frequent Visitor

Convert Table Expression (i.e. virtual table) to a set of Measures

New to DAX and power BI.  I am struggling and want to go hide under the covers.   Anywhoooo, after many iterations I have been able to generate a virtual table that produces the results I want.  But I want it in a measure.  The complexity comes in via the fact that I need to do calculations on measures several layers deep.

 

Here is my "measure" so far (it doesn't work because I can't figure out how return the value as a scalar)  Code is also in screenshot below for readability.

 
Impact = 
VAR countfailureTable =
    GROUPBY(
        FILTER( ALLSELECTED(Surveys), Surveys[SVRESULT] >= 0 ),
        Surveys[QuestionId],
        "QuestionCount", COUNTX( CURRENTGROUP(), Surveys[QuestionId] ),
        "FailurePercent", AVERAGEX( CURRENTGROUP(), Surveys[SVRESULT] )
    )
VAR allItems =
    ADDCOLUMNS(
        countfailureTable,
        "NegativeImpact",
            (
                [QuestionCount] / SUMX( countfailureTable, [QuestionCount] ) * [FailurePercent]
            )
    )
VAR finalItems =
    ADDCOLUMNS(
        allItems,
        "RelativeImpact", [NegativeImpact] / MAXX( allItems, [NegativeImpact] )
    )

If I take the above and create a new table from it in my model, it produces the correct result (aside from not having the row and filter contexts).  The table looks like this with the underlined columns being the data I want.

mastone_0-1680904583308.png

 

What I can't figure out is how to associate the [QuestionId] in my virtual table called [FinalItems] to the QuestionId field in the current row context of the visualization where the measure is used.  Furthermore, I feel (but can't figure out how to confirm) that the rows used by the GROUPBY does not have the current row context (even though I used ALLSELECTED).  So how do I lookup the [RelativeImpact] field using the row context [QuestionId] field???

I feel like my virtual table above needs to be converted into a series of measures, but I don't see a way to do it.  So if there is a completely better approach, please don't hesitate to scrap my many many days of work and suggest something new.  🙂

 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @mastone 
Please try

ImpactMeasure =
VAR countfailureTable =
    GROUPBY (
        FILTER ( ALLSELECTED ( Surveys ), Surveys[SVRESULT] >= 0 ),
        Surveys[QuestionId],
        "QuestionCount", COUNTX ( CURRENTGROUP (), Surveys[QuestionId] ),
        "FailurePercent", AVERAGEX ( CURRENTGROUP (), Surveys[SVRESULT] )
    )
VAR allItems =
    ADDCOLUMNS (
        countfailureTable,
        "NegativeImpact",
            (
                [QuestionCount] / SUMX ( countfailureTable, [QuestionCount] ) * [FailurePercent]
            )
    )
VAR finalItems =
    ADDCOLUMNS (
        allItems,
        "RelativeImpact", [NegativeImpact] / MAXX ( allItems, [NegativeImpact] )
    )
RETURN
    AVERAGEX (
        VALUES ( Surveys[QuestionId] ),
        MAXX (
            FILTER ( finalItems, [QuestionId] = EARLIER ( [QuestionId] ) ),
            [RelativeImpact]
        )
    )

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

Hi @mastone 
Please try

ImpactMeasure =
VAR countfailureTable =
    GROUPBY (
        FILTER ( ALLSELECTED ( Surveys ), Surveys[SVRESULT] >= 0 ),
        Surveys[QuestionId],
        "QuestionCount", COUNTX ( CURRENTGROUP (), Surveys[QuestionId] ),
        "FailurePercent", AVERAGEX ( CURRENTGROUP (), Surveys[SVRESULT] )
    )
VAR allItems =
    ADDCOLUMNS (
        countfailureTable,
        "NegativeImpact",
            (
                [QuestionCount] / SUMX ( countfailureTable, [QuestionCount] ) * [FailurePercent]
            )
    )
VAR finalItems =
    ADDCOLUMNS (
        allItems,
        "RelativeImpact", [NegativeImpact] / MAXX ( allItems, [NegativeImpact] )
    )
RETURN
    AVERAGEX (
        VALUES ( Surveys[QuestionId] ),
        MAXX (
            FILTER ( finalItems, [QuestionId] = EARLIER ( [QuestionId] ) ),
            [RelativeImpact]
        )
    )

Thank you so much tamerj1!  This did the trick with the slight modification to filter records instead of the VALUES() call.  It ended up looking like this and works as expected:

    MAXX(
        FILTER(
             Surveys ,
            Surveys[SVRESULT] >= 0
                && Surveys[SVRESULT] < 100
        ),
        MAXX(
            FILTER( finalItems, [QuestionId] = EARLIER( [QuestionId] ) ),
            [RelativeImpact]
        )
    )

 

Question for you.  Is this a common pattern?  I keep finding myself having to do aggregate measures based other aggregate measures and I run into problems.  Is the fact that I keep running into measures on measures an indication that I am doing something wrong?  Could that be a red flag?  Or is it a common pattern to have this sort of thing?

Thanks again for you help.  I was so frustrated.   Taking the average of a max that produces a single value seems odd, but it logically makes sense and wrangles the table into a scalar...which is what I needed.

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.