Forum Discussion

Andrew-HLP's avatar
Andrew-HLP
Icon for Helper I rankHelper I
1 year ago
Solved

Row & Filter Context / DAX formula

As a relative newbie to DAX, I am struggling with row & filter context.    BACKGROUND   I am building a financial dashboard.  The fact table has data for each account in the  P&L / Income Stateme...
  • Andrew-HLP's avatar
    Andrew-HLP
    1 year ago

    Thank you  for all your help  ๐Ÿ™‚

     

    GOOD NEWS #1: With a bit of tweaking of the code you suggested I was able to (i) created a disconnected dates table, (ii) created measures to work with that table.

     

    GOOD NEWS #2: After a lot of trial and error, I was able to limit the X-axis of my chart using the disconnected dates table.  The soltion may not be the neatest in the world, but it works!

     

    HERE IS THE DETAILED IN CASE IT HELPS ANYONE ELSE...

     

    (i) I made an exact copy of my dates table [Fiscal Calendar] and called it [Fiscal Calendar (Disconnected)].  I have a 31st March  year end, so it was easier to replicate the table then build a slimmed down version and it gives me the flexibility to use any of the columns.

     

    (ii)  I then created two new measures which work in conjunction with [Fiscal Calendar (Disconnected)].  The measure only returns a value if the date from the disconnected calandar (i.e. the date on the x-axis of my chart) is within the required 13 month range, otherwise it returns blank().  

     

    GL Value Period New (13 Months) =

     

    var _ldmend = [Selected Date]

    var _ldmstart = EOMONTH([Selected Date],-13)+1

    var _ld13m =  EOMONTH(lastdate('Fiscal Calendar (Disconnected)'[Date]),0)

     

    RETURN

     

    if ( _ld13m >= _ldmstart && _ld13m <= _ldmend ,

     

    CALCULATE(

        [GL Value Period],

            'Fiscal Calendar'[Date] = _ld13m

        ),

     

    blank()

     

    )

     

     

    GL Value Run Tot New (13 Months) =

     

    var _ldmend = [Selected Date]

    var _ldmstart = EOMONTH([Selected Date],-13)+1

    var _ld13m =  EOMONTH(lastdate('Fiscal Calendar (Disconnected)'[Date]),0)

     

    RETURN

     

    if ( _ld13m >= _ldmstart && _ld13m <= _ldmend ,

     

    CALCULATE(

        [GL Value Run Tot],

            'Fiscal Calendar'[Date] = _ld13m

        ),

     

    blank()

     

    )

     

    (iii) The nested [Selected Date] measure converts the selected month and fiscal year (from my slicers) to a date. 

     

     

    This provides a nice clean solution and avoids referencing the filtered [Fiscal Calendar] table which cauises unexpected interactions with other measures).  I could have written code to do the conversion, but as the information is already stored in the dates table, it seemed less error prone to simply look up the relevant date.

     

    Selected Date =

     

    VAR _SelectedFiscalMonthName = max('Lookup Month'[Month])

    VAR _SelectedFiscalYear = max('Lookup Year'[FiscalYear])

     

    RETURN

     

     calculate(

        max('Fiscal Calendar (Disconnected)'[EOMDate]),

        all('Fiscal Calendar (Disconnected)'),

           'Fiscal Calendar (Disconnected)'[FiscalMonthName]=_SelectedFiscalMonthName

           && 'Fiscal Calendar (Disconnected)'[FiscalYear]=_SelectedFiscalYear

     

    NB: As we have a 31st March year end, Sept 2024-25 converts to 30/09/2024 wheras Mar 2024-25 converts to 31/03/2025.  

     

    SET-UP OF VISUAL / OUTPUT

     

     

     

     

    NOTE RE. 13 MONTH WINDOW

     

    I didn't use a calculated table like the [13MonthWindow] table as you suggested as calculate tables are only "recalculated if any of the tables they pull data from are refreshed or updated" so they can't be dynically updated by a slicer (see https://learn.microsoft.com/en-us/power-bi/transform-model/desktop-calculated-tables) for more information about this).

     

    I hope this helps others who may be facing the same issue.