简体   繁体   中英

Power BI calculate the difference between two columns in a multi level matrix

Here is the source data:

https://i.stack.imgur.com/raYom.png

Columns: [Version, Unit, Customer, Quarter, Sales)

Here are the potential values:

https://i.stack.imgur.com/REmnV.png

*Note: We may have 10 different versions and 20+ different quarter year combinations.

Here is the output matrix in Power BI:

https://i.stack.imgur.com/FA1UE.png

*Users can select a Version and two quarters to compare.

Here are the Power BI Visualizations and Fields:

https://i.stack.imgur.com/VWd5A.png

I would like to create a measure to calculate the difference between Version 1 and 2 like this (Columns E and H):

https://i.stack.imgur.com/yWRQx.png

I'm able to create a new table with columns of sales for Version 1,2,3 then calculate the difference. The problem is I need the version and quarter to be dynamic. Any ideas how to do this in Power BI?

You can do something like

    Delta = IF (HASONEVALUE('Table'[Version]),
                   SUM('Table'[Sales]), 
                    CALCULATE(sum('Table'[Sales]), LASTNONBLANK('Table'[Version], sum('Table'[Sales]))) 
                  - CALCULATE(sum('Table'[Sales]), FIRSTNONBLANK('Table'[Version], sum('Table'[Sales])))
)

So whenever you have two versions in the filter context, it subtracts the first from the last, and whenever only one version is in the filter context, it passes the value through.

To add to this.

How to fix the duplicated columns in each of the subcategories.

I used the below DAX but somehow it's duplicated the calculated column "Delta TMCGP%" in all the subcategories. when i just want to show it at the end of the matrix table (Power BI)

Delta TMCGP% = CALCULATE([TMCGP%], FILTER( ALL(BC_Dashboard_V4_Standard[TYPE (groups)]), BC_Dashboard_V4_Standard[TYPE (groups)]="4_CWV") ) - CALCULATE([TMCGP%], FILTER( ALL(BC_Dashboard_V4_Standard[TYPE (groups)]), BC_Dashboard_V4_Standard[TYPE (groups)]="5_POR" ) )

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM