Forum Discussion

Maneko's avatar
Maneko
New Member
2 years ago
Solved

using field parameter inside a measure

Hi there, It is possible to use a field parameter inside a measure?   I have a report where I display in a line chart different moving averages for one specific column (DJM). Each line is drawn u...
  • OwenAuger's avatar
    2 years ago

    Hi Maneko 

    Field parameters cannot be used within a DAX expression to create a "dynamic" reference to the underlying field.

     

    However there are different methods to get the result you want.

    I would suggest:

    1. Use measures instead of columns within your prmField field parameter.
    2. Create a calculation group with a single calculation item to handle the moving average.

    The field parameter then allows selection of which measure(s) to visualize (i.e. which column to sum) and the calculation group applies the moving average calculation to the selected measure(s).

     

    Attached is my attempt using your PBIX.

     

    1. Created measures to SUM each column:

     

    DJM Sum = 
    SUM ( Carro[DJM] )
    
    GOL Sum = 
    SUM ( Carro[GOL] )
    
    // etc.

     

    2. Updated the field parameter to reference these measures:

     

    -------------------------------
    -- Calculated Table: 'prmField'
    -------------------------------
    TABLE prmField = 
        {
            ("DJM", NAMEOF([DJM Sum]), 0),
            ("GOL", NAMEOF([GOL Sum]), 1),
            ("IBI", NAMEOF([IBI Sum]), 2),
            ("LHB", NAMEOF([LHB Sum]), 3),
            ("SAO", NAMEOF([SAO Sum]), 4),
            ("SCA", NAMEOF([SCA Sum]), 5)
        }

     

    3. Created this calculation group:

     

    --------------------------------------
    -- Calculation Group: 'Moving Average'
    --------------------------------------
    CALCULATIONGROUP 'Moving Average'[Moving Average Option]
    
        CALCULATIONITEM "SMA (# weeks)" = 
            VAR MaxDate = MAX ( Carro[Date] )
            RETURN
                AVERAGEX (
                    DATESBETWEEN (
                        Carro[Date],
                        MaxDate - ( [prmMA_value] - 1 ) * 7,
                        MaxDate
                    ),
                    SELECTEDMEASURE ( )
                )

     

    4. Then assemble the chart with the field parameter on Y-axis and the calculation item applied as a visual-level filter:

    Hopefully this helps even if it needs to be tweaked.

     

    Regards

    Owen 🙂

     

    P.S. Only super users can directly attach files to posts, so you have to use cloud storage or similar (as you have already).