Forum Discussion

dilipan_mp's avatar
dilipan_mp
New Member
5 years ago
Solved

Power Query for Multiple category

Dear All,   I am having below table in Power BI. I am trying to find the query to do category wise summation and percentage.  Can you help us how to do in Power BI for the required output?   In t...
  • v-alq-msft's avatar
    5 years ago

    Hi, dilipan_mp 

     

    Based on your description, I'd like to suggest you try creating measures and displaying the result in a matrix visual. I created data to reproduce your scenario.

    Table:

     

    You may create three measures as below.

    Total Production = 
    var txt = LEFT(SELECTEDVALUE('Table'[KPI]),2)
    return
    IF(
        ISINSCOPE('Table'[KPI]),
        IF(
            txt<>"PC",
            SUM('Table'[KPI Value])
        ),
        IF(
            ISINSCOPE('Table'[City]),
            CALCULATE(
                SUM('Table'[KPI Value]),
                FILTER(
                    'Table',
                    'Table'[KPI Category]="Production"
                )
            ),
            CALCULATE(
                SUM('Table'[KPI Value]),
                FILTER(
                    'Table',
                    'Table'[KPI Category]="Production"
                )
            )
        )
    )
    % of Total Production = 
    var txt = LEFT(SELECTEDVALUE('Table'[KPI]),2)
    return
    IF(
        ISINSCOPE('Table'[KPI]),
        IF(
            txt<>"PC",
            DIVIDE(
               SUM('Table'[KPI Value]),
               CALCULATE(
                   SUM('Table'[KPI Value]),
                   FILTER(
                       ALLEXCEPT('Table','Table'[City]),
                       'Table'[KPI Category]="Production"
                   )
               )
            )
        ),
        IF(
            ISINSCOPE('Table'[City]),
            1,
            DISTINCTCOUNT('Table'[City])
        )
    )
    Total Production/Total Power = 
    var txt = LEFT(SELECTEDVALUE('Table'[KPI]),2)
    return
    IF(
        ISINSCOPE('Table'[KPI]),
        IF(
            txt<>"PC",
            [% of Total Production]*
            CALCULATE(
                SUM('Table'[KPI Value]),
                FILTER(
                    ALLEXCEPT('Table','Table'[City]),
                    'Table'[KPI Category]="Power"
                )
            )
        ),
        IF(
            ISINSCOPE('Table'[City]),
            CALCULATE(
                SUM('Table'[KPI Value]),
                FILTER(
                    ALLEXCEPT('Table','Table'[City]),
                    'Table'[KPI Category]="Power"
                )
            ),
            CALCULATE(
                SUM('Table'[KPI Value]),
                FILTER(
                    'Table',
                    'Table'[KPI Category]="Power"
                )
            )
        )
    )

     

    Result:

     

    Best Regards

    Allan

     

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