Forum Discussion

WishAskedSooner's avatar
WishAskedSooner
Continued Contributor
1 year ago
Solved

Generating a Lookup Table Help

HI Experts!

 

I am having difficulty generating a lookup table that I need for later processing in a measure. Here is a sample of my requirement and the problem.

 

I have the following table called 'Fact':

EIDAIDVAL1VAL2VAL3
130.10.050.025
140.20.10.05
150.30.150.075
230.40.20.1
240.50.250.125
250.60.30.15
330.70.350.175
340.80.40.2
350.90.450.225

 

Step 1: Calculate a needed temporary column

This involves adding the following column via the following DAX (for some reason the code format has disappeared from my editor, so I apologize for the poor formatting):

 

 
    VAR _NewVal =
        ADDCOLUMNS(
            'Fact',
            "NEWVAL",
            CALCULATE( SUM( 'Fact'[VAL1] ) )
            + CALCULATE( SUM( 'Fact'[VAL2]) ) * 0.1
            + CALCULATE( SUM( 'Fact'[VAL3] ) ) * 0.01
        )
 
which results in the following table:
EIDAIDVAL1VAL2VAL3NEWVAL
130.10.050.0250.10525
140.20.10.050.2105
150.30.150.0750.31575
230.40.20.10.421
240.50.250.1250.52625
250.60.30.150.6315
330.70.350.1750.73675
340.80.40.20.842
350.90.450.2250.94725

 

Step 2: Summarize by AID and calculate average NEWVAL for each AID

The final step is to generate the following lookup table which is the average of the above table by AID on NEWVAL:

 

Needed Table:

AIDAVG NEWVAL
30.421
40.52625
50.6315

 I have tried the following DAX but am getting the wrong values which is just the average of every NEWVAL:

 

    VAR _AvgNewVal =
        ADDCOLUMNS(
            SUMMARIZE(
                _NewVal,
                'Fact'[AID]
            ),
            "AVG NEW VAL",
                AVERAGEX( _NewVal, [NEWVAL] )
        )

 

Current Results (incorrect):

AIDAVG NEWVAL
30.52625
40.52625
50.52625

 

I am stuck and not sure what to do next. All your help is greatly appreciated!

  • Hey WishAskedSooner ,

    It looks like the issue is coming from how the AVERAGEX function is being used. Try the DAX expression:

     

    VAR _AvgNewVal =
        SUMMARIZE(
            'Fact',
            'Fact'[AID],
            "AVG NEW VAL", AVERAGEX(
                FILTER(
                    _NewVal, 
                    'Fact'[AID] = EARLIER('Fact'[AID])
                ),
                [NEWVAL]
            )
        )

     

    If you found this solution helpful, please consider accepting it and giving it a kudos (Like) it’s greatly appreciated and helps others find the solution more easily.


    Best Regards,
    Nasif Azam

3 Replies

  • Hey WishAskedSooner ,

    It looks like the issue is coming from how the AVERAGEX function is being used. Try the DAX expression:

     

    VAR _AvgNewVal =
        SUMMARIZE(
            'Fact',
            'Fact'[AID],
            "AVG NEW VAL", AVERAGEX(
                FILTER(
                    _NewVal, 
                    'Fact'[AID] = EARLIER('Fact'[AID])
                ),
                [NEWVAL]
            )
        )

     

    If you found this solution helpful, please consider accepting it and giving it a kudos (Like) it’s greatly appreciated and helps others find the solution more easily.


    Best Regards,
    Nasif Azam

      • Nasif_Azam's avatar
        Nasif_Azam
        Super User

        Thank you so much for your kind words! I’m glad I could help. 😊