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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
NBU_FFF
Helper I
Helper I

Repeated Calculated Column Optimization

Dear community, I have a table need to add several calculation columns.

Day0 = '1'[Qty]-'1'[Qty]/('1'[End_GroupQty_Previous]+'1'[VS_Qty])*'1'[Capacity]

Day1 = 
VAR CurrentDate = '1'[MAV]
VAR CurrentVS = '1'[VS]
VAR CurrentLine = '1'[Line]
VAR Previous_Qty2 =
    CALCULATE(
        MAX('1'[Day0]),
        FILTER(
            '1',
            '1'[vs] = CurrentVS &&
            '1'[line] = CurrentLine &&
            '1'[mav] < CurrentDate
        ),
        TOPN(1, 
            FILTER('1', '1'[vs] = CurrentVS && '1'[line] = CurrentLine && '1'[mav] < CurrentDate), 
            '1'[mav], 
            DESC
        )
    )
RETURN
    ('1'[Qty] + Previous_Qty2) - 
    ('1'[Qty] + Previous_Qty2) / ('1'[End_GroupQty_Previous] + '1'[VS_Qty]) * '1'[Capacity]


Day2 = 
VAR CurrentDate = '1'[MAV]
VAR CurrentVS = '1'[VS]
VAR CurrentLine = '1'[Line]
VAR Previous_Qty2 =
    CALCULATE(
        MAX('1'[Day1]),
        FILTER(
            '1',
            '1'[vs] = CurrentVS &&
            '1'[line] = CurrentLine &&
            '1'[mav] < CurrentDate
        ),
        TOPN(1, 
            FILTER('1', '1'[vs] = CurrentVS && '1'[line] = CurrentLine && '1'[mav] < CurrentDate), 
            '1'[mav], 
            DESC
        )
    )
RETURN
    ('1'[Qty] + Previous_Qty2) - 
    ('1'[Qty] + Previous_Qty2) / ('1'[End_GroupQty_Previous] + '1'[VS_Qty]) * '1'[Capacity]

 

The same logic for day2 applied on days3 and next by only change last row '1'[Qty] to '2'[Qty], '3'[Qty]…

In other words, in order to get Day9, I have to create columns next from Day3 to Day9.

 

Do we have other ways to use recurrence and create day9 directly? I Attached my sample file for your reference.

sample.pbix

 

 

8 REPLIES 8
v-kathullac
Community Support
Community Support

Hi @NBU_FFF ,

 

As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?


If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.


Regards,

Chaithanya.

v-kathullac
Community Support
Community Support

Hi @NBU_FFF ,

 

As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?


If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.


Regards,

Chaithanya.

Hi @NBU_FFF ,

 

As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?


If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.


Regards,

Chaithanya.

DataNinja777
Super User
Super User

Hi @NBU_FFF ,

 

The best approach to optimize your repeated Day0–Day9 calculated columns is to offload the recursive logic into Power Query using index-based row referencing. DAX is not well-suited for recursion over calculated columns because it lacks true looping behavior and cannot reference previously calculated column values in subsequent rows. Power Query, on the other hand, allows row-by-row processing using index columns and custom steps. You can start by sorting your table by [Line], [VS], and [MAV], then add an index column starting from 0. This index will serve as the “day” indicator, and you can reference the previous row's result for the recursive logic. After adding the index, you create a custom column such as [DayQty] using a recursive function. Here’s a simplified version of how the logic might look:

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    SortedRows = Table.Sort(Source,{{"Line", Order.Ascending}, {"VS", Order.Ascending}, {"MAV", Order.Ascending}}),
    AddIndex = Table.AddIndexColumn(SortedRows, "Day", 0, 1, Int64.Type),
    AddDayQty = Table.AddColumn(AddIndex, "DayQty", each null, type number),
    RecursiveCalculation = List.Accumulate(
        {0..Table.RowCount(AddDayQty)-1},
        AddDayQty,
        (state, current) =>
            let
                row = state{current},
                prev = if current = 0 then null else state{current - 1},
                qty = row[Qty],
                cap = row[Capacity],
                endGroupQty = row[End_GroupQty_Previous],
                vsQty = row[VS_Qty],
                prevQty = if current = 0 then null else prev[DayQty],
                calc = if current = 0 then
                    qty - qty / (endGroupQty + vsQty) * cap
                else
                    (qty + prevQty) - (qty + prevQty) / (endGroupQty + vsQty) * cap,
                newState = Table.ReplaceRows(state, current, {Record.TransformFields(row, {"DayQty", each calc})})
            in
                newState
    )
in
    RecursiveCalculation

This logic processes the table row-by-row while calculating the DayQty recursively. The first row (Day0) uses your original formula, and all subsequent rows use the previous row’s DayQty to compute the current one. This way, you no longer need to manually create Day1 to Day9 columns in DAX. You end up with a single [DayQty] column that contains the recursive result for each day index, and the logic can be extended to as many days as needed without repetition.

 

Best regards,

@DataNinja777 thanks a lot for your reply and explain. Unfortunately my data was combined from different sources and could not be processed in Power Query only. What's why I asked for the help with DAX.  

Hi @NBU_FFF ,

 

Apologies, my bad, DAX does not support true recursion, especially in calculated columns or tables where the value of a row depends on the value of a previous row in the same column. If you're trying to implement logic where each "DayN" calculation depends on the previous day’s result, DAX is not the right tool for this kind of recursion.

 

Best regards,

@DataNinja777  Thanks for your help again. I tried but showed error below:

NBU_FFF_0-1743518274355.png

 

Seemed DAX not support Recursive, not sure how you achieve that without error.

 

I attached my PBI file here, could you pls have a look?

sample_Dax.pbix

 

Hi @NBU_FFF,

 

DAX doesn't support true recursion in calculated columns or tables because it lacks row-by-row iteration based on previous results. Since Power Query isn't an option either, the best approach would be to use a measure instead of calculated columns.

 

Limitation: This won’t work for deep recursion across multiple rows because DAX measures rely on context, not row-by-row memory like Power Query.

 

If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it!


Regards,
Vinay Pabbu

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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