I'm new to Power BI so I may have titled this wrong but I'm trying to use a SUMPRODUCT in my table but the total is incorrect.
My goal is to take the SUMPRODUCT of (Linear_Feet_Out,Hrs) / SUM(Linear_Feet_Out)
So I broke it down to try and solve the problem but am having no luck. Slitting_Time_New is just my est field to make sure each row is calculating correctly. So 2.000 * 226,795 = 453,589
However the results I am expecting are 2.54 in the "Slitting_Time_Avg" field total.
Here is what I have so far:
Slitting_Time_Avg =
VAR Numerator = [Slitting_Time_New]
VAR Denominator =
SUM (Mfng_Analysis[Linear_Footage_Out] )
RETURN
CALCULATE ( DIVIDE ( Numerator, Denominator ) )
But I want it to take the sum of Slitting_Time_New (a calc measure), not the 8,719,451.50
Slitting_Time_New = SUM(Mfng_Analysis[Hrs])*SUM(Mfng_Analysis[Linear_Footage_Out])
This is due to evaluation context. When using the SUM function, DAX has no row context, unless you actively provide it in a visual table, i.e. for the Total row in your table [Slitting_Time_New] will evaluate
(2,000+2,900+0,800)x(226795+1136807+166127).
What you want to do, I guess, is
(2,0000x226795)+(2,900x1136807)+(0,800x166127)
In order to do that you have to provide row context. This you can do with an aggregator function, such as SUMX.
Slitting_Time_New =
SUMX(
Mfng_Analysis;
[Hrs]*[Linear_Footage_Out]
)
Related
I have a column named Cur_bal in power bi I just want to know what day function could help me change all negative a to 0 and keep all other values the same.
You can use either a calculated column or a measure. The formula for a calculated column would be:
Cur_Bal - Positive_Only =
IF ( 'Table'[Cur_Bal] < 0, 0, 'Table'[Cur_Bal] )
Once you have that, you can use it in an implicit or explicit measure to sum up the total.
For a measure, it would look like this:
Cur_Bal - Positive_Only (measure) =
CALCULATE ( SUM ( 'Table'[Cur_Bal] ), 'Table'[Cur_Bal] > 0 )
The measure uses a CALCULATE function with a simple filter on Cur_bal column.
Link to image of output sample table...
I have the code underneat in Amazone Athena to calculate how the last 7 days in footfall compare to the average week last year.
I only get a 0 as a result all the time, what is the problem.
I tried to make the x and y as Float but that still gave zero's
The data is on daily basis and a calculate a week average of last year by addding all weeks and divide by 52 (probably also a better way to do this)
select x.visitors/y.visitors*100
from
(select sum(visitors) as visitors
from corrected_scanners_per_day
where btcode in ('BT120031', 'BT120000','BT902', 'BT120052', 'BT120050', 'BT130109', 'BT120131', 'BT130110', 'BT130107', 'BT120126', 'BT120078', 'BT120076', 'BT120035', 'BT130450', 'BT120063', 'BT120044', 'BT120082', 'BT120030', 'BT120116', 'BT121196', 'BT130366', 'BT120085', 'BT120053', 'BT120014')
And datetime
BETWEEN current_date - interval '7'day
AND current_date) x
Join
(select sum(visitors) / 52 as visitors, year(datetime) as year
from corrected_scanners_per_day
where btcode in ('BT120031', 'BT120000','BT902', 'BT120052', 'BT120050', 'BT130109', 'BT120131', 'BT130110', 'BT130107', 'BT120126', 'BT120078', 'BT120076', 'BT120035', 'BT130450', 'BT120063', 'BT120044', 'BT120082', 'BT120030', 'BT120116', 'BT121196', 'BT130366', 'BT120085', 'BT120053', 'BT120014')
And datetime
BETWEEN CAST('2019-01-01' AS timestamp)
AND CAST('2019-12-31' AS timestamp)
group by year(datetime)
order by year(datetime)) y on 1=1
This is happening because your data types (visitors and 100) are all INT, so your output data type will also be an INT. When the calculation is converted to an INT, it is probably rounding to 0. So you need to explicitly make sure your final data type is a numeric type that allows decimals.
Try changing the first line to this:
select (x.visitors * 1.0) / (y.visitors * 1.0) * 100.0
You have other INT types inside your subqueries that should also probably be converted to decimals to make sure no truncation or rounding is occurring.
Sup, simple question ( i hope). I am adding a custom column in Power bi. I need to subtract time values, using custom column formula. Problem: (B-A)-C causes error.
Values are set to time type.
B = 15.00.00
A = 9.00.00
C = 0.05.00
custom column formula:
=([B]-[A])-[C]
Result i want: 5.55.55
Result i get:
Expression.Error: We cannot apply operator - to types Duration and Time.
Details:
Operator=-
Left=0.06:00:00
Right=0.05.00
So B-A = 0.06:00:00 and therefore 0.06:00:00 - 0.05.00 = error. I need to get B-A result in shape of 06.00.00 so i can subtract value C from it. Any suggestions?
Assuming your table is called "Table":
First, create a new column and calculate the difference of B and A.
Col1 = DATEDIFF(Table1[B],Table1[A],HOUR)
Then create another column and subtract C from it.
Col2 = DATEDIFF(Table1[Col1],Table1[C],HOUR)
would really love to get some help on a sas program i am trying to write
I have five variables that have the options Interest or Rewards
call them wo1 wo2 wo3 wo4 wo5
is there a way to do an if statement that checks all five for the value of "Interest" and then produces a calcualted variable with the totals of the variable that holds the amount value which are wo1amt wo2amt wo3mt etc.
and the ones that do not have the value of interest return a value of 0.
or would i need to do an if statement for each, creating a calc var for each and then summing them all together with another ??
any assistance, direction would be very appreciated...
If I'm understanding correctly, it looks like you will need multiple if statements.
You could initialize a total variable, and then add to it for each contributing variable, like this:
total = 0;
if wo1 = 'Interest' then total = total + wo1amt;
if wo2 = 'Interest' then total = total + wo2amt;
if wo3 = 'Interest' then total = total + wo3amt;
if wo4 = 'Interest' then total = total + wo4amt;
if wo5 = 'Interest' then total = total + wo5amt;
Arrays work nicely for situations like this.
array wo(*) wo1-wo5;
array woamt(*) wo1amt wo2amt wo3amt wo4amt wo5amt;
total = 0;
do i = 1 to 5;
total + woamt(i)*(wo(i)='Interest');
end;
Assume you are given three variables, revenue, expenses, and profit, all of type Money (a structured type with two int fields, dollars and cents). Assign to profit the result of subtracting expenses from revenue. Let's make the happy assumption that revenue exceeds expenses. However you still may find that the cents part of expenses exceeds that of revenue. If that is the case you will have to "borrow" 1 from revenue dollars (i.e. subtract 1) and "give" it to revenue's cents (i.e. add 100!) in order to carry out the subtraction properly.
Here is what I have but it's not working:
if (revenue.cents < expenses.cents)
{
revenue.dollars = revenue.dollars -1;
revenue.cents = revenue.cents + 100;
profit = revenue - expenses;
}
else
{
profit = revenue - expenses;
}
I get this error message:
error: no match for 'operator-' in 'revenue - expenses'
Any help is appreciated. Thanks.
You're getting that error because you cannot subtract one structure from another. You will have to define an operator-() function for your struct.
You need to call each element of the structure and subtract them separately. MyProgLab will not allow you to define a function for this exercise. Not only that but if you enter the code that you have above it will tell you that 'you were not supposed to modify the element'. In order to avoid this you must conduct the borrowing of the dollar inside the arithmetic.
Like this:
if(expenses.cents > revenue.cents)
{
profit.dollars = (revenue.dollars - 1) - expenses.dollars;
profit.cents = (revenue.cents + 100) - expenses.cents;
}//end if