Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Pinoo39
Frequent Visitor

Calculate a measure for custom date range

Hey All

 

I'm trying to calculate a measure (e.g. Avg Sales) for the period of previous 11 month from the current selected month, here is what I wrote :

AVGSales Snapshot =

VAR CurrentMonth = SELECTEDVALUE(DimDate[FinMonthNumber])
VAR CurrentYear = SELECTEDVALUE(DimDate[FinYear])
VAR MaxMonthNum = CALCULATE(MAX(DimDate[FinMonthNumber]), ALL(DimDate))

RETURN
IF(HASONEVALUE(DimDate[FinMonthNumber]),
SUMMARIZE(
FILTER(ALL(DimDate),
(DimDate[FinMonthNumber] in VALUES(FILTER(DimDate, DimDate[FinMonthNumber] <= CurrentMonth)) && DimDate[FinYear] = CurrentYear - 1) &&
(DimDate[FinMonthNumber] > CurrentMonth && DimDate[FinYear] = CurrentYear)),
"AVGSales Snapshot", [Average Sales]),
BLANK())


Since I'm using Custom calendar (Financial Calendar) which is not matching with the normal calendar, Time Intelligence functions are not going to work.

Any Idea ?
1 ACCEPTED SOLUTION
daxer-almighty
Solution Sage
Solution Sage

@Pinoo39 

 

I instructed you that you should create a column with consecutive numbering of fiscal months. I'm not talking about 1-12 but CONSECUTIVE numbers from 1 up to the last month you have in your calendar. Year has nothing to do with this.

View solution in original post

6 REPLIES 6
daxer-almighty
Solution Sage
Solution Sage

@Pinoo39 

 

I instructed you that you should create a column with consecutive numbering of fiscal months. I'm not talking about 1-12 but CONSECUTIVE numbers from 1 up to the last month you have in your calendar. Year has nothing to do with this.

daxer-almighty
Solution Sage
Solution Sage

// FiscalMonthID starts with 1 and goes up by 1
// until the very last fiscal month you have in
// the table.

AVGSales Snapshot =
var __minNumberOfMonths = 12
var __oneFullFiscalMonthVisible =
    var __oneMonthVisible = HASONEVALUE( DimDate[FiscalMonthID]
    var __dayCount = COUNTROWS( DimDate )
    var __fullMonthDayCount =
        CALCULATE(
            COUNTROWS( DimDate ),
            DimDate[FiscalMonthID] IN DISTINCT( DimDate[FiscalMonthID] ),
            ALL( DimDate )
        )
    return
        __oneMonthVisible
        &&
        ( __dayCount = _fullMonthDayCount )
        
var __thereIsEnoughMonthsToCalcOver =
    SELECTEDVALUE( DimDate[FiscalMonthID] ) >= __minNumberOfMonths
    
var __output =
    if( 
        __oneFullFiscalMonthVisible
        &&
        __thereIsEnoughMonthsToCalcOver,
        
        var __currentMonthID = SELECTEDVALUE( DimDate[FiscalMonthID] )
        var __lowerBound = __currentMonthID - __minNumberOfMonths + 1
        var __upperBound = __currentMonthID - 1
        return
            AVERAGEX(
                CALCULATETABLE(
                    Distinct( DimDate[FiscalMonthID] ),
                    DimDate[FiscalMonthID] >= __lowerBound,
                    DimDate[FiscalMonthID <= __upperBound,
                    all( DimDate )
                ),
                [Average Sales]
            )
    )
return
    __output

Wow, Thanks for your help
would you please explain how it filters the year ?
If FiscalMonthID = 12 then the previous 12 month will be in the same year, but what if FiscalMonthID = 7 ?

Pinoo39
Frequent Visitor

I do have a Month Number Column ("FinMonthNumber") starting from 1 as the beginning of Fincancial Year.
The question is how to select multiple months.  something like FinMonthNumber <= 6 will not work for Financial Months of 1 up to 6

daxer-almighty
Solution Sage
Solution Sage

Such a measure will be much simpler if you number your financial months consecutively starting with 1 (the very first month in your calendar). Why mess with years and months when you can use month numbers only? If you want to see how to correctly tackle financial calendars, you can go to Custom time-related calculations – DAX Patterns.

Pinoo39
Frequent Visitor

No one Knows how to make it work ?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.