PowerBI conditional calculation is not working.
I have created custom column and I am writing formula where I can get two different calculation for past month and current month
I have tried if else and switch function but it is not giving desired result.
This is Difference I am calculating based on 3 different columns from 3 different data sources.
Difference =
( SUM ( Opportunity[Revenue] ) + SUM ( 'August 2019'[Revenue] ) )
- SUM ( '2018 Invoice'[Revenue] )
I would like to get result where (Opportunity[Month]) = 1 ,2,3,4,5,6, 7 then Difference should be
Difference = sum('August 2019' [Revenue]))-sum('2018 Invoice'[Revenue])
else
Difference =
( SUM ( Opportunity[Revenue] ) + SUM ( 'August 2019'[Revenue] ) )
- SUM ( '2018 Invoice'[Revenue] )
How about this?
Difference =
IF (
Opportunity[Month] IN { 1, 2, 3, 4, 5, 6, 7 },
SUM ( Opportunity[Revenue] )
)
+ SUM ( 'August 2019'[Revenue] )
- SUM ( '2018 Invoice'[Revenue] )
If the Month > 7, then the IF returns BLANK() and you just get the last two terms.
Related
I have the following table
Date
Hour
Value
2022-11-17
15:21:00
1
2022-11-17
22:19:00
2
2022-11-18
00:44:00
2
2022-11-18
04:27:00
3
2022-11-18
13:21:00
2
2022-11-18
23:55:00
1
2022-11-19
09:01:00
4
Now I would like to sum the values based on the day and the specified hour range
It should sum from hour 6:00 of the previous day to hour 6:00 of today.
For example: So the first four rows should be the sum of 2022-11-17 and so on.
Date (6-6)
Sum of values
2022-11-17
8
2022-11-18
3
2022-11-19
4
At the moment I can sum values between hours, but on the same day
Usage =
CALCULATE(
SUM(Table[Value]),
TIME(
HOUR(Table[Hour]),
MINUTE(Table[Hour]),
SECOND(Table[Hour])
) >= TIMEVALUE("02:00:00"),
TIME(
HOUR(Table[Hour]),
MINUTE(Table[Hour]),
SECOND(Table[Hour])
) <= TIMEVALUE("14:00:00")
)
`
You can try this measure:
Usage =
VAR _min_time = SELECTEDVALUE ( 'Table'[Date] ) + TIME ( 6, 0, 0 )
VAR _max_time = SELECTEDVALUE ( 'Table'[Date] ) + 1 + TIME ( 6, 0, 0 )
RETURN
IF (
ISINSCOPE ( 'Table'[Date] ) ,
CALCULATE (
SUM ( 'Table'[Value] ) ,
FILTER (
ALL ( 'Table' ) ,
VAR _time = [Date] + [Hour]
RETURN _min_time < _time && _time <= _max_time
)
)
)
Calculates your desired result:
I am trying to compute the percentage of Free to Paid Sales conversions for a particular period.
Sales become paid after the threshold of 5 days. So Conversion % should exclude the last 5 days. Expected output is below.
Below are the measure I have created.
FreeSales : SUM( DATA[Free_Trials])
Conversions : SUM(DATA[Conversions])
Conv % : Calculate ( DIVIDE( FreeSales/Conversions,0), DATESBETWEEN(DATA[DATE], STARTDATE, ENDDATE-5))
(P.S: STARTDATE & ENDDATE are the min & max values from the date slicer)
Conv % is not working properly . It giving same value for all the rows in the table. Please help to fix this issue.
Thanks in advance!
You can create a calculated column with the below DAX formula.
Column2 =
VAR C = MAX ( Sheet1[Date] )
VAR RESULT =
IF (
( Sheet1[Date] ) = C,
0,
IF (
Sheet1[Date] = C - 1,
0,
IF (
Sheet1[Date] = C - 2,
0,
IF (
Sheet1[Date] = C - 3,
0,
IF (
Sheet1[Date] = C - 4,
0,
CALCULATE ( DIVIDE ( SUM ( Sheet1[Paid Sales] ), SUM ( Sheet1[Free Sales] ) ) )
)
)
)
)
)
RETURN
RESULT
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Trying to return loss provision ratio (loan provision/total assets) but for 1 year in the past based on the most recent date available. MO[Total]="11" is total loss provision, MO[Total]="15" is total assets, MO[Data_Date] is all dates. When I break these apart I get correct values, but when I combine them into a divide function it always returns blank. See code below:
Loss Provision BS LY =
DIVIDE (
CALCULATE (
SUM ( MO[Total] ),
MO[Line_Number] = "11",
DATEADD ( LASTDATE ( MO[Data_Date] ), -1, YEAR )
) * -1,
CALCULATE (
SUM ( MO[Total] ),
MO[Line_Number] = "15",
DATEADD ( LASTDATE ( MO[Data_Date] ), -1, YEAR )
)
)
I try to develop a dax code which calculates my companies obsolete stock. On the way to achieve this I had to derive some own measures (Please note that I am not the owner of the Datamart and cannot adjust something from there). In the final step it seems to be that one of my formulas (see 2nd below) requires too much processing power as I get the error message:Visual has exceeded the available resources. Since I am not an expert in PowerBI/DAX I am wondering whether the structure of my code is maybe causing those performance issue and whether I could make it smarter so that it finally works?
The measures I have created are following one:
1) Inventory Reach: This works fine and does not create any error message even though I run it on lowest level
Inv Reach =
CALCULATE (
DIVIDE ( SUM ( dimMaterialPlantSpecific[StockQTY] ), [Avg cons 5y] * -1, 9.01 ),
FILTER (
dimMaterialBasic,
dimMaterialBasic[CrossPlantMaterialStatusID] <> "BI"
),
FILTER (
dimMaterialBasic,
dimMaterialBasic[CrossPlantMaterialStatusID] <> "BO"
)
) +
CALCULATE (
DIVIDE (
SUM ( dimMaterialPlantSpecific[StockQTY] ),
[Consumption QTY Last 3 Years] * -1,
9.01
),
FILTER ( dimMaterialBasic, dimMaterialBasic[CrossPlantMaterialStatusID] = "BO" )
)
2) Devaluation class: Here the trouble starts. When I add this measure to my visual it crashes and I get the error message mentioned in the header. So do you see any possibility for optimizations?
Dev Class =
SUMX (
dimMaterialBasic,
IF (
AND ( dimMaterialBasic[CrossPlantMaterialStatusID] = "BB", [Inv Reach] > 9 ),
1,
IF (
AND ( [Inv Reach] > 1, [Inv Reach] <= 2 ),
0.05,
IF (
AND ( [Inv Reach] > 2, [Inv Reach] <= 3 ),
0.15,
IF (
AND ( [Inv Reach] > 3, [Inv Reach] <= 4 ),
0.25,
IF (
AND ( [Inv Reach] > 4, [Inv Reach] <= 5 ),
0.35,
IF (
AND ( [Inv Reach] > 5, [Inv Reach] <= 6 ),
0.45,
IF (
AND ( [Inv Reach] > 6, [Inv Reach] <= 7 ),
0.55,
IF (
AND ( [Inv Reach] > 7, [Inv Reach] <= 8 ),
0.65,
IF (
AND ( [Inv Reach] > 8, [Inv Reach] <= 9 ),
0.75,
IF ( [Inv Reach] > 9, 0.85, 0 )
)
)
)
)
)
)
)
)
)
)
Your help is much appreciated.
Thank you in advance
There are a lot of issues with the code... I'll make some suggestions but keep in mind I have no way of testing them, and some key information is missing (data model and formulas for [Avg cons 5y] and [Consumption QTY Last 3 Years] measures).
First, create a basic measure for stock quantity to avoid writing it multiple times:
Stock Quantity = SUM ( dimMaterialPlantSpecific[StockQTY])
Then
Inventory Reach =
VAR Stock_Quantity = [Stock Quantity]
VAR Consumption_5Y = - [Avg cons 5y]
VAR Consumption_3Y = - [Consumption QTY Last 3 Years]
VAR Default_Reach = 9.01
RETURN
CALCULATE (
DIVIDE ( Stock_Quantity, Consumption_5Y, Default_Reach ),
NOT dimMaterialBasic[CrossPlantMaterialStatusID] IN { "BI", "BO" })
+
CALCULATE (
DIVIDE ( Stock_Quantity, Consumption_3Y, Default_Reach),
dimMaterialBasic[CrossPlantMaterialStatusID]="BO")
Here, we stored calculations in variables to avoid calculating them multiple times. I also removed multiple filtration. Default_Reach is stored in VAR to make code easier to understand and maintain.
Second measure can be simplified as follows:
Dev Class =
SUMX (
dimMaterialBasic,
VAR Inventory_Reach = MAX ( TRUNC ( [Inv Reach] ), 9 )
RETURN
IF (
dimMaterialBasic[CrossPlantMaterialStatusID] = "BB" && Inventory_Reach = 9,
1,
0.1 * Inventory_Reach - 0.05
)
)
SUMX is essentially a loop. The key problem with the original code is that it calculates measure [Inv Reach] many times, and it does it in a loop. To fix this, we can use a few techniques:
Store [Inv Reach] in a variable. This insures that it will be calculated only once for each iteration of the loop;
Then, your if conditions can be simplified if you notice that your results can be computed with a simple formula that depends on the integer part of Inv Reach (i.e, it's 0.85 - 0.1*(9 - [Inv Reach]), which you can simplify algebraically to 0.1*[Inv Reach] - 0.05. So, I calculated the integer part by truncating fractions, and also capped it at 9 because all levels above 9 are treated the same way. The calculation then becomes a simple IF;
Please notice that you have not defined results for Inv Reach < 1, so I did not handle the situation.
enter image description hereI have daily values of closing balances of balance sheet. I want to have a measure which calculates the percentage change between two dates. The issue is that on weekdays the values are empty. So I have written the following measure which works when there are no filters, but when I place a date filter, the values of the measure become zeros.
Change_BS %(daily) =
IF (
SUM ( 'DB Daily_BS'[Value] ) = 0,
0,
IF (
SUM ( 'Date'[Weekday] ) = 1,
DIVIDE (
SUM ( 'DB Daily_BS'[Value] )
- CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), DATEADD ( 'Date'[Date], -3, DAY ) ),
CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), DATEADD ( 'Date'[Date], -3, DAY ) )
),
DIVIDE (
SUM ( 'DB Daily_BS'[Value] )
- CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), PREVIOUSDAY ( 'Date'[Date] ) ),
CALCULATE ( SUM ( 'DB Daily_BS'[Value] ), PREVIOUSDAY ( 'Date'[Date] ) )
)
)
)
de a new Column called BS_Value which fills in the gaps for the weekends.
BS_Value =
var wday = MOD(WEEKDAY(Balance[Date]);7)
var daysBack = IF(wday < 2; wday + 1)
return CALCULATE(SUM(Balance[Value]);Balance[Date] = EARLIER(Balance[Date]) - daysBack)
Based on this column I made BS-Change what is the increment/decrement form previous:
BS_Change = Balance[BS_Value] - CALCULATE(SUM(Balance[BS_Value]);Balance[Date] = EARLIER(Balance[Date]) - 1)
End result:
You can now make measures or other columns based on those values. I need to say a more dynamic solution is possible where we would go for a lookup of the last value not zero. I did not do this because I can imagine the balance is zero and it would then skip those values.