Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
nicoenz
Helper II
Helper II

Conditional calculations

Hi,

I really need some help with DAX. I can do basic calculations but this one is exceeding my skills.

In the table below I need to calculate the Volume Increase effect that it is defined as follows 

 

For each product:

1. Agregate the quantity and sales for 2020 and 2021 (the table contains also 2018, 2019, etc but i'm only interested in 2020 and 2021)

2. If the quantity for any of those years = 0 exclude that product from further calculation.

3. If the agregated quantity 2021 > than that of 2020 then multiply the price by the difference in quantities (2021-2020). Price is defined as the "total sales / total quantity" (where total quantity <>0, of course) for 2020

4. If the agregated quantity 2021 < than that of 2021 then allocate a 0 for that product

 

In summary, for each product:

=if(or(a=0,b=0),0,if( b > a, (b-a)*k, 0))

 

where:

a: total quantity for that product in 2020
b: total quantity for that product in 2021
c: total sales for product in 2020
d: total sales for product in 2021

k: price for that product in 2020. Price is calculated as = c/a

 

Fiscal yearProductQTYNSPrice
2020Product A80206225.78
2021Product A120294124.51
2018Product B-6-12010
2019Product B-8-16020
2020Product B0-1950.00
2020Product B-64-395361.77
2020Product B3202047563.98
2021Product B24182976.21
2021Product B72479866.64
2021Product C252855114.20
2021Product C5943188.60
2021Product C000.00
2020Product D161921120.06
2020Product D162782173.88
2020Product D000.00
2020Product D000.00
2021Product D000.00
2021Product D000.00
2021Product D164278267.38
2020Product E7622341630.73
2020Product E143613742.92
2020Product E5241639531.29
2020Product F2533213.28
2020Product F000.00
2020Product G52365473.00
2021Product G000.00
2021Product G25243797.48
2020Product H181233512.90
2021Product H000.00
2021Product H181245213.55
2021Product H181229912.70
2021Product H05970.00
2020Product J11130911278.48
2020Product J000.00
2020Product J-16-4224264.00
2020Product J11132923296.60
2020Product J12740644320.03
2020Product J328321260.03
2020Product J19148554254.21
2020Product J164475279.69
2020Product J6416886263.84
2020Product J17546570266.11
2020Product J6416831262.98
2020Product J000.00
2021Product J000.00
2021Product J000.00
2021Product J000.00
2021Product J000.00
2021Product J12742088331.40
2021Product J12741801329.14
2021Product J11135742322.00
2021Product J12739453310.65
2021Product J9525488268.29
2021Product J15942119264.90
2021Product J6416813262.70
2021Product J9525592269.39
2021Product J6417392271.75
2021Product J7921483271.94
2021Product J328449264.03
2021Product J4812535261.15
2020Product K03320.00

 

 

I solved it in excel with a pivot table giving a "Volume Increase Effect" of  82767 USD

 Sum of QTY Sum of NS Price Volume increase
 abcdk  
Row Labels20202021202020212020  
Product A801202062294125.775 1031
Product B2569616327662763.77734 0
Product C 30 37980  
Product D321647034278146.9688 0
Product E1429 45948 32.15395  
Product F25 332 13.28  
Product G52523652437473 9460
Product H1813622335475112.90055 2335
Product I 0 5970  
Product J8751128241891328955276.4469 69941.0549
Product K0 332 0  
       82767.05

 

I tried and tried and tried to do it in DAX and am certainly unsuccessful

 

 

 

1 ACCEPTED SOLUTION
v-yadongf-msft
Community Support
Community Support

Hi @nicoenz ,

 

Please add two additional measures to the measures @Bifinity_75  provided:

k = CALCULATE(sum('Table'[NS])/sum('Table'[QTY]),'Table'[Fiscal year]="2020")

Volume = 
var qty2020= CALCULATE(sum('Table'[Qty]),'Table'[Fiscal year]="2020")
var qty2021= CALCULATE(sum('Table'[Qty]),'Table'[Fiscal year]="2021")
return
IF(qty2021>qty2020,(qty2021-qty2020)*[k],0)

Sum_volumne = SUMX(ALL('Table'[Product]),[Volume])

Volume increase = IF(ISINSCOPE('Table'[Product]),[Volume],[Sum_volumne])

 

The result:

vyadongfmsft_0-1667892638101.png

Best regards,

Yadong Fang

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

View solution in original post

5 REPLIES 5
nicoenz
Helper II
Helper II

Hi @v-yadongf-msft ,

thanks for your reply!

Your model works perfectly well in your pbix file

but when i do excatly the same in mine, it still returns a 0:

nicoenz_0-1667904839067.png

exactly the same as with @Bifinity_75 

 

 

Adding an index colum, did the trick!!

v-yadongf-msft
Community Support
Community Support

Hi @nicoenz ,

 

Please add two additional measures to the measures @Bifinity_75  provided:

k = CALCULATE(sum('Table'[NS])/sum('Table'[QTY]),'Table'[Fiscal year]="2020")

Volume = 
var qty2020= CALCULATE(sum('Table'[Qty]),'Table'[Fiscal year]="2020")
var qty2021= CALCULATE(sum('Table'[Qty]),'Table'[Fiscal year]="2021")
return
IF(qty2021>qty2020,(qty2021-qty2020)*[k],0)

Sum_volumne = SUMX(ALL('Table'[Product]),[Volume])

Volume increase = IF(ISINSCOPE('Table'[Product]),[Volume],[Sum_volumne])

 

The result:

vyadongfmsft_0-1667892638101.png

Best regards,

Yadong Fang

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

Bifinity_75
Solution Sage
Solution Sage

I forgot to put the result:

Bifinity_75_0-1667845729834.png

 

Bifinity_75
Solution Sage
Solution Sage

Hi @nicoenz , try this:

- Create this measure:

k = CALCULATE(sum('Table'[Ns])/sum('Table'[Qty]),'Table'[Year]="2020")

 

- Create this measure:

Volume increase = var qty2020=CALCULATE(sum('Table'[Qty]),'Table'[Year]="2020")
var qty2021=CALCULATE(sum('Table'[Qty]),'Table'[Year]="2021")
return
IF(qty2021>qty2020,(qty2021-qty2020)*[k],0)

 

Best regards

 

 

the result in your excel is correct. but the measure in powerbi returns a 0 because it is calculating the total qty for 2020 and 2021 and comparing both as an agregated total.

the measure should do the comparison at product level rather than total level.

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors