Forum Discussion

dieterbe_vdb's avatar
dieterbe_vdb
Frequent Visitor
2 years ago

Filter issuev - Context transition

Hello, I have an issue with a measure not behaving as I would expect.

 

Fact Table:

DateMRRActivatedDeactivated
1/01/20234510
1/02/20233601
1/03/20237510
1/04/20231200
1/05/20232110
1/06/20237010
1/07/20237610
1/08/20239601
1/09/20236210
1/10/2023700
1/11/20236900
1/12/20237100
1/03/20246210
1/03/20246200
1/03/20244000
1/01/20244210
1/01/20246000
1/01/2024710
1/01/20248801
1/01/20244710
1/02/2024710
1/02/20245210
1/02/20247301
1/02/20249410
1/02/20247700
1/03/20246100
1/03/20249600
1/03/20242010
1/03/20246501
1/03/20246200

 

This Fact Table is linked to a Date table with columns:

  • Date (unique key)
  • IsBeforeThisMonth
  • Year
  • YYYY-MM

Measures:

  • MEASURE _Measures[MRR] = SUMX('Table', 'Table'[MRR])
  • MEASURE _Measures[New MRR] = CALCULATE([MRR], 'Table'[Activated] = 1)
  • MEASURE _Measures[New MRR AVG Not Working] = AVERAGEX(VALUES('Date'[YYYY-MM]), [New MRR])
  •  MEASURE _Measures[New MRR AVG Working] = AVERAGEX(VALUES('Date'[Date]), [New MRR])

I have two page filters:

  • filtering on YEAR 
  • filtering on Is BeforeThisMonth

In Power BI, I use a Table visualisation with Year, YYYY-MM as dimensions, New_MRR, New_MRR_AVG_Not_Working and New_MRR_AVG_Working as measures.

If I limit the page filter to a filter on YEAR (being 2024) then all measures are working as expected.

Query:

 

// DAX Query
DEFINE
    VAR __DS0FilterTable = 
        TREATAS({2024}, 'Date'[Year])

    VAR __DS0Core = 
        SUMMARIZECOLUMNS(
            ROLLUPADDISSUBTOTAL(ROLLUPGROUP('Date'[Year], 'Date'[YYYY-MM]), "IsGrandTotalRowTotal"),
            __DS0FilterTable,
            "New_MRR", '_Measures'[New MRR],
            "New_MRR_AVG_Not_Working", '_Measures'[New MRR AVG Not Working],
            "New_MRR_AVG_Working", '_Measures'[New MRR AVG Working]
        )

    VAR __DS0PrimaryWindowed = 
        TOPN(502, __DS0Core, [IsGrandTotalRowTotal], 0, 'Date'[Year], 1, 'Date'[YYYY-MM], 1)

EVALUATE
    __DS0PrimaryWindowed

ORDER BY
    [IsGrandTotalRowTotal] DESC, 'Date'[Year], 'Date'[YYYY-MM]

Result

 

YearYYYY-MMIsGrandTotalRowTotalNew_MRRNew_MRR_AVG_Not_WorkingNew_MRR_AVG_Working
  True331110,333333333333331
202424-01False969696
202424-02False153153153
202424-03False828282


However, when I add the filter IsBeforeThisMonth as well then one of the measures (New_MRR_AVG_Not_Working) does not work as expected.

Query

 

// DAX Query
DEFINE
    VAR __DS0FilterTable = 
        TREATAS({2024}, 'Date'[Year])

    VAR __DS0FilterTable2 = 
        TREATAS({TRUE}, 'Date'[IsBeforeThisMonth])

    VAR __DS0Core = 
        SUMMARIZECOLUMNS(
            ROLLUPADDISSUBTOTAL(ROLLUPGROUP('Date'[Year], 'Date'[YYYY-MM]), "IsGrandTotalRowTotal"),
            __DS0FilterTable,
            __DS0FilterTable2,
            "New_MRR", '_Measures'[New MRR],
            "New_MRR_AVG_Not_Working", '_Measures'[New MRR AVG Not Working],
            "New_MRR_AVG_Working", '_Measures'[New MRR AVG Working]
        )
I
    VAR __DS0PrimaryWindowed = 
        TOPN(502, __DS0Core, [IsGrandTotalRowTotal], 0, 'Date'[Year], 1, 'Date'[YYYY-MM], 1)

EVALUATE
    __DS0PrimaryWindowed

ORDER BY
    [IsGrandTotalRowTotal] DESC, 'Date'[Year], 'Date'[YYYY-MM]

 

Result

 

YearYYYY-MMIsGrandTotalRowTotalNew_MRRNew_MRR_AVG_Not_WorkingNew_MRR_AVG_Working
  True331110,333333333333331
202424-01False96110,33333333333396
202424-02False153110,333333333333153
202424-03False82110,33333333333382
202424-04False 110,333333333333 
202424-05False 110,333333333333 
202424-06False 110,333333333333 
202424-07False 110,333333333333 


In one way or another, the additional IsBeforeThisMonth filter results in the iterator AVERAGEX loses the "YYYY-MM" filter contect, I would expect that this measure (although not ideal) would work as well and that this additional filter wouldn't influence the result.

 

If I delete the Year dimension from the table (but keep ons using both page filters), then everything is working fine as well.

Query

 

// DAX Query
DEFINE
    VAR __DS0FilterTable = 
        TREATAS({2024}, 'Date'[Year])

    VAR __DS0FilterTable2 = 
        TREATAS({TRUE}, 'Date'[IsBeforeThisMonth])

    VAR __DS0Core = 
        SUMMARIZECOLUMNS(
            ROLLUPADDISSUBTOTAL('Date'[YYYY-MM], "IsGrandTotalRowTotal"),
            __DS0FilterTable,
            __DS0FilterTable2,
            "New_MRR", '_Measures'[New MRR],
            "New_MRR_AVG_Not_Working", '_Measures'[New MRR AVG Not Working],
            "New_MRR_AVG_Working", '_Measures'[New MRR AVG Working]
        )

    VAR __DS0PrimaryWindowed = 
        TOPN(502, __DS0Core, [IsGrandTotalRowTotal], 0, 'Date'[YYYY-MM], 1)

EVALUATE
    __DS0PrimaryWindowed

ORDER BY
    [IsGrandTotalRowTotal] DESC, 'Date'[YYYY-MM]

 

 

Result

 

YYYY-MMIsGrandTotalRowTotalNew_MRRNew_MRR_AVG_Not_WorkingNew_MRR_AVG_Working
 True331110,333333333333331
24-01False969696
24-02False153153153
24-03False828282



What is going wrong here?
I've attached my pibx file as well.

Problem AVG.pbix

Many thanks!

 

2 Replies

  • dieterbe_vdb ,

    AVERAGEX(VALUES('Date'[YYYY-MM]), [New MRR]) will sum up to the month level, and after that, it will calculate the average. It seems to be working correctly to me.

    AVERAGEX(VALUES('Date'[Date]), [New MRR]) sums up to the date level and then calculates the average. Check the file; it is showing the same as the month because you only have data for three dates.

  • dieterbe_vdb's avatar
    dieterbe_vdb
    Frequent Visitor

    Hello, thanks for your answer amitchandak .

     

    This does work indeed for the first and third example (in my original post) but it doesn't work when I use the page filters IsBeforeThisMonth and Year AND if my table includes the year dimension as well. Question is: why is example 2 not working but 1 (another scenario where I use the Year dimension in my table and only use the IsBeforeThisMonth filter as a page filter) and 3 are? What is different causing the measure not to work in example 2?
    So focussing on example 2 and 3: everything works fine without Year dimension in my table and using both page filters (IsBeforeThisMonth and Year) but after adding the Year dimension in my table the measure calculates the average over the total Year taking abstraction of the YY-MM field.

    This works (last example of my orignal post):

     


    This doesn't (second example of my original post):

     



     Any idea? Many thanks!!