How to divide each row of a calculated column by the total of another calculated column?

和自甴很熟 提交于 2019-12-02 05:46:15

Basically, there is nothing wrong with the calculated columns, and both Alexis and StelioK formulas are correct.

The root problem here is a confusion between calculated columns and measures. You are looking at the results in a conceptually wrong way - through the matrix visual, with several filters active on slicers. If you remove the filters, you will see that the total amount is 140,920, not 82,826. The latter number is the total for the filtered data set, not the entire table.

To get this right, you need to understand several fundamental concepts behind Power BI:

  • Calculated columns are always static. Once a calculation is completed, it can not respond to slicers or other UI controls. It's just static data, identical to data in non-calculated columns. DAX formulas used to calculate columns are active only when you create them, or upon data reload.
  • If you want your calculations to respond to slicers etc, they must be measures. It's the only way, no exceptions.
  • Avoid calculated columns, they are utterly useless. Power BI is all about measures; I can't think of a single reason for using calculated columns. When you add a column, you are essentially enhancing your source data, because you feel like you are missing something you need for your report. But that need can be much better addressed at the source (database or file you import), or using Power Query, which is designed exactly for this kind of tasks. The best practice is: build you columns at the source, for everything else design measures.
  • Another important advice: never drop fields (columns) into visuals directly. Always write a DAX measure, and then use it. Relying on Power BI auto-aggregations is a very bad practice.

You can do this by using the following DAX:

% Column =
VAR TotalSum =
    SUM ( 'Table'[Another Calc column] )
RETURN
    IF (
        NOT ( ISBLANK ( 'Table'[Calc Column] ) ),
        CALCULATE ( DIVIDE ( SUM ( 'Table'[Calc Column] ), TotalSum ) ),
        0
    )

Which yields the following:

I Hope it helps!!

For me the following works:

DIVIDE( Table1[Calculated column], SUM(Table1[Another calc column]) )

If that's not working, I'd need to see a file where you can reproduce the problem.


Edit: After looking at your file, the total of 82,826 is only true with the filters you've selected.

Note that calculated columns are not dynamic and cannot be responsive to filters since they are calculated only when the table is first loaded.

If you need it to be dynamic, then write it as a measure more like this:

Earned Daily =
DIVIDE (
    CALCULATE (
        SUM ( 'Test data'[Value] ),
        'Test data'[Act Rem] = "Actual Units",
        'Test data'[Type] = "Current"
    ),
    CALCULATE (
        SUM ( 'Test data'[Value] ),
        ALLSELECTED ( 'Test data' ),
        'Test data'[Act Rem] = "Remaining Units",
        'Test data'[Type] = "PMB"
    )
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!