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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
PBIDev01
Advocate I
Advocate I

measure for recursive approach

Hi community,

Im trying to calculate ProjectedStock, but I'm facing a recursive issue :

in the period or the same period as the extraction (for example 202508==period of extracted data is 22/08/2025) :

projectedstock = [V1]+[V2]+Stock_At_Time-[V3+V4].

Till here everything is ok, the problem appears in the calculation of the ProjectedStock for the next periods because the Stock_At_Time should be taken as the ProjectedStock[Period-1]:

for example :

ProjStock(202511) = [V1]+[V2]+ProjectedStock[202510]-[V3+V4].

and

ProjStock(202510) = [V1]+[V2]+ProjectedStock[202509]-[V3+V4].

and

ProjStock(202509) = [V1]+[V2]+ProjectedStock[202508]-[V3+V4].

So, how to do so without having a circular dependencies or recursive issues.

Thank you.

7 ACCEPTED SOLUTIONS
bhanu_gautam
Super User
Super User

@PBIDev01 First, calculate the ProjectedStock for the initial period (e.g., 202508) using a calculated column. This will serve as the base for subsequent calculations.

ProjectedStock_Initial = [V1] + [V2] + Stock_At_Time - ([V3] + [V4])

 

Use a calculated table to iterate over the periods and calculate the ProjectedStock for each subsequent period. This approach avoids circular dependencies by storing intermediate results in a separate table.

ProjectedStockTable =
VAR BasePeriod = 202508
VAR MaxPeriod = 202511
RETURN
GENERATE(
FILTER(
ADDCOLUMNS(
GENERATESERIES(BasePeriod, MaxPeriod, 1),
"ProjectedStock",
VAR CurrentPeriod = [Value]
VAR PreviousPeriodStock =
IF(CurrentPeriod = BasePeriod,
[V1] + [V2] + Stock_At_Time - ([V3] + [V4]),
CALCULATE(
MAX(ProjectedStockTable[ProjectedStock]),
ProjectedStockTable[Period] = CurrentPeriod - 1
)
)
RETURN
PreviousPeriodStock + [V1] + [V2] - ([V3] + [V4])
),
[Value] >= BasePeriod
),
ROW("Period", [Value])
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






View solution in original post

danextian
Super User
Super User

It would be easier for us to come up with a solution if you just provided a sample pbix with confidential data removed.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

Shahid12523
Resident Rockstar
Resident Rockstar

Use this

ProjectedStock =
VAR CurrPeriod = MAX(FactStock[Period])
VAR BasePeriod = 202508 // extraction period
VAR StartStock =
CALCULATE(MAX(FactStock[Stock_At_Time]), FactStock[Period] = BasePeriod)
VAR Movements =
SUMX (
FILTER ( FactStock, FactStock[Period] <= CurrPeriod && FactStock[Period] > BasePeriod ),
FactStock[V1] + FactStock[V2] - FactStock[V3] - FactStock[V4]
)
RETURN
StartStock + Movements

Shahed Shaikh

View solution in original post

AmiraBedh
Super User
Super User

Hello !

I don't think that you need recursion here what I understood is that you need an anchored running balance. (please correct me if I am wrong).

You need to have a proper Date table related to your fact and a [YearMonth] column like 202508 (INT or TEXT). If your V1–V4 are columns you need to wrap them as measures.

 

Net Flow =
VAR Inflow  = COALESCE([V1],0) + COALESCE([V2],0)
VAR Outflow = COALESCE([V3],0) + COALESCE([V4],0)
RETURN Inflow - Outflow

You can create a disconnected table Anchor Period with one column [YearMonth] and let the user pick the extraction period :

Anchor Date =
VAR AnchorYM = SELECTEDVALUE('Anchor Period'[YearMonth])
RETURN
CALCULATE(
    MIN('Date'[Date]),
    ALL('Date'),
    'Date'[YearMonth] = AnchorYM
)

Or if you always want to anchor to the last period that has stock at time:

Anchor Date =
CALCULATE(
    MAX('Date'[Date]),
    FILTER(
        ALL('Date'),
        NOT ISBLANK( CALCULATE([Stock At Time]) )
    )
)

Then projected stock :

Projected Stock =
VAR tDate       = MAX('Date'[Date])
VAR anchorDate  = [Anchor Date]
VAR InitialStock =
    CALCULATE(
        [Stock At Time],
        ALL('Date'[Date]),
        'Date'[Date] = anchorDate
    )
VAR NetSinceAnchor =
    CALCULATE(
        [Net Flow],
        ALL('Date'[Date]),                               
        DATESBETWEEN('Date'[Date], anchorDate, tDate)   
    )
RETURN
IF(
    tDate < anchorDate,                                  
    BLANK(),
    InitialStock + NetSinceAnchor
)

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

v-pnaroju-msft
Community Support
Community Support

Thankyou, @bhanu_gautam@danextian@Shahid12523 and @AmiraBedh for your responses.

Hi PBIDev01,

We appreciate your inquiry submitted through the Microsoft Fabric Community Forum.

Based on my understanding of the scenario, please find attached a screenshot and a sample PBIX file which may assist in resolving the issue:

vpnarojumsft_0-1756888887291.png
We hope the information provided proves helpful. Should you have any further queries, please feel free to contact the Microsoft Fabric Community.

Thank you.

View solution in original post

PBIDev01
Advocate I
Advocate I

Hello again,

Thanks so much for sharing your ideas, they're really helpful. I'm currently putting together a sample file to share with you, but honestly, I think we're already close to a solution based on what you've suggested.
Thank you.

View solution in original post

HarishKM
Memorable Member
Memorable Member

@PBIDev01  Hey,
Use this as your previous dax, I have created this.

 

ProjectedStock =
VAR CurrentPeriod = MAX(Calendar[Period])
VAR InitialStock = CALCULATE([V1]+[V2]+Stock_At_Time-[V3+V4], Calendar[Period] = 202508)
VAR ProjectedStockInitial = InitialStock

RETURN
SUMX(
FILTER(
ALL(Calendar[Period]),
Calendar[Period] <= CurrentPeriod
),
IF(
Calendar[Period] = MIN(Calendar[Period]),
ProjectedStockInitial,
[V1] + [V2] + [ProjectedStock] - [V3] - [V4]
)
)

 

Thanks

Harish M

Kindly give Kudos and accept it as solution if its solves your problem

View solution in original post

7 REPLIES 7
HarishKM
Memorable Member
Memorable Member

@PBIDev01  Hey,
Use this as your previous dax, I have created this.

 

ProjectedStock =
VAR CurrentPeriod = MAX(Calendar[Period])
VAR InitialStock = CALCULATE([V1]+[V2]+Stock_At_Time-[V3+V4], Calendar[Period] = 202508)
VAR ProjectedStockInitial = InitialStock

RETURN
SUMX(
FILTER(
ALL(Calendar[Period]),
Calendar[Period] <= CurrentPeriod
),
IF(
Calendar[Period] = MIN(Calendar[Period]),
ProjectedStockInitial,
[V1] + [V2] + [ProjectedStock] - [V3] - [V4]
)
)

 

Thanks

Harish M

Kindly give Kudos and accept it as solution if its solves your problem

PBIDev01
Advocate I
Advocate I

Hello again,

Thanks so much for sharing your ideas, they're really helpful. I'm currently putting together a sample file to share with you, but honestly, I think we're already close to a solution based on what you've suggested.
Thank you.

v-pnaroju-msft
Community Support
Community Support

Thankyou, @bhanu_gautam@danextian@Shahid12523 and @AmiraBedh for your responses.

Hi PBIDev01,

We appreciate your inquiry submitted through the Microsoft Fabric Community Forum.

Based on my understanding of the scenario, please find attached a screenshot and a sample PBIX file which may assist in resolving the issue:

vpnarojumsft_0-1756888887291.png
We hope the information provided proves helpful. Should you have any further queries, please feel free to contact the Microsoft Fabric Community.

Thank you.

AmiraBedh
Super User
Super User

Hello !

I don't think that you need recursion here what I understood is that you need an anchored running balance. (please correct me if I am wrong).

You need to have a proper Date table related to your fact and a [YearMonth] column like 202508 (INT or TEXT). If your V1–V4 are columns you need to wrap them as measures.

 

Net Flow =
VAR Inflow  = COALESCE([V1],0) + COALESCE([V2],0)
VAR Outflow = COALESCE([V3],0) + COALESCE([V4],0)
RETURN Inflow - Outflow

You can create a disconnected table Anchor Period with one column [YearMonth] and let the user pick the extraction period :

Anchor Date =
VAR AnchorYM = SELECTEDVALUE('Anchor Period'[YearMonth])
RETURN
CALCULATE(
    MIN('Date'[Date]),
    ALL('Date'),
    'Date'[YearMonth] = AnchorYM
)

Or if you always want to anchor to the last period that has stock at time:

Anchor Date =
CALCULATE(
    MAX('Date'[Date]),
    FILTER(
        ALL('Date'),
        NOT ISBLANK( CALCULATE([Stock At Time]) )
    )
)

Then projected stock :

Projected Stock =
VAR tDate       = MAX('Date'[Date])
VAR anchorDate  = [Anchor Date]
VAR InitialStock =
    CALCULATE(
        [Stock At Time],
        ALL('Date'[Date]),
        'Date'[Date] = anchorDate
    )
VAR NetSinceAnchor =
    CALCULATE(
        [Net Flow],
        ALL('Date'[Date]),                               
        DATESBETWEEN('Date'[Date], anchorDate, tDate)   
    )
RETURN
IF(
    tDate < anchorDate,                                  
    BLANK(),
    InitialStock + NetSinceAnchor
)

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696
Shahid12523
Resident Rockstar
Resident Rockstar

Use this

ProjectedStock =
VAR CurrPeriod = MAX(FactStock[Period])
VAR BasePeriod = 202508 // extraction period
VAR StartStock =
CALCULATE(MAX(FactStock[Stock_At_Time]), FactStock[Period] = BasePeriod)
VAR Movements =
SUMX (
FILTER ( FactStock, FactStock[Period] <= CurrPeriod && FactStock[Period] > BasePeriod ),
FactStock[V1] + FactStock[V2] - FactStock[V3] - FactStock[V4]
)
RETURN
StartStock + Movements

Shahed Shaikh
danextian
Super User
Super User

It would be easier for us to come up with a solution if you just provided a sample pbix with confidential data removed.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
bhanu_gautam
Super User
Super User

@PBIDev01 First, calculate the ProjectedStock for the initial period (e.g., 202508) using a calculated column. This will serve as the base for subsequent calculations.

ProjectedStock_Initial = [V1] + [V2] + Stock_At_Time - ([V3] + [V4])

 

Use a calculated table to iterate over the periods and calculate the ProjectedStock for each subsequent period. This approach avoids circular dependencies by storing intermediate results in a separate table.

ProjectedStockTable =
VAR BasePeriod = 202508
VAR MaxPeriod = 202511
RETURN
GENERATE(
FILTER(
ADDCOLUMNS(
GENERATESERIES(BasePeriod, MaxPeriod, 1),
"ProjectedStock",
VAR CurrentPeriod = [Value]
VAR PreviousPeriodStock =
IF(CurrentPeriod = BasePeriod,
[V1] + [V2] + Stock_At_Time - ([V3] + [V4]),
CALCULATE(
MAX(ProjectedStockTable[ProjectedStock]),
ProjectedStockTable[Period] = CurrentPeriod - 1
)
)
RETURN
PreviousPeriodStock + [V1] + [V2] - ([V3] + [V4])
),
[Value] >= BasePeriod
),
ROW("Period", [Value])
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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