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
dannytango
Regular Visitor

How to calculate a rolling product formula based on date slicer

Hi, 

 

I'm trying to calculate to do the product calculation to each row, but I can only get it to calculate from the beginning of dataset instead of calculating based on the slicer date.  As you can see in the first picture and the second picture.  In the second picture after the slicer is applied, it should start calculating from the beginning again and the cumulative result should be 94.8%.  

 

Thank you for your help.

 

dannytango_1-1603505973995.png

 

 

dannytango_0-1603504749889.png

 

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

Hi @dannytango ,

According to my understand, you want to calculate the accumulate of measure dynamically based on Slicer, right?

 

You could follow these steps:

1. Create a new table with Date for Slicer.

2. Use the following formula:

 

test =
VAR _min =
    MIN ( 'forSlicer'[Date] )
VAR _max =
    MAX ( 'forSlicer'[Date] )
RETURN
    CALCULATE (
        PRODUCTX ( TCMMonthlyReturnsInput, TCMMonthlyReturnsInput[MRTCM] + 1 ),
        FILTER (
            TCMMonthlyReturnsInput,
            'TCMMonthlyReturnsInput'[Date] >= _min
                && 'TCMMonthlyReturnsInput'[Date] <= MAX ( 'TCMMonthlyReturnsInput'[Date] )
        )
    ) - 1

 

 

 3. Apply the measure below to filter pane( set =1) 

 

Measure =
VAR _min =
    MIN ( 'forSlicer'[Date] )
VAR _max =
    MAX ( 'forSlicer'[Date] )
RETURN
    IF (
        MAX ( 'TCMMonthlyReturnsInput'[Date] ) >= _min
            && MAX ( 'TCMMonthlyReturnsInput'[Date] ) <= _max,
        1,
        0
    )

 

4. Calculate the accumulate value:

 

Final Accumulate test =
CALCULATE (
    SUMX ( 'TCMMonthlyReturnsInput', [test] ),
    FILTER (
        ALL ( 'TCMMonthlyReturnsInput' ),
        'TCMMonthlyReturnsInput'[Date] <= MAX ( 'TCMMonthlyReturnsInput'[Date] )
            && [Measure] = 1
    )
)

 

 My visualization looks like this:

10.27.5.1.gif

Here is the pbix file.

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please upload some insensitive data samples and expected output.

 

Best Regards,
Eyelyn Qin

View solution in original post

5 REPLIES 5
v-eqin-msft
Community Support
Community Support

Hi @dannytango ,

According to my understand, you want to calculate the accumulate of measure dynamically based on Slicer, right?

 

You could follow these steps:

1. Create a new table with Date for Slicer.

2. Use the following formula:

 

test =
VAR _min =
    MIN ( 'forSlicer'[Date] )
VAR _max =
    MAX ( 'forSlicer'[Date] )
RETURN
    CALCULATE (
        PRODUCTX ( TCMMonthlyReturnsInput, TCMMonthlyReturnsInput[MRTCM] + 1 ),
        FILTER (
            TCMMonthlyReturnsInput,
            'TCMMonthlyReturnsInput'[Date] >= _min
                && 'TCMMonthlyReturnsInput'[Date] <= MAX ( 'TCMMonthlyReturnsInput'[Date] )
        )
    ) - 1

 

 

 3. Apply the measure below to filter pane( set =1) 

 

Measure =
VAR _min =
    MIN ( 'forSlicer'[Date] )
VAR _max =
    MAX ( 'forSlicer'[Date] )
RETURN
    IF (
        MAX ( 'TCMMonthlyReturnsInput'[Date] ) >= _min
            && MAX ( 'TCMMonthlyReturnsInput'[Date] ) <= _max,
        1,
        0
    )

 

4. Calculate the accumulate value:

 

Final Accumulate test =
CALCULATE (
    SUMX ( 'TCMMonthlyReturnsInput', [test] ),
    FILTER (
        ALL ( 'TCMMonthlyReturnsInput' ),
        'TCMMonthlyReturnsInput'[Date] <= MAX ( 'TCMMonthlyReturnsInput'[Date] )
            && [Measure] = 1
    )
)

 

 My visualization looks like this:

10.27.5.1.gif

Here is the pbix file.

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please upload some insensitive data samples and expected output.

 

Best Regards,
Eyelyn Qin

dannytango
Regular Visitor

The code returned the calculation total correctly, but doesn't calculate for each row.  Couldn't upload pbix file, so I have to put paste it into table below.  Thanks.

 

DateMRTCM
1/31/20170.66%
2/28/20170.21%
3/31/20170.96%
4/30/20170.66%
5/31/20175.21%
6/30/20174.20%
7/31/20179.14%
8/31/20178.62%
9/30/20173.12%
10/31/201714.91%
11/30/20173.09%
12/31/2017-0.26%
1/31/201811.04%
2/28/20184.16%
3/31/2018-7.44%
4/30/2018-2.48%
5/31/201824.14%
6/30/20180.99%
7/31/2018-0.97%
8/31/20188.98%
9/30/20180.84%
10/31/2018-16.97%
11/30/2018-7.69%
12/31/2018-31.07%
1/31/201923.02%
2/28/20190.24%
3/31/20198.83%
4/30/201910.56%
5/31/2019-23.15%
6/30/201929.09%
7/31/201910.17%
8/31/2019-7.13%
9/30/20192.98%
10/31/20198.71%
11/30/20193.65%
12/31/20193.76%
1/31/20207.39%
2/29/20200.98%
3/31/2020-3.48%
4/30/202024.12%
5/31/20208.34%
6/30/20207.90%
7/31/20206.49%
8/31/20203.00%
9/30/20200.01%
dannytango
Regular Visitor

Thanks, @AllisonKennedy 

 

Sorry, but I'm pretty new to DAX so I don't think I've applied your advice properly.  This is my attempt following your advice:

 

TCMCumulative test =
var startDate = MIN(TCMMonthlyReturnsInput[Date]) return
CALCULATE(PRODUCTX(TCMMonthlyReturnsInput,TCMMonthlyReturnsInput[MRTCM]+1),
ALLSELECTED(TCMMonthlyReturnsInput[Date],TCMMonthlyReturnsInput[MRTCM]),
FILTER(TCMMonthlyReturnsInput,startdate<=MAX(TCMMonthlyReturnsInput[Date])))-1
 
This got me the final result calculated right, but each row is still not being calculated, it just reiterating the original data.
 
Thanks again for your help.
 
 

Screenshot 2020-10-24 001150.png

 
 

 

 

Try updating to something like this;
TCMCumulative test =
VAR startDate =
    CALCULATE (
        MIN ( TCMMonthlyReturnsInput[Date] ),
        ALLSELECTED ( TCMMonthlyReturnsInput[Date] )
    )
RETURN
    CALCULATE (
        PRODUCTX (
            FILTER ( TCMMonthlyReturnsInput, startdate <= TCMMonthlyReturnsInput[Date] ),
            TCMMonthlyReturnsInput[MRTCM] + 1
        ),
        ALLSELECTED ( TCMMonthlyReturnsInput[Date], TCMMonthlyReturnsInput[MRTCM] )
    ) - 1


If that still doesn't work, can you please provide sample data?


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

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

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
Super User
Super User

You will need to define the MAX(Date) as a variable before your ProductX, this ensures you get the max date prior to adding the row context that the ProductX function gives of row by row.

That also means you'll likely need to experiment with ALLSELECTED within the MaxDate variable.

Let us know if you need more info or resources on variables, otherwise hopefully that helps.

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

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

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

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.

Top Solution Authors