cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
j_w
Helper IV
Helper IV

How to calculate the sales total with changed date range based on the current filters

The Report has a Sales table with Date, Product, Amount$ fields, also have two filters, Date and Product matching the same name fields.

 

The financial year end date is 31 March.

 

How to write a measure to calculate the year-to-date total amount$ based on the current two filters?

 

For example:

Case 1

If the user selected Date filter range from 2020-09-01 to 2020-07-14,

then the year-to-date total amount$ will be from 2020-04-01 to 2020-07-14.

Case 2

If the user selected Date filter range from 2020-02-01 to 2020-08-31, with Product filter Product01 selected

then the year-to-date total amount$ will be from 2019-04-01 to 2020-08-31 for Product01.

 

Notes: in case 2, since the user selected Date filter range covers two financial years, so the year-to-date should also cover two financial years from 2019-04-01 to the max date of the user selected date range 2020-08-31.

 

1 ACCEPTED SOLUTION
v-zhenbw-msft
Community Support
Community Support

Hi @j_w ,

 

We can create a FY date table that has now relationship with the Sales table and a measure to meet your requirement.

 

1. Create a date table and add two columns.

 

Date table = CALENDARAUTO()

 

howto1.jpg

 

FY = 
var _FY = if(month([Date])<=3,year([Date])-1,year([Date]))
return
_FY
Star FY = DATE('Date table'[FY],4,1)

 

howto2.jpg

 

2. Then we can create a measure and use the Date table [Date] to be a slicer.

 

Measure =
VAR _min_date =
    MIN ( 'Date table'[Date] )
VAR _max_date =
    MAX ( 'Date table'[Date] )
VAR _currentFYmonth =
    CALCULATE (
        MAX ( 'Date table'[Star FY] ),
        FILTER ( 'Date table', 'Date table'[Date] = _min_date )
    )
VAR _result_date =
    IF (
        _min_date > _currentFYmonth,
        _currentFYmonth,
        DATE ( YEAR ( _currentFYmonth ) - 1, MONTH ( _currentFYmonth ), 1 )
    )
RETURN
    CALCULATE (
        SUM ( 'Table'[amount] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Date] >= _result_date
                && 'Table'[Date] <= _max_date
        )
    )

 

howto3.jpg

 

howto4.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that we have shared?

 

Best regards,

 

Community Support Team _ zhenbw

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

 

BTW, pbix as attached.

View solution in original post

6 REPLIES 6
v-zhenbw-msft
Community Support
Community Support

Hi @j_w ,

 

We can create a FY date table that has now relationship with the Sales table and a measure to meet your requirement.

 

1. Create a date table and add two columns.

 

Date table = CALENDARAUTO()

 

howto1.jpg

 

FY = 
var _FY = if(month([Date])<=3,year([Date])-1,year([Date]))
return
_FY
Star FY = DATE('Date table'[FY],4,1)

 

howto2.jpg

 

2. Then we can create a measure and use the Date table [Date] to be a slicer.

 

Measure =
VAR _min_date =
    MIN ( 'Date table'[Date] )
VAR _max_date =
    MAX ( 'Date table'[Date] )
VAR _currentFYmonth =
    CALCULATE (
        MAX ( 'Date table'[Star FY] ),
        FILTER ( 'Date table', 'Date table'[Date] = _min_date )
    )
VAR _result_date =
    IF (
        _min_date > _currentFYmonth,
        _currentFYmonth,
        DATE ( YEAR ( _currentFYmonth ) - 1, MONTH ( _currentFYmonth ), 1 )
    )
RETURN
    CALCULATE (
        SUM ( 'Table'[amount] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Date] >= _result_date
                && 'Table'[Date] <= _max_date
        )
    )

 

howto3.jpg

 

howto4.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that we have shared?

 

Best regards,

 

Community Support Team _ zhenbw

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

 

BTW, pbix as attached.

amitchandak
Super User
Super User

@j_w ,

You can use start of year with end date of year given in function. Or you can start of year from you table . Use date table as you need Continuous dates for this

Measure =
var _min = minx(allselected(Date),startofyear([Date[Date],"3/31")) //or use start of year from calendar
var _max = maxx(allselected(Date),[Date[Date])
return
calculate([measure], filter(all(Date), Date[DAte]<=_max && Date[DAte]>=_min))

 

FY of your choice - https://community.powerbi.com/t5/Community-Blog/Creating-Financial-Calendar-Decoding-Date-and-Calend...



Power BI Features || Datamarts: https://youtu.be/8tskWsJTEpg || Field Parameters : https://youtu.be/lqF3Wa1FllE?t=70
Time Intelligence Decoded : https://youtu.be/aU2aKbnHuWs&t=145s || 50+Power Query List Functions : Learn Power Query List
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !! Proud to be a Super User! !!
Dashboard of My Blogs !! Connect on Linkedin !! Subscribe to my youtube Channel !!
Want To Learn Power BI | Beginners !! Advance Concepts !! Power BI For Tableau User !!
amitchandak
Super User
Super User

@j_w ,

 

Try like

Measure =
var _min = minx(allselected(Date),startofyear([Date[Date],"3/31")) // take start of year from you date table
var _max = maxx(allselected(Date),[Date[Date])
return
calculate([measure], filter(all(Date), Date[DAte]<=_max && Date[DAte]>=_min))

 

Make sure you use a date table with continuous date



Power BI Features || Datamarts: https://youtu.be/8tskWsJTEpg || Field Parameters : https://youtu.be/lqF3Wa1FllE?t=70
Time Intelligence Decoded : https://youtu.be/aU2aKbnHuWs&t=145s || 50+Power Query List Functions : Learn Power Query List
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !! Proud to be a Super User! !!
Dashboard of My Blogs !! Connect on Linkedin !! Subscribe to my youtube Channel !!
Want To Learn Power BI | Beginners !! Advance Concepts !! Power BI For Tableau User !!
CNENFRNL
Super User
Super User

Hi, @j_w , you can assign a specific date as year_end_date in TOTALYTD func, say "3/31" for your case,

TOTALYTD(<expression>,<dates>[,<filter>][,<year_end_date>])

 Official doc for more details,

https://docs.microsoft.com/en-us/dax/totalytd-function-dax


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is beyond their comprehension!

DAX is simple, but NOT EASY!

AllisonKennedy
Super User
Super User

Not sure about your Case 2 example, should that only work if they select a date between 2019-04-01 and 2020-03-31?

See if this helps: https://blog.enterprisedna.co/learn-how-to-calculate-sales-financial-year-to-date/

It's one way to use financial YTD calculations by using the built in time intelligence functions in Power BI with Year End date specified. You must have a DimDate table for this to work properly. https://excelwithallison.blogspot.com/2020/04/dimdate-what-why-and-how.html

Please @mention me in your reply if you want a response.

Check out my Olympics report with live stats - KUDOS much appreciated

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

@AllisonKennedy  I have added some explanation:

In case 2, since the user selected Date filter range covers two financial years, so the year-to-date should also cover two financial years from 2019-04-01 to the max date of the user selected date range 2020-08-31.

Helpful resources

Announcements
September Update

Check it Out!

Click here to learn more about the September 2022 updates!

Power BI Show Episode 10

The Power BI Community Show

Join us on October 3 at 11 am PST when Amit Chandak, a Power BI Super User, will demo how to use Field Parameters to make reports more dynamic.

Power BI Dev Camp Session 26

New Date - Check it Out!

Mark your calendars and join us on Thursday, October 6 at 11a PDT for a great session with Ted Pattison!

Top Solution Authors