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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Aahil10
Frequent Visitor

Custom Time Intelligence Function

Hello,

I've managed to use the TotalMTD function to calulate variance on a monthly level - However we use production month which tends to be different for celandar month. So for example, whilst May ends on May 31, the production month May ends On June 5 as seen below:

Aahil10_0-1622763973920.png

My question is, is there anyway for me to filter the MTD meausre or use a different method to filter my results based on production month - Can this be done by creating a custom date table where the first 5 days of June fall under Month 5?

 

Or is there a simpler method to this?

1 ACCEPTED SOLUTION

Hi @Aahil10 

In your screenshot, you build custom columns in Power Query Editor(M query). I build calculated column in Report view (Dax). Try to use my dax code to build calculated column.

For reference: Create calculated columns in Power BI Desktop

1.png

If you want to calcualte MTD for each "Month", you can build a Fiscal Month column.

Fiscal Month = RANKX(FILTER('Calendar','Calendar'[Year] = EARLIER('Calendar'[Year])),'Calendar'[Month Start Date],,ASC,Dense)

Ex: Jan Prod Month is Jan 1- Jan 30 is Month 1, 

1.png

May 1 - June 5 is Month 5.

2.png

Then you can calculate MTD by Fiscal Month for each Fiscal Month.

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

View solution in original post

5 REPLIES 5
amitchandak
Super User
Super User

@Aahil10 , You can create month start and end date like this for your the custom month in your date table

 

Month Start Date =  eomonth([date]-5, -1) +5

Month End Date =  eomonth([date]-5, 0) +4

 

Create a rank column too

 

Month Rank = RANKX(all('Date'),'Date'[Month Start date],,ASC,Dense)

Month Rank = RANKX(all('Date'),'Date'[Month Start date],,ASC,Dense)
Month Day = DATEDIFF('Date'[Month Start Date],'Date'[Date],Day)+1

 

 

Try measures like

This Month = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])))
Last Month = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])-1))

 

 

MTD = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank]) && [Month Day] <=max([Month Day])))
LMTD = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])-1 && [Month Day] <=max([Month Day])))

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Hey @amitchandak,

Thanks for the quick reply!

I created a date table from scratch to try this and got the following results:

Aahil10_0-1622778584990.png

I see the logic and it could potentially solve the problem. However, I'm wondering if the offset for every month is different, will it still work? Because it iterates the entire table with the query you rpovided.

 

For example, Jan Prod Month is Jan 1- Jan 30, so only one day is excluded. Whereas in my first example, May overflows into June by 5 days.

Basically, each month is different and I'm wondering if this solution can account for that?

Thank you!

 

Hi @Aahil10 

If your have different logic in each month, you can try switch function to build calculated columns.

Month Start Date = 
VAR _Minus =
    SWITCH (
        'Calendar'[Month],
        1, -1,
        2, 0,
        3, 0,
        4, 0,
        5, 0,
        6, 5,
        7, 0,
        8, 0,
        9, 0,
        10, 0,
        11, 0,
        0
    )
VAR _Add =
    SWITCH (
        MONTH(EOMONTH ( 'Calendar'[Date] - _Minus, -1 )+1),
        1, 1,
        2, 0,
        3, 1,
        4, 1,
        5, 1,
        6, 6,
        7, 1,
        8, 1,
        9, 1,
        10, 1,
        11, 1,
        1
    )
VAR _Result =
    EOMONTH ( 'Calendar'[Date] - _Minus, -1 ) +_Add
RETURN
_Result
Month End Date = 
VAR _Minus =
    SWITCH (
        'Calendar'[Month],
        1, -1,
        2, 0,
        3, 0,
        4, 0,
        5, 0,
        6, 5,
        7, 0,
        8, 0,
        9, 0,
        10, 0,
        11, 0,
        0
    )
VAR _Add =
    SWITCH (
        MONTH ( EOMONTH ( 'Calendar'[Date] - _Minus, 0 ) ),
        1, -1,
        2, 0,
        3, 0,
        4, 0,
        5, 5,
        6, 0,
        7, 0,
        8, 0,
        9, 0,
        10, 0,
        11, 0,
        0
    )
VAR _Result =
    EOMONTH ( 'Calendar'[Date] - _Minus, 0 ) + _Add
RETURN
    _Result

Result is as below.

Jan Prod Month is Jan 1- Jan 30,

1.png

May overflows into June by 5 days

2.png

You can change the number in switch by your requirement.

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

 

 

Hi @v-rzhou-msft ,

Thanks for your detailed response - really appreciate it! I think this may actually work, however, 

I ran into an error trying to incorporate your formula:

Aahil10_0-1623206631040.png

Not sure how to resolve this. Am I doing something wrong? 

Also,

Would you know whether the MTD Function will work within the new dates that we have set in the above table, or whether I have to create new formulas to get a month to date total?

 

Thanks again!

Hi @Aahil10 

In your screenshot, you build custom columns in Power Query Editor(M query). I build calculated column in Report view (Dax). Try to use my dax code to build calculated column.

For reference: Create calculated columns in Power BI Desktop

1.png

If you want to calcualte MTD for each "Month", you can build a Fiscal Month column.

Fiscal Month = RANKX(FILTER('Calendar','Calendar'[Year] = EARLIER('Calendar'[Year])),'Calendar'[Month Start Date],,ASC,Dense)

Ex: Jan Prod Month is Jan 1- Jan 30 is Month 1, 

1.png

May 1 - June 5 is Month 5.

2.png

Then you can calculate MTD by Fiscal Month for each Fiscal Month.

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.