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

后端 未结 3 1718
心在旅途
心在旅途 2021-01-16 02:37

I can\'t get a division correct with this sample data:

Calculated column    Another calc. column
   48                  207
  257                  370
  518          


        
相关标签:
3条回答
  • 2021-01-16 03:01

    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"
        )
    )
    
    0 讨论(0)
  • 2021-01-16 03:06

    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!!

    0 讨论(0)
  • 2021-01-16 03:20

    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 your 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.
    0 讨论(0)
提交回复
热议问题