Sum of a measure column used in a further measure - powerbi

I have a matrix, in which there is a measure that calculates month on month progress in a column called progress
This is the code for it:
Progress =
VAR diff =
CALCULATE(SUM('forecasting'[forecast]),PARALLELPERIOD('forecasting'[month],-1,MONTH))
RETURN SUM('forecasting'[forecast]) - diff
I need to calculate where the monthly change has been allocated across items as a percentage with the following formula:
(individual forecast value / sum of calculated progress measure for whole month) * 100
How can I use the total progress measure across the whole month? I've tried using a SUMX(), as seen below, but this didn't work and many values came to 100. Is is even possible to do this?
Change =
VAR Total =
SUMX('forecasting', 'forecasting'[Progress])
RETURN ((forecasting[Progress] / Total) * 100)

Try this:
Change =
VAR Total =
CALCULATE(forecasting[Progress],FILTER(ALLSELECTED(forecasting),'forecasting'[month] = SELECTEDVALUE('forecasting'[month])))
RETURN ((forecasting[Progress] / Total) * 100)

Related

SUM of Column Virtual Table

I am facing this problem that I cannot solve.
I have this virtual table in power bi Desktop (made in excel just for you) where the last column should give me as a subtotal the sum of the TOTAL column, instead it gives me the multiplication between AVAILABLE * AVERAGE PRICE as it is the formula used for each row
I list the formulas I used:
AVERAGE PRICE = divide (SUMX (zzzAcqVen1, zzzAcqVen1 [ACQ AMOUNT AT COST]), SUMX (zzzAcqVen1, zzzAcqVen1 [QUANTITY PURCHASED]))
AMOUNT INPUT = SUM (zzzAcqVen1 [Q. INPUT]) * [AVERAGE PRICE]
https://i.stack.imgur.com/OJjY1.jpg
in red the wrong result
in green right result
Thanks everyone
Hello You can use this measure:
Calculate the AVERAGE_PRICE as Calculated Column.
AVERAGE_PRICE =
DIVIDE ( [ACQ AMOUNT AT COST], [QUANTITY PURCHASED] )
Then Calculate the AMOUNT_INPUT AS measure;
AMOUNT_INPUT = SUMX(zzzAcqVen1, [Q. INPUT] * [AVERAGE_PRICE] )

Power BI: Percentage of total over multiple sliced categories

I have data in the following format:
Category1
Category2
Category3
...
Amount
A
D
X
...
2
A
E
X
...
5
A
E
Y
...
1
B
D
Z
...
10
B
F
X
...
2
I want to be able to show (in a card, probably) the amount of Category1 with value A (this category has only 2 values) as a percentage of all. But I have slicers/controls on all the other categories and I want this to reflect the values set on those at any time. There are quite a few categories.
For example, when no controls are applied, the sum of amounts with Category1=A is 8 and the overall total is 20, so the percentage would be 40%. With Category3 set to 'X', the sum of Category1=A is 7 and the overall total is 9, so the percentage would be 78%. But I want this to work with any combination of one or more slicers over many categories.
I've found this on SO - Percentage value of a segment against segment total Power BI(DAX) - and tried creating a helper column to give the Amount where Category1=A and 0 if not. I've then tried to create a measure, using (pseudocode):
%Amount = DIVIDE ( SUM ( Table[HelperColumn] ), CALCULATE ( SUM (Table[Amount]), ALLEXCEPT(Table[Category1]) ) )
but it's showing 0%.
I'm new to Power BI and DAX and trying to replicate something I've done in another system, so I don't really understand what that formula is doing and I'm not really sure where to go from here.
I created 2 dax:
First is numerator(used to calculate stack amount based on probability of all 3 categories)Please refer SS attached
numerator =
var cat3Selected = SELECTEDVALUE(Stack[Category3])
var cat2Selected = SELECTEDVALUE(Stack[Category2])
var cat1Total =
CALCULATE(
IF(and(ISBLANK(cat2Selected),ISBLANK(cat3Selected)),
sum(Stack[Amount]),
IF(and(not(ISBLANK(cat2Selected)), not(ISBLANK(cat3Selected))),
CALCULATE(SUM(Stack[Amount]),Stack[Category2] = cat2Selected && Stack[Category3]=cat3Selected),
IF(NOT(ISBLANK(cat2Selected)),
CALCULATE(sum(Stack[Amount]),Stack[Category2]=cat2Selected),
CALCULATE(sum(Stack[Amount]),Stack[Category3]=cat3Selected)
)
))
)
return cat1Total
Second (Denominator ) is used to calculate stack amount for all category
*Denominator = CALCULATE(SUM(Stack[Amount]),ALL(Stack[Category1]))*
& then calculate % ,
*Percentage = DIVIDE(Stack[check msr],[normal all mSR],0)*
attached some screenshot for your reference:

PowerBI: how to get average of sum of multiple categories

For a report, I would like to have the average of a sum over 2 categories (date_type_project & ID).
In PowerBI, I would like to have 2 cards with 1 number. There'll be cards with the numbers:
data_type_project - non-standard, 30.44
data_type_project - standard, 9.47
The numbers are composed as follow: (29 + 33 + 20.25 + 39.5) / 4 = 30.44
and (3.5 + 25.5 + 9.75 + 17.5 + 17.25 + 14.5 + 1.5 + 1.25 + 4.5) / 9 = 9.47
How can I calculate this number in DAX?
You could try the following and use ISINSCOPE() function:
measure_hours =
-- will check, if 'Table'[ID] is in current filter context
var cond = ISINSCOPE('Table'[ID])
RETURN
IF(
cond,
SUM('Table'[hours]),
AVERAGE('Table'[hours])
)
You can create a measure and place it in place of hours
Measure = SUM([Hours])/COUNT([ID])
I've found a workaround that is answering my question: I've created a measure. Then, put it in a card visual and set the legend to 'data_type_project'. Thank you for the help! :)
measure =
AVERAGEX(
KEEPFILTERS(VALUES('Table'[ID])),
CALCULATE(SUM('Table'[hours]))
)

ratio goes beyond 100%

I have a measure " realiseret ratios %". It calculates the allocation of cost in % but when I filter out some variables like 105 in left column then it recalculates the ratio and i goes beyond 100%. the ratio market with red must never surpass 100%. picture 1 is with no filter(including 105) and picture 2 is with filter(excluding 105)
The code for the measure is:
realiseret ratios %:= Var Income =
CALCULATE(
[Project Cost Amount],
All(Art),Art[Type]= "I"
)
Var Expense =
CALCULATE(
ABS([Project Cost Amount]) )
return Divide(If(ISBLANK(Expense), Blank(), Expense) , Income,Blank())
Art type "I" is all our revenue. and "199 omsætning" is the revenue in left column

Unit testing Datetime values

I have a function that makes use of the current time (now). The Contract as a whole is a Crowdfunding token and the cost of tokens differ depending on the date and time that tokens are purchased.
How does one simulate different times when testing a Smart Contract? For instance, with regards to the code below, I would like to do unit testing to find out if the code for setting price is correct but I can't change the value of now.
Would it be a good solution to simply substitute the now keyword for another temporary testing variable, say now_sim and then manually changing now_sim during simulation?
if (now < (startTime + 1 days)) {
currentPrice = safeDiv(safeMul(price, 80), 100); // 20 % discount (x * 80 / 100)
}
else if (now < (startTime + 2 days)) {
currentPrice = safeDiv(safeMul(price, 90), 100); // 10 % discount (x * 90 / 100)
}
else if (now < (startTime + 12 days)) {
// 1 % reduction in the discounted rate from day 2 until day 12 (sliding scale per second)
// 8640000 is 60 x 60 x 24 x 100 (100 for 1%) (60 x 60 x 24 for seconds per day)
currentPrice = price - safeDiv(safeMul((startTime + 12 days) - now), price), 8640000);
}
else {
currentPrice = price;
}
If you use pyethereum for testing - which I highly recommend, it's lovely - you can directly alter the timestamp of the simulated block that is mining your transaction.
self.s = t.state()
self.s.block.timestamp = self.s.block.timestamp + 86400
self.s.mine(1)
some_val = your_contract.do_something(some_parameter)
self.assertEqual(some_val, whatever)
See a working example here (maybe a bit out-of-date): https://github.com/realitykeys/subjectivocracy/blob/master/contracts/test.py#L85