Forum Discussion

veikko's avatar
veikko
Frequent Visitor
3 years ago

Calculating costs per Path on hierarchical data

Hello,

I have a million rows of hierarchical data which looks like this:

IDPathLevelCostPathCostCode
11010120 
21|2115110XXX
31|2|322095 
41|2|3|431025 
51|2|3|4|5455 
61|2|3|4|641010YYY
71|2|3|732050 
81|2|3|7|841520 
91|2|3|7|8|9555ZZZ
101|2|3|7|1041010 


PathCost is a calculated column which calculates the costs for every row. Costs are cumulated from every row that contain row's path. This is the code for the PathCost column:

PathCost =
    VAR Id_ = [Id]
    RETURN
        SUMX (
            FILTER (
                COSTS,
                FIND(Id_, COSTS[Path], 1, BLANK()) > 0
            ),
            [Cost]
        )

The problem is that the calculated column is very slow. I narrowed the data to 10,000 rows and it loaded the dax for over 15 minutes. Do you have any suggestions how the code could be optimized? I do not need the costs for every row, only the rows where column Code has a value.

I also tried finding paths inside paths instead of finding id's inside paths. It was not significantly faster.

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi veikko ,

     

    Please try using a measure instead of calculated column and then output them as tabular visuals.

    athCost =
        VAR Id_ = max([Id])
        RETURN
            SUMX (
                FILTER (
                    all(COSTS),
                    FIND(Id_, COSTS[Path], 1, BLANK()) > 0
                ),
                [Cost]
            )
    

    For more information about DAX optimization, you can refer to:

    Performance Tuning DAX - Part 1 - Microsoft Power BI Community

    DAX Best Practices | MAQ Software Insights

     

    Best Regards,

    Neeko Tang

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