Forum Discussion

chefe's avatar
chefe
Icon for Helper II rankHelper II
10 years ago
Solved

PERCENTILE inside CALCULATE does not (anymore) recognise data fields

Hi there,   I recently figured out that I can write an in-memory table in DAX. I just set-up a table that spans from 1-100, and I want to add a column returning the percentile for 1-100 for a given...
  • OwenAuger's avatar
    10 years ago

    Hi Chefe,

     

    I think your best bet is to create a variable to store the value of the current row's [pct] before calling CALCULATE:

     

    Table =
    VAR dat =
        DATE ( 2016; 1; 1 )
    VAR pct =
        SUMMARIZE (
            ADDCOLUMNS ( CALENDAR ( dat; dat + 99 ); "pct"; [Date] - dat + 1 );
            [pct]
        )
    VAR pct_value_calculate =
        ADDCOLUMNS (
            pct;
            "pct_value";
            VAR CurrentPct = [pct]
            RETURN
                CALCULATE (
                    PERCENTILE.INC (
                        'Foreign exchange rates'[INDIRECT_FX_CCY_QUOTE];
                        CurrentPct / 100
                    );
                    'Foreign exchange rates'[FX_CCY] = "USD"
                )
        )
    RETURN
        pct_value_calculate

    I think a rough explanation is that when CALCULATE triggers a context transition (i.e. row context becomes filter context), any columns within the row context that were created with ADDCOLUMNS (like [pct]) are no longer accessible within the first argument of CALCULATE. This is because 'created columns' have no lineage so cannot be converted to filter context.

     

     

    This means you can refer to [pct] outside CALCULATE or in one of CALCULATE's filter arguments, but not in CALCULATE's first argument. So the only way I can think of to access the value of [pct] is to save it in a variable before calling CALCULATE.

     

    Owen :)