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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

3 month rolling calculation error

Hi,

I have a 2 data sources and need to do some operations between them. I have created a sample to represent the real life solution that I need.

Table Sales:

dateSoldSectorIdsaleNPS
01/02/2022A1good
01/01/2022A2bad
01/01/2022A3good
01/01/2022B4bad
01/12/2021A5bad
01/12/2021B6good
01/12/2021B7good
01/11/2021A8good
01/11/2021A9good
01/11/2021A10good


Table Weights:

SectorYearWeight
A20210,7
B20210,3
A20220,8
B20220,2

 

Below are all the variables that I created:

 

good = COUNTROWS(
        CALCULATETABLE(Sales
            , DATESINPERIOD(Sales[dateSold],SELECTEDVALUE('Calendar'[MonthYear]),-3,MONTH)
            , Sales[NPS] == "good"
)    )

bad = 
COUNTROWS(
        CALCULATETABLE(Sales
            , DATESINPERIOD(Sales[dateSold],SELECTEDVALUE('Calendar'[MonthYear]),-3,MONTH)
            , Sales[NPS] == "bad"
)    )

total = COUNTROWS(
        CALCULATETABLE(Sales
            , DATESINPERIOD(Sales[dateSold],SELECTEDVALUE('Calendar'[MonthYear]),-3,MONTH)
)    )

NPSCalculated = DIVIDE([good],[total])-DIVIDE([bad],[total]) 

 

And to connect the tables I did this on both tables:

 

WeightsKey = CONCATENATE(Weights[Year], Weights[Sector])

 

And the connection is like this:

Renanrdo_2-1660855216111.png
I have already created a calculated measure that counts sales for the last 3 months, and consider all the sectors in this period.
But now I need to to calculate this measure by month and multiply it by the weight of that year. This is what I got and is not working:

 

FinalValue = 
VAR BASE = CROSSJOIN ( VALUES ('Calendar'[MonthYear]), VALUES(Weights[Sector]) )

VAR BASE2 = ADDCOLUMNS(BASE, "Ano", YEAR('Calendar'[MonthYear]))

VAR BASE3 = NATURALINNERJOIN(BASE2,Weights)

VAR BASE4 = ADDCOLUMNS(BASE3, "NPS_FINAL", [Weight] * [NPSCalculated])

RETURN
    SUMX ( BASE4, [NPS_FINAL] )

 

This is returning 0,33 for 2022-02 as example, and it should be returning 0,067 acording to the table below:

SectorbadgoodTotalNPSCalculatedWeightFinal Value
A2240,000,80
B1230,330,20,067


Below I leave an example of what is happening on the background with some things right and some wrong:

Renanrdo_0-1660854341910.png

The operation for 2021-12 completely right, but for 2022-02 it is considering 2021 weight, which it shouldnt be considered. Also, note that 2022-02 has no Sector B on the table, as it should consider the previous appeareances of B in the previous 3 months, so this is also right.

Renanrdo_1-1660854485451.png

This is the final table and result I will use, and as I said, 2021-12 is right, but every other MonthYear that the period of 3 months before include another year is broken.

I know it looks like a lot, but I'm very close to the solution, could someone please help me??

2 REPLIES 2
amitchandak
Super User
Super User

@Anonymous , You join it with the calendar table marked as date table and create measure like

 

example

 

Rolling 3 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date ],MAX('Date'[Date ]),-3,MONTH))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

Hi @amitchandak, thank you for your reply!

 

Actually, there is no issue on calculating the rolling 3 period, the issue is on multiplying it by the respective weight of its Sector, like I'm trying to do in FinalValue field.

The rolling 3 is working fine, as done by the variables BAD, GOOD and TOTAL.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors